Advertisement
amjadArabia

Menu By Code

Nov 17th, 2017
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.05 KB | None | 0 0
  1. package com.arabiaexample.amjad.menubycodetester;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.view.Menu;
  6. import android.view.MenuItem;
  7. import android.widget.Toast;
  8.  
  9. public class MainActivity extends AppCompatActivity {
  10.  
  11.     private int groupId = 1;
  12.     int homeId = Menu.FIRST;
  13.     int profileId = Menu.FIRST + 1;
  14.     int searchId = Menu.FIRST + 2;
  15.     int dealId = Menu.FIRST + 3;
  16.     int helpId = Menu.FIRST + 4;
  17.     int contactUs = Menu.FIRST + 5;
  18.  
  19.  
  20.     @Override
  21.     protected void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.activity_main);
  24.  
  25.     }
  26.  
  27.     @Override
  28.     public boolean onCreateOptionsMenu(Menu menu) {
  29.         menu.add(groupId, homeId, homeId, "Home");
  30.         menu.add(groupId, profileId, profileId, "Profile");
  31.         menu.add(groupId, searchId, searchId, "Search");
  32.         menu.add(groupId, dealId, dealId, "Deal");
  33.         menu.add(groupId, helpId, helpId, "Help");
  34.         menu.add(groupId, contactUs, contactUs, "Contact Us");
  35.  
  36.  
  37.         return super.onCreateOptionsMenu(menu);
  38.     }
  39.  
  40.     @Override
  41.     public boolean onOptionsItemSelected(MenuItem item) {
  42.         switch (item.getItemId()) {
  43.             case 1:
  44.                 Toast.makeText(this, "Home clicked", Toast.LENGTH_SHORT).show();
  45.                 break;
  46.  
  47.             case 2:
  48.                 Toast.makeText(this, "Profile clicked", Toast.LENGTH_SHORT).show();
  49.                 break;
  50.             case 3:
  51.                 Toast.makeText(this, "Search", Toast.LENGTH_SHORT).show();
  52.                 break;
  53.             case 4:
  54.                 Toast.makeText(this, "Deal", Toast.LENGTH_SHORT).show();
  55.                 break;
  56.             case 5:
  57.                 Toast.makeText(this, "Help", Toast.LENGTH_SHORT).show();
  58.                 break;
  59.             case 6:
  60.                 Toast.makeText(this, "Contact Us", Toast.LENGTH_SHORT).show();
  61.                 break;
  62.  
  63.  
  64.         }
  65.         return super.onOptionsItemSelected(item);
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement