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

How does the getView() method work when creating your own custom adapter?

1: The LayoutInflater takes your layout XML-files and creates different View-objects from its contents. 2: The adapters are built to reuse Views, when a View is scrolled so that is no longer visible, it can be used for one of the new Views appearing. This reused View is the convertView. If this is null it … Read more

Problem inflating custom view for AlertDialog in DialogFragment

The first error line gives me the hint that this is related to how you are creating your dialog – not the dialog itself. Are you creating the dialog automatically (which could mean this gets called before the views are all set up) or in response to a button click? I initially had problems with … Read more

NPE while inflating layout (Attempt to invoke virtual method ‘boolean java.lang.String.equals(java.lang.Object)’ on a null object reference)

Change <view to <View, because view is not about empty view. It’s for custom view defined through class attr, like below: <view android:layout_width=”wrap_content” android:layout_height=”wrap_content” class=”com.your.package.YourCustomView” /> And you got Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘boolean java.lang.String.equals(java.lang.Object)’ on a null object reference because of LayoutInflater tries to parse class attr: LayoutInflater source code … Read more