Search code examples
androidandroid-tabhostrenameandroid-tabs

how to rename tab android use tabhost and long click


I have designed an app in that I am using Tabhost and I have added two tabs. I want rename tab Device1 when "long click" this tab. I try some way but it is not working. I have implemented following:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabHost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

<HorizontalScrollView
    android:id="@+id/horScrollView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true"
    android:scrollbars="none" >

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

<FrameLayout
    android:id="@android:id/tabcontent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <LinearLayout
        android:id="@+id/Device1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:paddingTop="80px" >

    </LinearLayout>

    <LinearLayout
        android:id="@+id/Device2"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:paddingTop="80px" >

    </LinearLayout>
</FrameLayout>

MainActivity.java

  package com.example.renametab;
  import android.os.Bundle;
  import android.app.Activity;
  import android.view.Menu;
  import android.view.View;
  import android.view.View.OnLongClickListener;
  import android.widget.TabHost;
  import android.widget.TextView;
  import android.widget.TabHost.TabSpec;

  public class MainActivity extends Activity {
    public static TabHost tabHost;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabHost = (TabHost) findViewById(R.id.tabHost);
        tabHost.setup();

    TabSpec tabDevice1 = tabHost.newTabSpec("Device1");
    tabDevice1.setContent(R.id.Device1);
    tabDevice1.setIndicator("Device1");

    TabSpec tabDevice2 = tabHost.newTabSpec("Device2");
    tabDevice2.setContent(R.id.Device2);
    tabDevice2.setIndicator("Device2");
    tabHost.addTab(tabDevice1);
    tabHost.addTab(tabDevice2);
    tabHost.getTabWidget().getChildAt(0).setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            //How to rename tab????
            //I try this way but it is false
            TextView tv = (TextView) tabHost.getTabWidget().getChildAt(0).findViewById(R.id.Device1);
            tv.setText("New Name Tab");
            return true;
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
}

Solution

  • On close inspection using the Android Studio debugger, each tab contained in TabWidget is a LinearLayout with 2 children:

    1. An ImageView with the id android.R.id.icon;
    2. A TextView with the id android.R.id.title.

    Knowing this, we can find each tab's title TextView, set an OnLongClickListener on each tab, and then modify the title using TextView.setText().

    Here's my Fragment's onCreateView() method, defining 3 tabs, each implementing this functionality:

            @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
    
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    
            mTabHost = (TabHost) rootView.findViewById(android.R.id.tabhost);
            mTabHost.setup();
            mTabHost.addTab(mTabHost.newTabSpec(TAB_1).setIndicator("Tab 1").setContent(R.id.tab_1));
            mTabHost.addTab(mTabHost.newTabSpec(TAB_2).setIndicator("Tab 2").setContent(R.id.tab_2));
            mTabHost.addTab(mTabHost.newTabSpec(TAB_3).setIndicator("Tab 3").setContent(R.id.tab_3));
    
            LinearLayout tabOne = (LinearLayout) mTabHost.getTabWidget().getChildTabViewAt(0);
            final TextView tabOneTitle = (TextView) tabOne.findViewById(android.R.id.title);
            LinearLayout tabTwo = (LinearLayout) mTabHost.getTabWidget().getChildTabViewAt(1);
            final TextView tabTwoTitle = (TextView) tabTwo.findViewById(android.R.id.title);
            LinearLayout tabThree = (LinearLayout) mTabHost.getTabWidget().getChildTabViewAt(2);
            final TextView tabThreeTitle = (TextView) tabThree.findViewById(android.R.id.title);
    
            tabOne.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    tabOneTitle.setText("New Tab 1");
                    return true;
                }
            });
    
            tabTwo.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    tabTwoTitle.setText("New Tab 2");
                    return true;
                }
            });
    
            tabThree.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    tabThreeTitle.setText("New Tab 3");
                    return true;
                }
            });
    
            return rootView;
        }
    

    Here are before and after screenshots documenting the results:

    Before the long press on Tab 1

    After the long press on Tab 1