8

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>
ElefantPhace
  • 3,806
  • 3
  • 20
  • 36
pocoa
  • 4,197
  • 9
  • 37
  • 45
  • I'm testing this on Android 2.2 and cannot reproduce, the focus is on Destination in the beginning. If you continue to have problems I recommend to add `EditText e = (EditText) findViewById(R.id.destination); e.requestFocus();` in your `onResume` method. – Sebastian Roth Nov 18 '10 at 05:33
  • Thanks for your response. Yeah, but I'm testing on Android 2.1. requestFocus() could not solve the problem. – pocoa Nov 18 '10 at 06:04
  • See my answer in topic: http://stackoverflow.com/a/13194129/348192 – iBog Nov 02 '12 at 11:18

4 Answers4

8

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.

Craig McMahon
  • 1,550
  • 1
  • 14
  • 36
Thomas
  • 3,603
  • 1
  • 18
  • 23
  • Thanks for the link. Yeah, I tried using destination.requestFocus() but it didn't work. – pocoa Nov 18 '10 at 05:41
  • 7
    Thank you. this.post(new Runnable() { public void run() { text.requestFocus(); } }); solved the problem. – pocoa Nov 20 '10 at 13:52
3

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

             editText.post(new Runnable() {
                 public void run() {
                       editText.requestFocus(); 
                                   } 
                                           }); 
Parag Chauhan
  • 35,760
  • 13
  • 86
  • 95
2

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.

StenaviN
  • 3,687
  • 24
  • 34
1

Use EditText's requestFocus() method. :)

chown
  • 51,908
  • 16
  • 134
  • 170
Shamitha
  • 1,407
  • 1
  • 10
  • 9