3

I tried to implement a custom menu. I used the answer given in this question. In my code the name is ExpandedMenuItem, but in all the examples it is IconMenuItemView. What is happening there? How can I correct this?

Here is my code.

    public class MyActivity extends PreferenceActivity {

         @Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.proximity_alert_menu, menu);
    getLayoutInflater().setFactory(new Factory() {

        @Override
        public View onCreateView(String name, Context context, AttributeSet attrs) {         

                        //if(name.equalsIgnoreCase("com.android.internal.view.menu.MenuItem")) {}
                        try {
                           LayoutInflater li = LayoutInflater.from(context);
                           final View view = li.createView(name, null, attrs);
                           new Handler().post(new Runnable() {
                           public void run() {
                            TextView tView = (TextView) view;
                            tView.setTypeface(Config.set_font);
                            tView.setTextColor(Color.RED);
                           }
                    });
                    return view;
                 } catch (InflateException e) {
                   e.printStackTrace();
                 } catch (ClassNotFoundException e) {
                   e.printStackTrace();
                 }
                 return null;
      }
        });
    return super.onCreateOptionsMenu(menu);
        }
    }

Exception show that

      java.lang.ClassCastException:com.android.internal.view.menu.ExpandedMenuView cannot be cast to android.widget.TextView

How can i cast this into TextView?

Community
  • 1
  • 1
Kelum Deshapriya
  • 258
  • 4
  • 14
  • You're trying to cast apples to oranges. In order to cast `ExpandedMenuView` to `TextView` successfully, `ExpandedMenuView` must extend `TextView`, which it doesn't. – Tadej Mar 24 '14 at 13:39
  • yes that is the problem. how can i make this solved? i mean get any *View* which can cast to `TextView` as a `name`? do you know the reason why i have recieved this `ExpandedMenuView` ? – Kelum Deshapriya Mar 24 '14 at 17:04

2 Answers2

1

You can check as follows that the View is TextView or not...

if (view instanceof TextView) {
    TextView tView = (TextView) view;
    tView.setTypeface(Config.set_font);
    tView.setTextColor(Color.RED);
}

If the view is TextView then the Font and Color will be changed.

Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
-4
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);

    tf = Typeface.createFromAsset(MainActivity.this.getAssets(),
            "MuseoSansCyrl.otf");
    MenuItem i = menu.findItem(R.id.item1);
    TextView itemuser = (TextView) i.getActionView();

           Typeface tf= Typeface.createFromAsset(this.getAssets(), "MuseoSansCyrl.otf");
           itemuser.setTypeface(tf);
           itemuser.setText("Item1");
           itemuser.setBackgroundColor(Color.TRANSPARENT);
           retun true;
}
David
  • 3,392
  • 3
  • 36
  • 47
  • While this code block may answer the question, it would be best if you could provide a little explanation for why it does so. – David Mar 18 '15 at 10:09