Bite code
Bite code

Reputation: 597371

Why is Android TabHost stealing focus from a TextView?

I have an application using the following layout :

alt text http://img15.imageshack.us/img15/238/screenshot003xbo.png

When the app starts, the focus is on the first TextView, but if you try to type any letter in it, the focus goes directly to the tabs. It seems that I am not the only one fighting with this issue, and maybe this is related to the following:

http://groups.google.com/group/android-developers/browse_thread/thread/435791bbd6c550a/8022183887f38f4f?lnk=gst&q=tabs+focus#8022183887f38f4f

Anyway, have you any idea of why that happens? And of course, any workaround would be appreciated.


I post the code below to avoid overloading the question :

The XML :

<?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">

    <LinearLayout 
          android:padding="5px"
          android:orientation="vertical" 
          android:id="@+id/task_edit_panel"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
           android:layout_weight="50" >

        <LinearLayout android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <TextView android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:text="@string/title" 
                android:textStyle="bold" />

            <EditText android:id="@+id/title" 
              android:layout_width="fill_parent"
                android:layout_height="fill_parent" />

        </LinearLayout>

        <TabHost android:id="@+id/edit_item_tab_host"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"> 

            <TabWidget android:layout_width="fill_parent"
                        android:layout_height="wrap_content" 
                        android:id="@android:id/tabs" /> 

            <FrameLayout
              android:id="@android:id/tabcontent"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:paddingTop="65px"> <!--  you need that if you don't want the tab content to overflow --> 

              <LinearLayout
                   android:id="@+id/edit_item_date_tab"
                   android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:padding="5px" > 

                    <TextView android:layout_width="wrap_content"
                            android:layout_height="wrap_content" 
                            android:text="date" 
                             android:textStyle="bold" />

                </LinearLayout>

                <LinearLayout
                   android:id="@+id/edit_item_geocontext_tab"
                   android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:padding="5px" > 

                <TextView android:layout_width="wrap_content"
                            android:layout_height="wrap_content" 
                            android:text="lieu" 
                             android:textStyle="bold" />

                </LinearLayout>

                <LinearLayout
                   android:id="@+id/edit_item_text_tab"
                   android:orientation="vertical"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                     android:padding="5px"> 

                <EditText android:id="@+id/details" 
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:scrollbars="vertical" />

                </LinearLayout>

            </FrameLayout>

        </TabHost>

    </LinearLayout>

    <!-- Bottom pannel with "add item" button -->
    <LinearLayout 
          android:padding="5px"
          android:orientation="horizontal" 
          android:layout_weight="1"
          android:id="@+id/task_edit_panel"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:background="#E7E7E7" >

          <!--  Let the height set to fill_parent until we find a better way for the layout  -->
          <Button android:id="@+id/item_edit_ok_button"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:text="@string/ok"
                style="?android:attr/buttonStyleSmall"
                android:layout_weight="1" />

           <Button android:id="@+id/item_edit_cancel_button"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:text="@string/cancel"
                style="?android:attr/buttonStyleSmall"
                android:layout_weight="1" />

    </LinearLayout>

</LinearLayout>

And the Java code :

    TabHost tab_host = (TabHost) findViewById(R.id.edit_item_tab_host);
    // don't forget this setup before adding tabs from a tabhost using a xml view or you'll get an nullpointer exception
    tab_host.setup(); 

    TabSpec ts1 = tab_host.newTabSpec("TAB_DATE");
    ts1.setIndicator(getString(R.string.when), getResources().getDrawable(R.drawable.ic_dialog_time));
    ts1.setContent(R.id.edit_item_date_tab);
    tab_host.addTab(ts1);

    TabSpec ts2 = tab_host.newTabSpec("TAB_GEO");
    ts2.setIndicator(getString(R.string.where),  getResources().getDrawable(R.drawable.ic_dialog_map));
    ts2.setContent(R.id.edit_item_geocontext_tab);
    tab_host.addTab(ts2);

    TabSpec ts3 = tab_host.newTabSpec("TAB_TEXT");
    ts3.setIndicator(getString(R.string.what),  getResources().getDrawable(R.drawable.ic_menu_edit));
    ts3.setContent(R.id.edit_item_text_tab);
    tab_host.addTab(ts3);

    tab_host.setCurrentTab(0);

Upvotes: 6

Views: 10352

Answers (5)

Hardik Bamania
Hardik Bamania

Reputation: 11

after adding all the specs add requestFocus method to your edittext in java file

Upvotes: 0

Oleksandr Bodashko
Oleksandr Bodashko

Reputation: 131

this example show how to fix this issue:

userNameEditText.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                userNameEditText.requestFocusFromTouch();
                return false;
            }
        });

Upvotes: 0

Jeremy Logan
Jeremy Logan

Reputation: 47514

If it isn't related to that other bug you pointed out (and it looks like it probably is) and if you're experiencing this on on of the G1s (and not the emulator) I suspect your problem is that the focus either isn't being frozen before the orientation change (when you fold out the keyboard and go into landscape mode) or unfrozen after the view is recreated. You could try Overriding the activity's onSaveInstanceState() and onRestoreInstanceState() methods to save/restore the focus.

Upvotes: 0

Haider
Haider

Reputation: 71

I was facing the same kind of issue with HoneyComb but found a very simple solution. So I thought it can be helpful for someone to share my experience. Everytime when a tab is clicked, the focus is on Edit Text and you can actually type with hard keyboard but soft keyboard never pops up. Even with below code, it didnt solve the issue:

input1.requestFocusFromTouch();
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(input1, InputMethodManager.SHOW_FORCED);

Solution: In the main class where you are handling Tabs, just write below Java code:

myTabHost.setOnTabChangedListener(new OnTabChangeListener(){    
    public void onTabChanged(String tabID) {    
        myTabHost.clearFocus(); 
    }   
}); 

Upvotes: 7

Bite code
Bite code

Reputation: 597371

Well, definitly a bug : http://code.google.com/p/android/issues/detail?id=2516.

Hope it will be fixed in the next release because this is a really anoying issues for complex apps with an advanced layout.

Upvotes: 8

Related Questions