5

I googled this but couldn't find any solution please help. I have created a tabbar view having 3 tabs with Tabhost and set its title with TabSpec like this :

TabSpec tbhome = tabHost.newTabSpec("Home");
tbhome.setIndicator("Selected Topic");

I have 2 buttons in my another tab which is next to above tab. Now what I want is, if I click a button in this tab the title for this button must be set to the title of my home tab. That is in above code "Selected Topic " must be set to my button's title.

Thanks.

Vish
  • 341
  • 1
  • 4
  • 19
  • please put your some code so other can help you. – Shreyash Mahajan Aug 28 '13 at 09:09
  • I am setting the title of tab with above code and want to change it at runtime while clicking button from any of the other tab activities.!! I don't know much about tabs just setting them and their title etc. So I don't have any other code to put up!! – Vish Aug 31 '13 at 14:42
  • Why tabhost and not action-bar tabs? –  Sep 01 '13 at 06:08
  • @YekhezkelYovel I need solution for tabhost, please make me solve that. Thanks for your help. – Vish Sep 02 '13 at 07:05
  • @Vish, I didn't put that as an answer for this reason. You are using a view that is no longer recommended for use and may be deprecated in the future (perhaps already), so I wanted to make sure you are aware of that. This is what comments are for. –  Sep 02 '13 at 07:19
  • @YekhezkelYovel But if you know the answer then please help. For now I will use it and I will change it later. Thanks. – Vish Sep 03 '13 at 03:46
  • @Vish, if I knew the answer of course I would give it. As it happens, I never used TabHost because action-bar does a great job for me. Wish you success anyway, and if you'll ever need help with action-bar tabs or ABS (ActionBarSherloc - a great third-party variant that supports down to API level 6) I'll be more than happy to help. –  Sep 03 '13 at 08:17

4 Answers4

2

first you get the button and make it final then on click set its text

        final Button bX = (Button) findViewById(R.id.bXX);
        bX.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                    bX.setText("Selected Topic ");
            }
        });
Ahmed Ekri
  • 4,601
  • 3
  • 23
  • 42
  • No, Tabbar is created with only one activity and each tab has its own activity. Now in my second tab's activity I have 2 buttons for example. How should I change the name of my "FIRST TAB" to something else from second tab. – Vish Aug 25 '13 at 14:14
2

The code could be refined but this shoud do :

public void onClick(View button)
{
    RelativeLayout tbhome = tabHost.getTabWidget().getChildAt(0);
    for (int j = 0; j < tbhome.getChildCount(); j++)
    {
        if (tbhome.getChildAt(j) instanceof TextView)
        {
            ((TextView) tbhome.getChildAt(j)).setText(button.getText());
            break;
        }
    }
}
user1732313
  • 139
  • 1
  • 9
1

Create a static variable and the value as "Selected Topic" and in the button click event, make the property of the button widget as the value in the statis variable.Hope this is what you meant.

Mothy
  • 406
  • 1
  • 7
  • 19
  • No Monthy Tabbar is created with only one activity and each tab has its own activity. Now in my second tab's activity I have 2 buttons for example. How should I change the name of my "FIRST TAB" equal to name of button clicked.!! – Vish Aug 25 '13 at 13:33
  • @Vish : First you need to understand that your problem has nothing to do with activity or tab.If you want to change the name of the FIRST TAB to name of the button when clicked,try to create a string variable called "myVariable" and assign the text of button to that variable.In the click event,read the text of the button and make sure that the tbhome.setIndicator(myVariable); is set. – Mothy Sep 01 '13 at 08:54
  • But how the tabActivity (where for the first time the tabs are created) will be called again to set the tab's name!!! I have to setIndicator somehow in FIRSTTAB!! Thats the real problem.! – Vish Sep 02 '13 at 07:02
  • If you want,you can maintain the activty instance – Mothy Sep 02 '13 at 09:29
1

Untested, but I'd try sending the new text Bundle'd in an Intent from one activity to the other.

Perhaps catch it with Activity.onNewIntent()

David Sainty
  • 1,398
  • 12
  • 10