I have a tabHost and a searchView on an activity which extends
TabActivity.
I found that the TabHost steals focus from the SearchView when I tap
on the SearchView. So it disables typing by an external hardware
keyboard.
It also occurs when it is developed by Fragment instead of TabHost.

My original source code and XMLs are like this :
===========================================
1. SearchViewFocusTestActivity.java
package com.infraware.tabtest;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.SearchView;
import android.widget.TabHost;

public class SearchViewFocusTestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LayoutInflater inflater = this.getLayoutInflater();
        inflater.inflate(R.layout.layout_tabhost,
(LinearLayout)findViewById(R.id.tabhost_layout), true);

        Resources res = getResources();
        final TabHost tabHost =
(TabHost)findViewById(android.R.id.tabhost);
        tabHost.setup();
        TabHost.TabSpec spec;

        View indicator =
inflater.inflate(R.layout.frame_tab_contentview,
tabHost.getTabContentView(), true);

        spec = tabHost.newTabSpec("A").setIndicator("tab1",
 
res.getDrawable( R.drawable.ic_launcher) ).setContent(R.id.tab1);

        tabHost.addTab(spec);

       spec = tabHost.newTabSpec("B").setIndicator("tab2",
 
res.getDrawable( R.drawable.ic_launcher) ).setContent(R.id.tab2);

       tabHost.addTab(spec);

       spec = tabHost.newTabSpec("C").setIndicator("tab3",
 
res.getDrawable( R.drawable.ic_launcher) ).setContent(R.id.tab3);

       tabHost.addTab(spec);
    }

    public boolean onCreateOptionsMenu(Menu menu) {
                getMenuInflater().inflate(R.menu.mainmenu_home, menu);
                return super.onCreateOptionsMenu(menu);
        }
}

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <LinearLayout
        android:id="@+id/tabhost_layout"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

        </LinearLayout>

</LinearLayout>

3. mainmenu_home.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
  xmlns:android="http://schemas.android.com/apk/res/android";>
        <item android:id="@+id/menu_home_dummy_search"
                  android:showAsAction="always"
        >
        </item>
    <item android:id="@+id/menu_home_search"
          android:showAsAction="always"
          android:iconifiedByDefault="false"
 
android:actionViewClass="android.widget.SearchView"
    >
    </item>
</menu>

4. layout_tabhost.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
        <TabHost
                android:id="@android:id/tabhost"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
        >
                <LinearLayout
                        android:orientation="vertical"
                        android:gravity="center_horizontal"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">
                        <TabWidget
                                android:id="@android:id/tabs"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content">
                        </TabWidget>
                        <FrameLayout
                                android:id="@android:id/tabcontent"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content">

                        </FrameLayout>
                </LinearLayout>
        </TabHost>
</LinearLayout>

5. frame_tab_contentview.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/
android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
        <LinearLayout
                android:id="@+id/tab1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="horizontal"
                android:background="#ff0000">
        </LinearLayout>
        <LinearLayout
                android:id="@+id/tab2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="horizontal"
                android:background="#00ff00">
        </LinearLayout>
        <LinearLayout
                android:id="@+id/tab3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="horizontal"
                android:background="#0000ff">
        </LinearLayout>
</FrameLayout>

===========================================

I have googled a tip and modified onCreateOptionsMenu() on the source
like this :
    public boolean onCreateOptionsMenu(Menu menu) {
                getMenuInflater().inflate(R.menu.mainmenu_home, menu);
                final SearchView oSearchView =
(SearchView)menu.findItem(R.id.menu_home_search).getActionView();
                oSearchView.setOnTouchListener(new View.OnTouchListener() {

                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                                // TODO Auto-generated method stub
                                oSearchView.requestFocusFromTouch();
                                return true;
                        }
                });
                return super.onCreateOptionsMenu(menu);
        }

But it gets the focus back only when I touch on the Search icon of the
expanded SearchView. In other cases, it happens same. So I have tried
to catch the only textfield of the expanded SearchView to control it
such as setting an OnTouchListener. But I could not.
Please help to solve this problem.
Thank you in advance.

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to