24

hi can you tell me how to disable a tab in the UI of android code.. (eclair code)

garima
  • 5,154
  • 11
  • 46
  • 77
  • 1
    did you check the android documentation ? http://developer.android.com/reference/android/widget/TabWidget.html#setEnabled(boolean) – Reno Dec 21 '10 at 07:51

2 Answers2

47

If you mean to disable one tab button on TabWidget, then try this code:

// tabHost = ... (get TabHost)
tabHost.getTabWidget().getChildTabViewAt(your_index).setEnabled(false);

If you want to disable tab widget in overall, then:

// tabWidget = ... (get TabWidget)    
tabWidget.setEnabled(false);

Read SDK Help for references:

Sergey Glotov
  • 20,200
  • 11
  • 84
  • 98
  • 1
    setEnabled(false) doesn't seem to prevent the tab from being clicked on and selected. It does though trigger the disabled style on it. – cabez0n Jul 05 '11 at 19:17
  • why I have to use tabHost.getTabWidget().getChildTabViewAt(index).setClickable(false) ? – micahli123 Apr 21 '15 at 02:56
  • @micahli123 Who talks about `setClickable()`? Can you explain your question? I don't understand it. Why you have to use `setClickable()`, or `tabHost.getTabWidget().getChildTabViewAt(index)`, or anything else? – Sergey Glotov Apr 21 '15 at 08:19
  • @Sergey Glotov I want to disable the tab button. But I tried the method you talked. It doesn't not work for me. Then I replace setEnable to setClickable. Then it works. – micahli123 Apr 22 '15 at 09:34
1

Extend TabHost and override methods:

@Override
public void setCurrentTab(int currentTab) {
    if (currentTab != 2)  // position of the tab that should not get selected
        super.setCurrentTab(currentTab);
    else
        // in my case I want to trigger something here but I don't want the button to get selected
}

@Override
public void setCurrentTabByTag(String tag) {
    if (!"\"plus_tab\"".equals(tag))  // tag of the tab that should not get selected
        super.setCurrentTabByTag(tag);
    else
        // in my case I want to trigger something here but I don't want the button to get selected
}
Emil Banca
  • 36
  • 1
  • 2