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:
now if i run in the 2.2 android os then it looks like the below image:
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.
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); }
Related
I want to change the 'font style` my menu items.
by default it's looks like this
but I want to change into this
You can customize the option menu, including:
1.Add a custom font
2.Change font size
3.Change font color
4.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 an image, border
//gradient, drawable, etc.
view.setBackgroundResource(R.drawable.myimage);
((TextView) view).setTextSize(20);
// set the text 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);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.AboutUs:
Intent i = new Intent("com.test.demo.ABOUT");
startActivity(i);
break;
case R.id.preferences:
Intent p = new Intent("com.test.demo.PREFS");
startActivity(p);
break;
case R.id.exit:
finish();
break;
}
return false;
}
}
Dont forget to create folder called menu in res folder, and inside the menu folder create an XML for your menu (e.g. cool_menu.xml) such as this:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="about"android:id="#+id/AboutUs" />
<item android:title="Prefs" android:id="#+id/preferences" />
<item android:title="Exit" android:id="#+id/exit" />
</menu>
OR
First download the .ttf file of the font you need (arial.ttf). Place it in the assets folder(Inside assets folder create new folder named fonts and place it inside it). If txtyour is the textviews you want to apply the font , use the following piece of code,
Typeface type = Typeface.createFromAsset(getAssets(),"fonts/Kokila.ttf");
txtyour.setTypeface(type);
I'm trying to change the text color of menu item in options menu.
i read here many similar question and answers saying it's impossible via theme and style and only via code. i also tried the code example and it didn't work.
I manage to change the background using these theme attributes:
#drawable/menu_hardkey_panel_holo_dark
#drawable/menu_hardkey_panel_holo_dark
#ffffff
#ffffff
but i can't make the text color to change.
i saw an answer suggesting using android:itemTextAppearance but it didn't work as well.
I'm using Android 4.0 SDK.
any help will be highly appreciated.
Thanks,
Gidi
You can not change this it comes by default in android API. If you make changes then it is not work for different version of android device.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
getLayoutInflater().setFactory(new Factory() {
#Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
if (name .equalsIgnoreCase(“com.android.internal.view.menu.IconMenuItemView”)) {
try{
LayoutInflater f = getLayoutInflater();
final View view = f.createView(name, null, attrs);
new Handler().post(new Runnable() {
public void run() {
// set the background drawable
view .setBackgroundResource(R.drawable.my_ac_menu_background);
// set the text color
((TextView) view).setTextColor(Color.WHITE);
}
});
return view;
} catch (InflateException e) {
} catch (ClassNotFoundException e) {}
}
return null;
}
});
return super.onCreateOptionsMenu(menu);
}
I'm using Kotlin and the new Navigation component and I was able to change this by adding this line to my theme:
<item name="android:actionMenuTextColor">#color/white</item>
And then manually updating the theme applied to my toolbar like this:
toolbar = findViewById(R.id.toolbar)
toolbar.popupTheme = R.style.AppTheme
Here's the difference:
I would like to display badges on menu items. How do I do it?
Basically, I don't want to draw or use the canvas to do so.
You could try creating a LayerListDrawable, with your regular icon as the first layer and your badge as the second layer, then use that with setIcon() on MenuItem.
The options menu in Android can be customized to set the background or change the text appearance. The background and text color in the menu couldn’t be changed using themes and styles.
The Android source code (data\res\layout\icon_menu_item_layout.xml) uses a custom item of class “com.android.internal.view.menu.IconMenuItem”View for the menu layout. We can make changes in the above class to customize the menu. To achieve the same, use the LayoutInflater factory class and set the background and text color for the view.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.my_menu, menu);
getLayoutInflater().setFactory(new Factory() {
#Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
if (name .equalsIgnoreCase(“com.android.internal.view.menu.IconMenuItemView”)) {
try{
LayoutInflater f = getLayoutInflater();
final View view = f.createView(name, null, attrs);
new Handler().post(new Runnable() {
public void run() {
// Set the background drawable
view .setBackgroundResource(R.drawable.my_ac_menu_background);
// Set the text color
((TextView) view).setTextColor(Color.WHITE);
}
});
return view;
}
catch (InflateException e) {
}
catch (ClassNotFoundException e) {
}
}
return null;
}
});
return super.onCreateOptionsMenu(menu);
}
Menuitem has an attribute icon, for example:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/mi_main_preferences"
android:title="#string/cmd_preferences"
android:icon="#android:drawable/ic_menu_preferences">
</item>
</menu>
Above example uses system icon (preferences menu).
I have a "Cart" item in a menu option, and I'd like to display the number of products in the cart, as shown below.
To achieve this I want to create several images with numbers from 1 to 9 and 9+ and set the correct image as the background of the corresponding menu option item when opening the menu.
How can I do this, i.e. how can I change the background of a menu option item dynamically?
Thanks
Override onPrepareOptionsMenu() menu method. It is called each time user clicks Menu button.
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// Let's find id of resource for drawable with required count
// assuming you have cartIcon1.png, cartIcon2.png etc
// in your `drawable` folder
int resId = getResources().getIdentifier("cartIcon" + numberOfElementsInCart, "drawable", getPackageName());
if (resId != 0)
menu.findItem(R.id.cart).setIcon(resId);
// If resource was not found, set default icon
else
menu.findItem(R.id.cart).setIcon(R.drawable.defaultCart);
return true;
}
Overriding background is much harder, and I believe it's more convinient to have icons with cart and number, since you already have .png for each number
Below is a horrible, horrible hack to change the background image of menus - note that it'll change all the menu item backgrounds. There is probably an easier way to do this now, but this was the only way I found a year or so ago.
Setting the icon of the menu item is a lot easier, and can be done in onPrepareOptionsMenu
// Hack to make the menu item selector blue
protected void setMenuBackground(final int id)
{
if(getLayoutInflater().getFactory() != null)
return;
getLayoutInflater().setFactory(new Factory()
{
#Override
public View onCreateView(String name, Context context, AttributeSet attrs)
{
if(name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView"))
{
try
{
LayoutInflater li = getLayoutInflater();
final View view = li.createView(name, null, attrs);
//What?
//Well the Android system is going to set the background after this is method is done
//so we run it later to override the override. Simples?
new Handler().post(new Runnable()
{
public void run()
{
view.setBackgroundResource(id);
}
});
return view;
}
catch(InflateException e)
{
}
catch(ClassNotFoundException e)
{
}
}
return null;
}
});
}
I have used the following code to customize the background of options menu successfully.
getLayoutInflater().setFactory(new Factory() {
#Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
if (name.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
try { // Ask our inflater to create the view
LayoutInflater f = getLayoutInflater();
final View view = f.createView(name, null, attrs);
/*
* The background gets refreshed each time a new item is
* added the options menu. So each time Android applies
* the default background we need to set our own
* background. This is done using a thread giving the
* background change as runnable object
*/
new Handler().post(new Runnable() {
public void run() {
view.setBackgroundResource(R.drawable.blue_bg_pixel);
}
});
return view;
}
catch (InflateException e) {
}
catch (ClassNotFoundException e) {
}
}
return null;
}
});
Now I have to customize menu size, text size/color. Anybody plz help me in this.
Thanks in advance.
This fails on Android 2.3
See
http://code.google.com/p/android/issues/detail?id=4441#c6
and
Change menu background color on android 2.3
for details
Width , Heigth you need hack action bar to that.
But if you need customize, use : ActionBarGenerator (http://jgilfelt.github.io/android-actionbarstylegenerator/)