Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does action_bar_embed_tabs exactly work in Android?

I have tabs in an actionbar. On large screens the tabs are embed to the actionbar but on small screens the are not. I want to control the tabs manual so i can separate the tabs from the actionbar. I tried to set abs__action_bar_embed_tabs but that didn't work

<resources>
    <bool name="abs__action_bar_embed_tabs">false</bool>
</resources>
like image 957
Rickyrick Avatar asked Dec 05 '22 13:12

Rickyrick


1 Answers

I know this is an old post, however I would like to add a solution using action_bar_embed_tabs for future readers.

Create the below method (do take care of the imports),

public static void setHasEmbeddedTabs(Object inActionBar, final boolean inHasEmbeddedTabs)
{
    // get the ActionBar class
    Class<?> actionBarClass = inActionBar.getClass();

    // if it is a Jelly Bean implementation (ActionBarImplJB), get the super class (ActionBarImplICS)
    if ("android.support.v7.app.ActionBarImplJB".equals(actionBarClass.getName()))
    {
            actionBarClass = actionBarClass.getSuperclass();
    }

    try
    {
            // try to get the mActionBar field, because the current ActionBar is probably just a wrapper Class
            // if this fails, no worries, this will be an instance of the native ActionBar class or from the ActionBarImplBase class
            final Field actionBarField = actionBarClass.getDeclaredField("mActionBar");
            actionBarField.setAccessible(true);
            inActionBar = actionBarField.get(inActionBar);
            actionBarClass = inActionBar.getClass();
    }
    catch (IllegalAccessException e) {}
    catch (IllegalArgumentException e) {}
    catch (NoSuchFieldException e) {}

    try
    {
            // now call the method setHasEmbeddedTabs, this will put the tabs inside the ActionBar
            // if this fails, you're on you own <img src="http://www.blogc.at/wp-includes/images/smilies/icon_wink.gif" alt=";-)" class="wp-smiley">
            final Method method = actionBarClass.getDeclaredMethod("setHasEmbeddedTabs", new Class[] { Boolean.TYPE });
            method.setAccessible(true);
            method.invoke(inActionBar, new Object[]{ inHasEmbeddedTabs });
    }
    catch (NoSuchMethodException e)        {}
    catch (InvocationTargetException e) {}
    catch (IllegalAccessException e) {}
    catch (IllegalArgumentException e) {}
}

Then call this like below,

  1. If you want tabs to appear inside the action bar,

    setHasEmbeddedTabs(actionBar, true);

  2. If you want tabs to appear separate/ below the action bar,

    setHasEmbeddedTabs(actionBar, false);

All credits to Cliff.

like image 54
2 revs, 2 users 99% Avatar answered Jan 21 '23 08:01

2 revs, 2 users 99%