1

Seriously, nobody knows how to achieve this?. Is this even possible?. Maybe a yes or no answer could help me out!

I have a form that looks like this:

enter image description here

The rectangles with a B on it, means that are buttons. The other fields are Edit Texts and Text Views.

I want to be able to press the Tabulator keyboard and focus in this direction:

  • 1st Name Edit text
  • 2nd Company Edit text
  • 3rd Button
  • 4th Employee Edit text
  • 5th Button
  • 6th Date
  • 7th Button
  • 8th Time
  • 9th Button
  • 10th Description Edit text
  • 11th Mark as done check box
  • 12th save
  • 13th clean

This is my Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@style/ContenedorPrincipal"
tools:context=".ClientsActivity" >

<TextView
    android:id="@+id/textViewTask"
    style="@style/FTitle"
    android:text="@string/new_task" />


<!--____________________ Section: form, field: name task______________________________-->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:padding="3dp"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/textViewTaskName"
        style="@style/FForm"
        android:text="@string/name" />

    <EditText
        android:id="@+id/editTextTaskName"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:inputType="textCapSentences" >
    </EditText>

</LinearLayout>

<!--______________________________ Section: form, fields: company,employee____________________-->
<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:padding="3dip"
    android:stretchColumns="1,4" >

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textViewCompany"
            style="@style/FForm"
            android:text="@string/company" />

        <AutoCompleteTextView 
            android:id="@+id/autocompleteCompany" />

        <ImageButton
            android:id="@+id/imageButtonWatchCompany"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_menu_view" 
            android:contentDescription="@string/see_company"/>

        <TextView
            android:id="@+id/textViewEmployee"
            style="@style/FForm"
            android:text="@string/employee" />

        <AutoCompleteTextView   
            android:id="@+id/autocompleteEmployee" />


        <ImageButton
            android:id="@+id/imageButtonWatchEmployee"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_menu_view" 
            android:contentDescription="@string/see_employee"/>
    </TableRow>

    <!--____________________ Section: form, fields: date, time______________________________-->
    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textViewDate"
            style="@style/FForm"
            android:text="@string/date" />

        <Button
            android:id="@+id/buttonDate" >
        </Button>
        <TextView />

        <TextView
            android:id="@+id/textViewTime"
            style="@style/FForm"
            android:text="@string/time" />

        <Button
            android:id="@+id/buttonTime">
        </Button>

    </TableRow>
</TableLayout>
<!--____________________ Section: form, field description ______________________________-->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:orientation="horizontal">

    <TextView
        android:id="@+id/textViewDescription"
        style="@style/FForm"
        android:text="@string/description" />

    <EditText
        android:id="@+id/editTextDescription"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:lines="5"
        android:gravity="top|left"
        android:scrollbars="vertical"
        android:inputType="textMultiLine|textCapSentences"/>
</LinearLayout>

<CheckBox 
    android:id="@+id/checkBoxDone"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/mark_as_done"/>

<!--____________________ Section: buttons: save and clean______________________________-->
 <TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:padding="3dip" 
    android:stretchColumns="0,1">

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:ignore="UselessParent"  >

        <Button
            android:id="@+id/buttonSave"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/save" />

       <Button
           android:id="@+id/buttonClean"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@string/clean" />

    </TableRow>
</TableLayout>

Is there anyway to achive this?. Any help will be aprecciated.

SOLUTION

In the layout make sure to use android:imeOptions="actionNext":

<AutoCompleteTextView 
            android:id="@+id/autocompleteCompany"
            android:imeOptions="actionNext" />

And programmatically in the onCreateView method, call to a personal method:

private void setUpFocus(View view){
    etName.setNextFocusDownId(R.id.autocompleteCompany);
    etCompany.setNextFocusDownId(R.id.imageButtonWatchCompany);
    ImageButton button = (ImageButton) view.findViewById(R.id.imageButtonWatchCompany);
    button.setFocusableInTouchMode(true);

}

With this part of the code, using tab key, I can go from name, then company, then first button, then employee.

kiduxa
  • 3,339
  • 11
  • 37
  • 52

2 Answers2

1

You can override the onKeyDown() method of your activity to explicitly handle the tab key press, switching focus in whatever order you desire. Keep in mind, your keycode may be different if you are using imeOptions, in which case you can check for those particular actions (ie next, done, etc.).

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keycode == KeyEvent.KEYCODE_TAB) {
        View currentFocus = getCurrentFocus();

        switch (currentFocus.getId()) {
        case R.id.etName:
            etCompany.requestFocus();
            return true;
        case R.id.etCompany:
            btn3.requestFocus();
            return true;
            ...
        }
    }

    return super.onKeyDown(keyCode, event);
}

Also, make sure your buttons are focusable http://developer.android.com/reference/android/view/View.html#attr_android:focusable and focusable in touch mode.

android:focusable="true"
android:focusableInTouchMode="true"
invertigo
  • 6,336
  • 5
  • 39
  • 64
  • I'm sure this is going to work, but I have several forms and I'm using fragments. So this method I can override it from my main activity that calls the other fragments/forms . So, the code will be very ugly if I try this :s. What do you think about mattgmg1990 approach?. I guess this is more elegant, but I cant get my button focused. Or maybe I'm wrong about your approach?. Thanks for the help :). – kiduxa Jun 18 '13 at 21:24
0

In your xml layout, you should be able to just set the android:imeOptions="actionNext" tag for each of these elements. That should make the soft keyboard display a next button that will move the focus to the next box or button.

You'll probably want to set your save button to android:imeOptions="actionDone" so that the user can complete the form by saving.

EDIT: This stackoverflow answer described how to set focus to a button and helped OP solve the problem: https://stackoverflow.com/a/3234884/815679

Community
  • 1
  • 1
mattgmg1990
  • 5,576
  • 4
  • 21
  • 26
  • Finally someone answers. Thanks. I did what you mentioned but it doesn't work for buttons. Actually I'm getting a warning in eclipse that says "intended for editable text widgets". And the employee edit text is never focus because the focus goes up to down, no left to right. Any other clue?. – kiduxa Jun 18 '13 at 19:17
  • Ah, okay, I didn't realize that it couldn't be used on buttons. What about `android:nextFocusDown`or `android:nextFocusRight`? I haven't used these before but the documentation indicates that they might be useful for shifting the focus in your intended direction when the user presses tab or next. – mattgmg1990 Jun 18 '13 at 20:16
  • Ok, using android:nextFocusRight and android:imeOptions="actionNext" I have part of the focus desired. I mean, I can go from company to employee but I cant get my button focused. Im trying this: etCompany.setNextFocusRightId(R.id.imageButton1); and in my layout: – kiduxa Jun 18 '13 at 21:18
  • Try changing `etCompany.setNextFocusRightId(R.id.imageButton1);` to `etCompany.setNextFocusDownId(R.id.imageButton1);`. I think that the "next" button will be triggering focusDown and not focusRight. – mattgmg1990 Jun 18 '13 at 23:29
  • That's a bummer. Yes, I believe so. Check out this answer where someone sets focus to a button programmtically: http://stackoverflow.com/a/3234884/815679 – mattgmg1990 Jun 19 '13 at 03:38
  • Awesome! I'm very glad that helped! – mattgmg1990 Jun 19 '13 at 19:45