Home » Android » android – requestFocus skipping next EditText

android – requestFocus skipping next EditText

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m having some problems with the control focusing. My interface definition below has:

source (RadioGroup/optional)
destination (EditText)
quantity (EditText)
transfer (Button)

I’m changing the visibility of the “source” into my code. When the times I don’t display it, focus is automatically goes to “quantity” which I would expect that to be “destination” instead. I don’t know what I’m missing. I suspect it could be an Android bug, I don’t know. Is there anybody know how to solve this?

Thanks

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <RadioGroup android:id="@+id/source"
     android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="vertical"
   android:visibility="gone">
   <RadioButton android:id="@+id/default"
    android:checked="false"
    android:text="@string/default"/>
 </RadioGroup>

 <TableLayout
  android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:stretchColumns="1">
     <TableRow>
   <TextView 
    android:text="@string/destination"
    android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:textSize="16sp"
       android:textColor="#ffffff"
       android:paddingRight="10sp"
       android:layout_weight="1"/>

      <EditText android:id="@+id/destination"
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:singleLine="true"><requestFocus/></EditText>
  </TableRow>

  <TableRow>
   <TextView 
    android:text="@string/quantity"
    android:layout_width="wrap_content" 
       android:layout_height="wrap_content"
       android:textSize="16sp"
       android:textColor="#ffffff"
       android:paddingRight="10sp"
       android:layout_weight="1"/>

      <EditText android:id="@+id/quantity"
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:singleLine="true"/>
  </TableRow>
 </TableLayout>

 <Button android:id="@+id/transfer"
  android:layout_width="fill_parent" 
     android:layout_height="wrap_content"
     android:text="@string/transfer"/>
</LinearLayout>
How to&Answers:

It appears to be a known issue. The following post on google code for the Android project describes the same problem.

Issue 2705: Setting requestFocus on EditText prevents soft keyboard from opening

You could try setting focus in your code instead of in the layout.

Answer:

I faced same issue ,
Try this code that worked for me.

             editText.post(new Runnable() {
                 public void run() {
                       editText.requestFocus(); 
                                   } 
                                           }); 

Answer:

From online documentation:

A view will not actually take focus if it is not focusable (isFocusable() returns false), or if it is focusable and it is not focusable in touch mode (isFocusableInTouchMode()) while the device is in touch mode.

Answer:

Use EditText‘s requestFocus() method. 🙂