enable Embedded ActionBar Tabs - Android User Interface

Android examples for User Interface:ActionBar

Description

enable Embedded ActionBar Tabs

Demo Code


import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarImplJB;
import android.support.v7.app.ActionBarImplJBMR2;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Main{
    /*from ww  w  .  j  av a 2s. c om*/
    public static void enableEmbeddedTabs(ActionBar actionBar) {
        if (actionBar instanceof ActionBarImplJBMR2) {
            try {
                Field actionBarField = actionBar.getClass().getSuperclass()
                        .getSuperclass().getDeclaredField("mActionBar");
                actionBarField.setAccessible(true);
                enableEmbeddedTabs(actionBarField.get(actionBar));
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (actionBar instanceof ActionBarImplJB) {
            try {
                Field actionBarField = actionBar.getClass().getSuperclass()
                        .getDeclaredField("mActionBar");
                actionBarField.setAccessible(true);
                enableEmbeddedTabs(actionBarField.get(actionBar));
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            enableEmbeddedTabs((Object) actionBar);
        }

    }
    private static void enableEmbeddedTabs(Object actionBar) {
        try {
            Method setHasEmbeddedTabsMethod = actionBar.getClass()
                    .getDeclaredMethod("setHasEmbeddedTabs", boolean.class);
            setHasEmbeddedTabsMethod.setAccessible(true);
            setHasEmbeddedTabsMethod.invoke(actionBar, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related Tutorials