Friday, February 22, 2019

Custom button Menu

Normaly, button Menu if press will look like this.

We want Menu button can show some tabs to choose an action, like to open new class.
In file string of tag values, add these lines.

<string name="he">Update</string>
 <string name="he2">Newgame</string>
<string name="he3">Quit</string>

Change file main.xml in tag menu like this.

<item
        android:id="@+id/action_settings"
        android:orderInCategory="1"
        android:title="@string/action_settings"
        app:showAsAction="ifRoom"
        android:icon="@android:drawable/btn_star"/>
    <item
        android:id="@+id/new_game"
        android:orderInCategory="2"
        android:title="@string/he"
        app:showAsAction="ifRoom"
        android:icon="@android:drawable/btn_star"/>
    <item
        android:id="@+id/new_game2"
        android:orderInCategory="3"
        android:title="@string/he2"
        app:showAsAction="ifRoom"
        android:icon="@android:drawable/btn_star"/>

In main class, add this.
@Override
     public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
     }
     @Override
     public boolean onOptionsItemSelected(MenuItem item) {

     int id = item.getItemId();
          if (id == R.id.action_settings) {
     startActivityForResult(new Intent(
                          android.provider.Settings.ACTION_SETTINGS), 0);
               return true;
          }
          if (id == R.id.new_game) {
     Intent intent = new Intent(getApplicationContext(),
                          Newclass.class);
               startActivity(intent);

               return true;
          }
          if (id == R.id.new_game2) {
     Intent intent = new Intent(getApplicationContext(),
                          Newclass2.class);
               startActivity(intent);
               return true;
          }
          return super.onOptionsItemSelected(item);
     }


Now when press button Menu, screen will look like this.

No comments:

Post a Comment