Home > Back-end >  Cannot type in EditText when it's in a custom dialog
Cannot type in EditText when it's in a custom dialog

Time:03-23

I'm trying to get some numbers as user input from a custom dialog. I used an edittext for this purpose. when edittext is clicked the soft keyboard pops up normally, but when I'm trying to type numbers it won't show anything in edittext. only the decimal point . can be typed.

I've also tried this without limiting characters to numbers, and I've got a weird result, in this case, I can type all characters to edittext, but backspace only works on non-number characters, when I type numbers backspace won't remove them. this problem only happens in the dialog and not in activities or fragments. what am i doing wrong?

Also i have to mention that I'm using English keyboard.

custom dialog layout:

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

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_centerInParent="true"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_gravity="center"
    android:layout_width="@dimen/_180sdp"
    android:layout_height="@dimen/_130sdp"
    android:theme="@style/Theme.Trading.main_background"
    app:cardCornerRadius="@dimen/_15sdp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:theme="@style/Theme.Trading.main_background">

        <ImageView
            android:layout_width="@dimen/_15sdp"
            android:layout_height="@dimen/_15sdp"
            android:background="@drawable/ic_up_down"
            android:backgroundTint="@color/gray_1"
            android:alpha="0.5"
            android:layout_marginTop="@dimen/_17sdp"
            android:layout_marginStart="@dimen/_27sdp"/>

        <RelativeLayout
            android:background="@drawable/border_spinner"
            android:layout_marginTop="@dimen/_15sdp"
            android:layout_marginStart="@dimen/_55sdp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <Spinner
                android:theme="@style/Theme.Trading.main_textview"
                android:spinnerMode="dropdown"
                android:backgroundTint="@color/purple_1"
                android:id="@ id/alarm_spinner"
                android:layout_width="@dimen/_90sdp"
                android:layout_height="@dimen/_20sdp" />



        </RelativeLayout>


        <ImageView
            android:layout_width="@dimen/_20sdp"
            android:layout_height="@dimen/_20sdp"
            android:background="@drawable/ic_money"
            android:backgroundTint="@color/gray_1"
            android:alpha="0.5"
            android:layout_marginTop="@dimen/_53sdp"
            android:layout_marginStart="@dimen/_25sdp"/>

        <EditText
            android:imeOptions="actionNext"
            android:textColor="@color/gray_1"
            android:inputType="numberDecimal"
            android:textSize="@dimen/_10sdp"
            android:layout_marginTop="@dimen/_48sdp"
            android:layout_marginStart="@dimen/_55sdp"
            android:layout_width="@dimen/_90sdp"
            android:layout_height="wrap_content"/>

        <ImageView
            android:layout_width="@dimen/_20sdp"
            android:layout_height="@dimen/_20sdp"
            android:background="@drawable/ic_symbol"
            android:backgroundTint="@color/gray_1"
            android:alpha="0.5"
            android:layout_marginTop="@dimen/_89sdp"
            android:layout_marginStart="@dimen/_25sdp"/>





    </RelativeLayout>

</androidx.cardview.widget.CardView>

and i show this dialog ti user with code below:

    public void showDialog(){
        final Dialog dialog = new Dialog(this);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(false);
        dialog.setContentView(R.layout.dialog_add_alarm);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));


        dialog.setOnKeyListener(new Dialog.OnKeyListener() {

            @Override
            public boolean onKey(DialogInterface arg0, int keyCode,
                                 KeyEvent event) {
                // TODO Auto-generated method stub
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    dialog.dismiss();
                }
                return true;
            }
        });

        dialog.show();

    }

CodePudding user response:

You are returning true in your dialogue method. That is the cause of your problem. Use below code.

 public void showDialog(){
    final Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setCancelable(false);
    dialog.setContentView(R.layout.dialog_add_alarm);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
   

    dialog.setOnKeyListener(new Dialog.OnKeyListener() {

        @Override
        public boolean onKey(DialogInterface arg0, int keyCode,
                             KeyEvent event) {
            // TODO Auto-generated method stub
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                dialog.dismiss();
            }
            //just replace your return boolean to false. it will work
            return false;
        }
    });

    dialog.show();

}

CodePudding user response:

You need to declare the EditText and handle the onTextChanged event for it.

I am expecting to see something like this

customDialog_EditText = (EditText)dialog.findViewById(R.id.dialogedittext);

Here is a good example of a custom dialog with a working EditText: https://www.edumobile.org/android/custom-dialog-with-edittext-in-android-development/

maybe you need a TextWatcher as outlined here: https://stackoverflow.com/a/11134227/15119767

  • Related