get Menu Presenter - Android android.app

Android examples for android.app:Activity Property

Description

get Menu Presenter

Demo Code

import java.lang.reflect.Field;

import android.app.Activity;
import android.view.View;
import android.view.ViewParent;

public class Main {

  public static Object getMenuPresenter(Activity a) {
    View homeButton = a.findViewById(android.R.id.home);
    ViewParent parentOfHome = homeButton.getParent().getParent(); // ActionBarView is parent of home ImageView, see
                                                                  // layout file in sources
    try {//from w  w  w  .j a v  a  2s . c om
      if (!parentOfHome.getClass().getName().contains("ActionBarView")) {
        parentOfHome = parentOfHome.getParent();
        Class<?> absAbv = parentOfHome.getClass().getSuperclass(); // ActionBarView -> AbsActionBarView class
        Field menuPresenter = absAbv.getDeclaredField("mActionMenuPresenter");
        // ActionMenuPresenter is the object that calls openOverflowMenu()
        // closeOverflowMenu()
        menuPresenter.setAccessible(true); // and contains the overflow button view.
        Object menuPresenterView = menuPresenter.get(parentOfHome);
        return menuPresenterView;
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;
  }

}

Related Tutorials