Search code examples
androidandroid-layoutandroid-emulatorandroid-version

android option menu issue in different versions


I have created one simple menu with two options that says "Add new contact" & "Settings" with white png images.

So now if i run in the 2.3.3 android OS version it looks like the below image:

Android 2.3.3 option menu

now if i run in the 2.2 android os then it looks like the below image:

enter image description here

So now what can i do if i want to make background black in android 2.2 so that i can get icons visible. please give me any suggestion regarding this issue.


Solution

  • The best way to deal with option menu is to customize it.

    You can customize the option menu, including:

    Add a custom font

    Change font size

    Change font color

    Set background to a Drawable resource (e.g. image, border, gradient)

    To change background to a border or gradient you have to create a resource folder in res called drawable and, inside it, create the border XML or gradient XML.

    This can all be done programatically as shown below:

     public class CustomMenu extends Activity {   
    /** Called when the activity is first created. */   
    @Override  
    public void onCreate(Bundle savedInstanceState) {   
        super.onCreate(savedInstanceState);     } 
    public boolean onCreateOptionsMenu(android.view.Menu menu) { 
        MenuInflater inflater = getMenuInflater();  
        inflater.inflate(R.menu.cool_menu, menu);  
        getLayoutInflater().setFactory(new Factory() {  
            public View onCreateView(String name, Context context, 
                    AttributeSet attrs) {   
            if (name.equalsIgnoreCase(                                             "com.android.internal.view.menu.IconMenuItemView")) { 
        try {  
            LayoutInflater li = LayoutInflater.from(context); 
            final View view = li.createView(name, null, attrs); 
            new Handler().post(new Runnable() { 
            public void run() {   
    // set the background drawable if you want that or keep it default 
    //either FOR image, border, gradient, drawable,etc.//   
            view.setBackgroundResource(R.drawable.myimage); 
            ((TextView) view).setTextSize(20);
            // set the text font and color  
            Typeface face = Typeface.createFromAsset(  
            getAssets(),"OldeEnglish.ttf");  
            ((TextView) view).setTypeface(face); 
            ((TextView) view).setTextColor(Color.RED);      }     }); 
            return view;      } 
            catch (InflateException e) {  
            //Handle any inflation exception here  
            } catch (ClassNotFoundException e) {    
            //Handle any ClassNotFoundException here    
                            }     }   
                return null;    }   }); 
               return super.onCreateOptionsMenu(menu);     }