Mr_Risk
Mr_Risk

Reputation: 41

Why does the keyboard change and editText box lose focus in android studio project?

Ok, first time poster. I teach in High School, first year on job no mentor to help, so if my code is garbage, be gentle.

Problem: : This program is to keep inventory for poptarts being sold. In the XML I have a linear layout, listview, and finally a linear layout. The listview goes through, uses a custom adapter to show the current pop-tarts and has an edit text to update the count. The last linear layout has edit texts to add a new poptart to the arraylist. After I added this code, whenever I select the edit text in the listview (which I forced to be an input_type:"number") the number pad pops up and then disappears to be replaced by the normal qwerty keyboard and the edit text box loses focus. If I jab the edit text box 3-4 times quickly it will keep the number keypad up and maintain focus. I just can't figure out why the focus and keyboard are being changed.

<LinearLayout
    ...
<TextView
   ...
<TextView
    ...
<TextView
   ...
<TextView
   ...
<TextView
    ...
</LinearLayout>

<ListView
    android:layout_below="@+id/flavor"

    android:id="@+id/simpleListView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="#000"
    android:dividerHeight="2dp"
    android:layout_weight="1"
    android:layout_gravity="top" />
<LinearLayout
    android:layout_width="match_parent"
    android:orientation="horizontal"
    android:layout_height="0dp"
    android:layout_weight="1">
    <EditText
        android:id="@+id/newFlavor"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="Flavor"
        android:textSize="10pt"
         />
    <CheckBox
        android:id="@+id/isSeasonal"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:checked="false"
         />
    <EditText
        android:id="@+id/newCurrent"
        android:layout_width="0dp"
        android:maxLength="3"
        android:layout_height="wrap_content"
        android:hint="Current"
        android:textSize="10pt"
         />
    <EditText
        android:id="@+id/newMax"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:maxLength="3"
        android:hint="Max"
        android:textSize="10pt"
         />
    <Button
        android:id="@+id/addNewFlavor"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textSize="10pt"
        android:text="Add"
        android:layout_weight="1" />

</LinearLayout>

Inside of list_view_inventory.xml:

...
<EditText
    android:id="@+id/edit_text"
    android:maxLength="3"
    android:paddingLeft="25dp"
    android:layout_width="75dp"
    android:layout_height="wrap_content"
    android:paddingRight="30dp"
    android:layout_weight="1" />
 ...

Finally I'll post MyAdapter:

public class MyAdapter extends ArrayAdapter<PopTart> {
ArrayList<PopTart> popTartList = new ArrayList<>();
Button buttonOne;
CheckBox seasonal;
public MyAdapter(Context context, int textViewResourceId, ArrayList<PopTart> tarts){
    super(context, textViewResourceId, tarts);
    popTartList = tarts;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View v = convertView;
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = inflater.inflate(R.layout.list_view_inventory, null);

    TextView nameView = (TextView) v.findViewById(R.id.name);
    final TextView countView = (TextView) v.findViewById(R.id.count);
    final EditText countUpdate = (EditText) v.findViewById(R.id.edit_text);
    CheckBox seasonal = (CheckBox) v.findViewById(R.id.seasonal);

    //Tried to force the input type again here programmatically
    countUpdate.setInputType(InputType.TYPE_CLASS_NUMBER);

    nameView.setText(popTartList.get(position).getName());
    countView.setText(String.valueOf(popTartList.get(position).getCount()));
    seasonal.setChecked(popTartList.get(position).getmSeasonal());
    seasonal.setEnabled(false);

    buttonOne = (Button) v.findViewById(R.id.increment);
    buttonOne.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            if (countUpdate.getText().toString().trim().length() == 0)
             {
            //Making sure the EditText isn't empty
            }
            else {

            String temp2 = countUpdate.getText().toString();
            PopTart temp = popTartList.get(position);
            temp.setCount(Integer.valueOf(temp2));
            popTartList.set(position, temp);
            countView.setText(String.valueOf(popTartList.get(position).getCount()));
            countUpdate.setText(""); }


        }
    });

    return v;
}

}

Thanks for all you do, Stack Overflow Community! You have taught me so much already!

Upvotes: 4

Views: 2752

Answers (2)

NyoSeint Lay
NyoSeint Lay

Reputation: 156

In Android 7, I did not see this problem but I also having this problem recently at older version of Android.
I solved this problem by adding the following three.

1: Set android:descendantFocusability="afterDescendants" on the listView.

FOCUS_AFTER_DESCENDANTS
added in API level 1
int FOCUS_AFTER_DESCENDANTS
This view will get focus only if none of its descendants want it.

2: Add the following to your EditText
android:focusable="true"
android:focusableInTouchMode="true"

3: Add android:windowSoftInputMode="adjustResize" to activity of your manifest file. So your list can be scrollable even when keyboard is appeared.

PS: If someone facing the same problem, I hope this can help to them.

Upvotes: 3

Nakul
Nakul

Reputation: 1329

I think your list_view_inventory.xml should look like as follows, just added android:inputType="number" in your EditText

...
<EditText
    android:id="@+id/edit_text"
    android:maxLength="3"
    android:paddingLeft="25dp"
    android:layout_width="75dp"
    android:inputType="number"
    android:layout_height="wrap_content"
    android:paddingRight="30dp"
    android:layout_weight="1" >
    <requestFocus />
    </EditText>
 ...

Or request focus programmatically.

edittext.requestFocus();

you can even remove code which sets input-type to EditText in getView() method of your adapter.

Quoting Android docs

whenever the ListView needs to display a particular row of data, it requests the associated adapter to provide the view corresponding to that the data at that position through getView() method. This may occur whenever the view needs to be updated on screen (eg. during creation/scroll etc.).

so inputType= "number" is specified whenever getView() is called, and then it falls back to default inputtype and popsup qwerty keyboard.

I think it will solve your problem.

EDIT

So you can use following

android:focusableInTouchMode="true"

To get desired result, remove previous <requestFocus/>

Upvotes: 0

Related Questions