Tuesday, September 1, 2020

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>


No comments:

Post a Comment