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).
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 am trying to implement Option menu with icons but i m trying to set background on option menu but not succeeded how to achieve please help me
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.menu,menu);
setMenuBackground();
return true;
}
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" ) ) {
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 () {
// sets the background color
view.setBackgroundResource( R.color.androidcolor);
// sets the text color
((TextView) view).setTextColor(Color.BLACK);
// sets the text size
((TextView) view).setTextSize(18);
}
} );
return view;
}
catch ( InflateException e ) {}
catch ( ClassNotFoundException e ) {}
}
return null;
}});
}
See ActionBarStyleGenerator
Wher you need to set Popup color which will change the menu background as you want. Download generated style and apply in your project.
I have three menu items in my app and i want to change the background image , text color and font for all items but nothing getting changed.
This is the following code.
Java Code -:
public boolean onCreateOptionsMenu(android.view.Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.custom_menu, menu);
getLayoutInflater().setFactory(new Factory()
{
public View onCreateView(String name, final 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()
{
((TextView) view).setTextSize(30);
Typeface face = Typeface.createFromAsset(getAssets(),"fonts/arialbd.ttf");
((TextView) view).setTypeface(face);
((TextView) view).setBackgroundResource(R.drawable.white6);
((TextView) view).setTextColor(Color.BLUE);
}
});
return view;
}
catch (InflateException e)
{
}
catch (ClassNotFoundException e)
{
}
}
return null;
}
});
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.abc:
break;
case R.id.xyz:
break;
case R.id.exit:
break;
}
return false;
}
XML Menu Code-:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/abc"
android:orderInCategory="10"
android:showAsAction="always"
android:title="ABC"/>
<item
android:id="#+id/xyz"
android:orderInCategory="10"
android:showAsAction="always"
android:title="XYZ"/>
<item
android:id="#+id/exit"
android:orderInCategory="10"
android:showAsAction="always"
android:title="Andi"/>
</menu>
and i want to keep all the android:showAsAction="always" , please let me know , what is the problem? Suggest me some good solution.
as i see you want to create and options menu that is doing some changes on your text, color, bg image etc.. when you click on and menu option right ?.
First of all why do u have two onCreateOptionsMenu functions, is the first one in super class or so in an activity extended class how canu extend it too. İ think u can do it in simpley way.
Create your menu items and put your #Override (d) onCreateOptionsMenu function in your class and call a different method which are doing different jobs e.g changeColor() for abc, changeThem() for xyz, changeBGImage() for qwe and so on...
and let me give u some links that are doing this way and i
http://www.mustafasevgi.com/2012/08/android-de-optionsmenu-olusturma.html
http://www.thegeekstuff.com/2013/12/android-app-menus/ (this site has more)
just check these sites if you have time..
You can see they are calling methods in the options switch case blocks for each action.
btw iam not and android developer but curiosity.
hope helps
regards.
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 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); }