EditText input filter decimal | Android kotlin

EditText input filter decimal | Android kotlin - Hello friend inabnomaniiyaha, In the article that you read this time with the title EditText input filter decimal | Android kotlin, we have prepared this article well for you to read and take information in it. hopefully the contents of the post Artikel kotlin,what we write you can understand. Alright, happy reading.

Judul : EditText input filter decimal | Android kotlin
link : EditText input filter decimal | Android kotlin

Baca juga


EditText input filter decimal | Android kotlin

EditText input filter decimal

MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)


        // apply edit text decimal input filter

        editText.inputFilterDecimal(

            // this values must be positive (0+) unless it throw exception

            maxDigitsIncludingPoint = 5,

            maxDecimalPlaces = 2

        )


        textView.text = "Max digits including point : 5"

        textView.append("\nMax decimal places : 2")

    }

}



// extension function to input filter edit text decimal digits

fun EditText.inputFilterDecimal(

    // maximum digits including point and without decimal places

    maxDigitsIncludingPoint: Int,

    maxDecimalPlaces: Int // maximum decimal places

){

    try {

        filters = arrayOf<InputFilter>(

            DecimalDigitsInputFilter(maxDigitsIncludingPoint, maxDecimalPlaces)

        )

    }catch (e: PatternSyntaxException){

        isEnabled = false

        hint = e.message

    }

}



// class to decimal digits input filter

class DecimalDigitsInputFilter(

    maxDigitsIncludingPoint: Int, maxDecimalPlaces: Int

) : InputFilter {

    private val pattern: Pattern = Pattern.compile(

        "[0-9]{0," + (maxDigitsIncludingPoint - 1) + "}+((\\.[0-9]{0,"

                + (maxDecimalPlaces - 1) + "})?)||(\\.)?"

    )


    override fun filter(

        p0: CharSequence?,p1: Int,p2: Int,p3: Spanned?,p4: Int,p5: Int

    ): CharSequence? {

        p3?.apply {

            val matcher: Matcher = pattern.matcher(p3)

            return if (!matcher.matches()) "" else null

        }

        return null

    }

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<androidx.constraintlayout.widget.ConstraintLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:app="http://schemas.android.com/apk/res-auto"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/constraintLayout"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="#FEFEFA"

    tools:context=".MainActivity">


    <EditText

        android:id="@+id/editText"

        android:layout_width="0dp"

        android:layout_height="wrap_content"

        android:layout_marginStart="12dp"

        android:layout_marginEnd="12dp"

        android:inputType="numberDecimal"

        android:padding="12dp"

        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toTopOf="parent"

        app:layout_constraintVertical_bias="0.12" />


    <TextView

        android:id="@+id/textView"

        android:layout_width="0dp"

        android:layout_height="wrap_content"

        android:layout_marginTop="12dp"

        android:fontFamily="sans-serif-condensed-medium"

        android:gravity="center"

        android:padding="8dp"

        android:textColor="#4F42B5"

        android:textSize="25sp"

        app:layout_constraintEnd_toEndOf="parent"

        app:layout_constraintStart_toStartOf="parent"

        app:layout_constraintTop_toBottomOf="@+id/editText"

        tools:text="TextView" />


</androidx.constraintlayout.widget.ConstraintLayout>




That's the article EditText input filter decimal | Android kotlin

That's it for the article EditText input filter decimal | Android kotlin this time, hopefully can be useful for all of you. well, see you in another article post.

You are now reading the article EditText input filter decimal | Android kotlin with link addresshttps://inabnonapudyawanabing.blogspot.com/2020/09/edittext-input-filter-decimal-android.html

0 Response to " EditText input filter decimal | Android kotlin"

Post a Comment

Tips Tricks for Android Phone

Tips & Tricks for Android Phone is a free android app and Collection of Tips and Tricks related to using your android mobile device lik...