Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a font for the Options menu?

Tags:

android

When I create an Options Menu the items seem to default to the native "sans" font. When I look at commercial apps they mostly seem to do the same thing. Is it possible to set the font size, color weight or typeface for Option Menu items?

Thanks in advance.

like image 292
Peter Nelson Avatar asked Nov 09 '10 15:11

Peter Nelson


People also ask

How do I change my font options?

Click on the page menu in the top right-hand corner. It's the three horizontal dots. You'll see three options: Default, Serif, and Mono. Just pick the one you like best, and the font will automatically change.

How do I set my default font?

Go to Format > Font > Font. + D to open the Font dialog box. Select the font and size you want to use. Select Default, and then select Yes.

Where you can find font options?

The font options can all be found in the Home tab on the left. Older versions of Word may have font options in the top-left corner or in the Format menu. If you have an older version of Word, search the internet for where to find the font options in your specific version.

Which menu will allow you to change the font?

To change the font:On the Home tab, click the drop-down arrow next to the Font box. A menu of font styles will appear. Select the font style you want to use. The font will change in the document.


1 Answers

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> 

Then the output will be something like this:

enter image description here

like image 67
Android Stack Avatar answered Sep 29 '22 13:09

Android Stack