Call getLayoutInflater() in places not in activity

You can use this outside activities – all you need is to provide a Context: LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE ); Then to retrieve your different widgets, you inflate a layout: View view = inflater.inflate( R.layout.myNewInflatedLayout, null ); Button myButton = (Button) view.findViewById( R.id.myButton ); EDIT as of July 2014 Davide’s answer on how … Read more

Change background color of android menu [duplicate]

When ur are inflating the menu call this setMenuBackground() method @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater=getMenuInflater(); inflater.inflate(R.menu.menu,menu); setMenuBackground(); return true; } and write this in the setMenuBackground() method protected void setMenuBackground(){ // Log.d(TAG, “Enterting setMenuBackGround”); getLayoutInflater().setFactory( new Factory() { public View onCreateView(String name, Context context, AttributeSet attrs) { if ( name.equalsIgnoreCase( “com.android.internal.view.menu.IconMenuItemView” ) … Read more