1

I'm new to android, trying to build a simple app involving some simple data input and management. When I follow the guide here, which ask for user input through an alert dialog. Everything works fine except that, after the AlertDialog appears, the EditText field does not have an input cursor displayed, and it won't display/update the text I typed. However, I can obtain the text string I typed in correctly after I pressed the 'OK' button in the alert dialog. Here is the xml file and code involving that dialog:

xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp" >

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Type Your Message : "
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/grade_in"
    android:inputType="text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

</EditText>

code:

Button grade_in = button;

  grade_in.setTag(tag);
  grade_in.setOnClickListener(new OnClickListener(){

        public void onClick(View btn) {

            LayoutInflater inflater = LayoutInflater.from(activity.getApplicationContext());
            View prompt_grade_in = inflater.inflate(R.layout.grade_in, null);

            final EditText input_field = (EditText)prompt_grade_in.findViewById(R.id.grade_in);

            AlertDialog.Builder alertBuilder = new AlertDialog.Builder(activity);

            alertBuilder.setView(prompt_grade_in);

            alertBuilder.setCancelable(true)
                        .setPositiveButton("OK", 
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        // TODO Auto-generated method stub
                                        Log.v(null, input_field.getText().toString());

                                    }
                                })

                        .setNegativeButton("Cancel", 
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.cancel();

                                    }
                        })

                        .setMessage("Grade for the course:");

            AlertDialog alert = alertBuilder.create();
            alert.show();

        }
  });

I tried adding a TextWatcher but it does not help. Is the version the source of this problem? Because I think I pretty much followed the guide except that I've made some minor changes. Thanks for helping!

p.s. the page containing the button invoke the input dialog is a fragment.

UPDATE: The fragment containing the button consists of an ExpandableListView, which is implemented according to this guide (part 15). And the above code implementing the alert dialog is in a custom adapter class, which create the group item in the list, inside the following function:

public View getGroupView(int groupPosition, boolean isExpanded, View convertView, final ViewGroup parent) {

if (convertView == null) {
  convertView = inflater.inflate(R.layout.listrow_group, null);
}

CheckedTextView checkedTextView = (CheckedTextView) convertView.findViewById(R.id.textView1);

course courses = (course) getGroup(groupPosition);

((CheckedTextView) checkedTextView).setText(courses.code + " - " + courses.title);
((CheckedTextView) checkedTextView).setChecked(isExpanded);

String tag = courses.code + " - " + courses.title;

CheckBox checkbox = (CheckBox) convertView.findViewById(R.id.checkbox_course);
checkbox.setChecked(sharedPref.getBoolean(tag, false));
setBoxListener(checkbox, tag);

Button button = (Button) convertView.findViewById(R.id.grade_button);
setBtnListener(button, tag);

return convertView;
}
Petro
  • 3,484
  • 3
  • 32
  • 59
peakingcube
  • 1,388
  • 15
  • 32
  • Try http://stackoverflow.com/a/12998922/28557 – Vinayak Bevinakatti May 20 '14 at 12:28
  • Just tried, but problem persists. I think the creator in that topic is dealing with some problem with input method/keyboard? – peakingcube May 20 '14 at 12:49
  • Try `input_field.requestFocus();` after `alert.show();` – Vinayak Bevinakatti May 20 '14 at 13:01
  • Identified and fixed by my friend, turns out that it is a minor mistake in: LayoutInflater inflater = LayoutInflater.from(activity.getApplicationContext()); Instead it should be: LayoutInflater inflater = LayoutInflater.from(activity); And I thought I am using activity right from the beginning. :( – peakingcube May 20 '14 at 13:10

6 Answers6

1

try to set android:textCursorDrawable="@null" to your EditText for input Cursor Display

M D
  • 47,665
  • 9
  • 93
  • 114
1

Check your Import first.

if you are using androidx. use:

androidx.appcompat.app.AlertDialog

instead of

import android.app.AlertDialog;

check input type

0

replace your EditText code in xml with the following code. The following code contains tag, which gets the focus in the EditText. Hope this will solve your issue.

<EditText
    android:id="@+id/grade_in"
    android:inputType="text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

        <requestFocus />

    </EditText>
Anu
  • 1,884
  • 14
  • 30
0

Call the soft keyboard before you create the AlertDialog.

alert.getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Rajiv Manivannan
  • 504
  • 7
  • 15
0

Identified and fixed by my friend, turns out that it is a minor mistake in:

LayoutInflater inflater = LayoutInflater.from(activity.getApplicationContext());

Instead it should be:

LayoutInflater inflater = LayoutInflater.from(activity);

And I thought I am using activity right from the beginning. :(

peakingcube
  • 1,388
  • 15
  • 32
0

I had this problem too... In my case : I Used custom dialog, extends DialogFragment / AppCompatDialogFragment and EdidText inside my dialog_custom.xml;

I add following code inside onStart in my dialog class and worked fine the EditText:

override fun onStart() {
     super.onStart()
     dialog!!.window!!.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
}
ultra.deep
  • 1,699
  • 1
  • 19
  • 23