javaandroidandroid-fragmentsandroid-edittext

EditText doesn't accept digits as inputs


I have two EditTexts in my fragment. Both of them have android:inputType set to numberSigned|numberDecimal, since I wish to input only decimal numbers through them, which may be signed as well.

The issue is that if I completely delete the existing contents of the EditTexts, they simply don't accept digits as inputs, they only accept signs such as - (negative sign) or .(decimal point). So basically the only case when the text in them can be changed is if a digit is already present in there and we append some new digit/symbol.

Here, is a small demo of the problem I am facing:- Issue Demo

I have had a look at these similar issues:-

  1. EditText does not accept digits as input (describes the exact same problem that I'm facing)
  2. EditText is not accepting Numeral input

However, I don't have any wrapping layout/element which implements any method that may be consuming the ENTER event (such as setOnKeyListener or dispatchKeyEvent). Here is the layout file which has these EditTexts and is included in the main layout file of the fragment:-

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rounded_custom_border">

<io.pslab.others.FloatSeekBar
    android:id="@+id/seekbar_vertical_offset"
    android:layout_width="@dimen/dimen_zero_dp"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="@+id/spinner_channel_select_vertical_offset"
    app:layout_constraintEnd_toStartOf="@+id/guideline"
    app:layout_constraintStart_toEndOf="@+id/spinner_channel_select_vertical_offset"
    app:layout_constraintTop_toTopOf="parent" />

<io.pslab.others.FloatSeekBar
    android:id="@+id/seekbar_horizontal_offset"
    android:layout_width="@dimen/dimen_zero_dp"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/guideline"
    app:layout_constraintStart_toEndOf="@+id/spinner_channel_select_horizontal_offset"
    app:layout_constraintTop_toTopOf="@+id/spinner_channel_select_horizontal_offset" />

<Spinner
    android:id="@+id/spinner_channel_select_vertical_offset"
    android:layout_width="wrap_content"
    android:layout_height="@dimen/osc_spinner_height"
    android:layout_marginStart="@dimen/osc_cb_margin"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textview_vertical_offset_unit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="@dimen/osc_cb_margin"
    android:text="@string/unit_volts"
    app:layout_constraintBottom_toBottomOf="@+id/spinner_channel_select_vertical_offset"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Spinner
    android:id="@+id/spinner_channel_select_horizontal_offset"
    android:layout_width="wrap_content"
    android:layout_height="@dimen/osc_spinner_height"
    android:layout_marginStart="@dimen/osc_cb_margin"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

<TextView
    android:id="@+id/textview_horizontal_offset_unit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="@dimen/osc_cb_margin"
    android:text="@string/unit_milliseconds"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="@+id/spinner_channel_select_horizontal_offset" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.8" />

<EditText
    android:id="@+id/edittext_vertical_offset"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@null"
    android:inputType="numberSigned|numberDecimal"
    android:imeOptions="actionDone"
    android:textAlignment="viewEnd"
    android:textAppearance="@android:style/TextAppearance.Material.Small"
    app:layout_constraintBottom_toBottomOf="@+id/spinner_channel_select_vertical_offset"
    app:layout_constraintEnd_toStartOf="@+id/textview_vertical_offset_unit"
    app:layout_constraintTop_toTopOf="parent" />

<EditText
    android:id="@+id/edittext_horizontal_offset"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@null"
    android:inputType="numberSigned|numberDecimal"
    android:imeOptions="actionDone"
    android:textAlignment="viewEnd"
    android:textAppearance="@android:style/TextAppearance.Material.Small"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/textview_horizontal_offset_unit"
    app:layout_constraintTop_toTopOf="@+id/spinner_channel_select_horizontal_offset" />

</androidx.constraintlayout.widget.ConstraintLayout>

The parent activity of this fragment extends AppCompatActivity and implements View.OnClickListener. Does anyone know what could be consuming the ENTER event and causing this issue ?

Edit:-

I tested on two android devices. This works absolutely fine on the device running Android 8.1. However, on the device with Android 10, the above issue comes up. Also, this issue only comes up with the EditTexts placed inside Fragments. The ones placed directly in the activity layouts work absolutely fine.


Solution

  • Ok, so the issue was that I had set android:layout_width to wrap_content for both the EditTexts.

    When all the existing content was deleted, the EditTexts shrinked to 0 width, and hence the issue was arising. Still doesn't explain the fact as to why .(decimal point) was getting accepted as an input, but setting the android:layout_width to some fixed value instead of wrap_content worked out for me. Alternatively, setting the android:minWidth attribute can also help.