I have a TabWidget
nested within a HorizontalScrollView
. I am programmatically setting the active/current tab (which I have no problem doing). My problem is, the TabWidget
is not scrolling to show thenewly active tab.
Example - I have 9 tabs, only 3 are shown at any given time (so think 3 'pages' of tabs). I am on tab 1 (page 1). I programmatically set the current tab to tab 8 (page 3). The tab content switches properly (ie, I now see the contents of tab 8), however, the tabs up at the top still show tabs 1, 2, and 3. If I manually scroll over to the right, I will see tab 8 (properly highlighted). I want to automatically scroll the TabWidget
so that the currently active tab is shown.
Any ideas?
EDIT example source:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
String tag;
/* logic to populate tag from intent*/
/* reason for this is because I know the tag of the tab I need,
not the index num. of tab */
getTabHost().setCurrentTabByTag(tag);
int i = getTabHost().getCurrentTab();
getTabWidget().setCurrentTab(i);
getTabWidget().focusCurrentTab(i);
}
It turns out that the
TabWidget
views had focusableInTouchMode set to false. As per View.java documentation:To correct the problem I iterated over the
TabWidget
views and set them focusable in touch mode:set to TabHost next OnTabChangeListener:
You must both focusCurrentTab(int i) and setCurrentTab(int i). focusCurrentTab(i) will focus the UI of the TabWidget to the ith tab (what you want), and, as you already know, setCurrentTab(i) will focus the actual content of the ith tab in the main View.
For reference: http://developer.android.com/reference/android/widget/TabWidget.html#focusCurrentTab(int)
EDIT:
My answer was incorrect. This is the correct one (that the the poster found out themself):
It turns out that the TabWidget views had focusableInTouchMode set to false. As per View.java documentation:
A view will not actually take focus if it is not focusable ({@link #isFocusable} returns false), or if it is focusable and it is not focusable in touch mode ({@link #isFocusableInTouchMode}) while the device is in touch mode. To correct the problem I iterated over the TabWidget views and set them focusable in touch mode: