Custom ActionBar sub tab not displaying correctly

When using tabs on the action bar, sometimes they appear as “folded” when the contents of the tab are too large to display. The problem arises when I use a custom view for the contents of the tab, it causes the selected tab not to appear in the drop-down list, and as soon as the tab is selected, the popup menu disappears and small empty tabs appear.

Here is a screenshot of the drop-down list before selecting an item: (note that the contents of the tab are not displayed, even if the tab is selected) before selecting item

In addition, after selecting an item, the tabs are no longer stacked, and the contents of the tabs are empty: after selecting item

Here is my code (note that I am using a custom tab view to demonstrate the problem)

public class ExampleActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final TextView selectedTabText = new TextView(this);
        setContentView(selectedTabText);

        ActionBar.TabListener listener = new ActionBar.TabListener() {
            @Override
            public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
                TextView customView = (TextView) tab.getCustomView();
                selectedTabText.setText(customView.getText());
            }

            @Override
            public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {

            }

            @Override
            public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {

            }
        };

        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        addTab(actionBar, listener, "Tab one with a very long name");
        addTab(actionBar, listener, "Tab two with a very long name");
        addTab(actionBar, listener, "Tab three with a very long name");
        addTab(actionBar, listener, "Tab four with a very long name");
    }

    private void addTab(ActionBar actionBar, ActionBar.TabListener listener, String text) {
        ActionBar.Tab tab = actionBar.newTab();
        TextView textView = new TextView(this);
        textView.setText(text);
        tab.setCustomView(textView);
        tab.setTabListener(listener);
        actionBar.addTab(tab);
    }
}
+4
source share
3 answers

This is a known bug and is already reported to the error tracker:

https://code.google.com/p/android/issues/detail?id=41392

To get around this, I disabled the collapse behavior of the action bar with an unpleasant hack. The following method should be called from the onStart method:

/**
 * SUPER hack to disable tab collapsing with smaller screens, which doesn't allow custom tab bar views to be used
 * https://code.google.com/p/android/issues/detail?id=41392
 */
private void disableCollapsibleTabs() {
    try {
        ActionBar actionBar = getActionBar();
        Field actionViewField = actionBar.getClass().getDeclaredField("mTabScrollView");
        actionViewField.setAccessible(true);
        Object mTabScrollView =  actionViewField.get(actionBar);

        Method setAllowCollapse = mTabScrollView.getClass().getDeclaredMethod("setAllowCollapse", boolean.class);
        setAllowCollapse.setAccessible(true);
        setAllowCollapse.invoke(mTabScrollView, false);

    } catch (Exception e) {
        Log.e("", "Error while disabling actionbar collapsible tabs", e);
    }
}
+3
source

Edit: Finished with

<android.support.design.widget.TabLayout
    android:id="@+id/tabLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/tint" />`

not obsolete ActionBar tabs. This will not cause the tabs to change when the orientation changes, but it looks like the google and facebook apps do the same (I think).

: , , . , → onConfigurationChanged , , , . , , , ( , , , - ):

@Override
public void onConfigurationChanged(Configuration newConfig) {

    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
   super.onConfigurationChanged(newConfig);

}

. , .

@Override
public void onConfigurationChanged(Configuration newConfig) {
    final ActionBar actionBar = getActivity()).getSupportActionBar();
    actionBar.invalidateOptionsMenu();
}
+1

reset onConfigurationChanged. . , .

@Override
public void onConfigurationChanged(Configuration newConfig) {

    try {
        Thread.sleep(100);
        actionBar.getTabAt(0).setCustomView(/*your custom view*/);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    super.onConfigurationChanged(newConfig);


}
0

Source: https://habr.com/ru/post/1523970/


All Articles