m_naish

Menu - example

Nov 20th, 2017
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. activityMain
  2. =================
  3. package com.example.app0811.mymenuxml;
  4.  
  5. import android.app.AlertDialog;
  6. import android.content.DialogInterface;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.os.Bundle;
  9. import android.text.InputType;
  10. import android.view.Menu;
  11. import android.view.MenuInflater;
  12. import android.view.MenuItem;
  13. import android.widget.EditText;
  14. import android.widget.Toast;
  15.  
  16. public class MainActivity extends AppCompatActivity {
  17.  
  18.     String myText="";
  19.     @Override
  20.     protected void onCreate(Bundle savedInstanceState) {
  21.         super.onCreate(savedInstanceState);
  22.         setContentView(R.layout.activity_main);
  23.     }
  24.  
  25.     @Override
  26.     public boolean onCreateOptionsMenu(Menu menu)
  27.     {
  28.         MenuInflater inflater = getMenuInflater();
  29.         inflater.inflate(R.menu.menu, menu);
  30.         return super.onCreateOptionsMenu(menu);
  31.     }
  32.  
  33.     @Override
  34.     public boolean onOptionsItemSelected(MenuItem item)
  35.     {
  36.         switch (item.getItemId())
  37.         {
  38.             case R.id.AddTask:
  39.                 dlgAddTask();
  40.                 Toast.makeText(this, myText, Toast.LENGTH_SHORT).show();
  41.                 //insert code here
  42.                 break;
  43.             case R.id.Logout:
  44.                 Toast.makeText(this, "LOGOUT", Toast.LENGTH_SHORT).show();
  45.                 //insert code here
  46.                 break;
  47.         }
  48.         return super.onOptionsItemSelected(item);
  49.     }
  50.  
  51.     private void dlgAddTask()
  52.     {
  53.         //create builder
  54.         AlertDialog.Builder builder=new AlertDialog.Builder(this);
  55.         //set title
  56.         builder.setTitle("Input new task");
  57.  
  58.         //set up the input
  59.         final EditText input = new EditText(this);
  60.         //specify the type of input expected
  61.         input.setInputType(InputType.TYPE_CLASS_TEXT);
  62.         //add EditText view to the Dialog
  63.         builder.setView(input);
  64.         // positive feed back
  65.         builder.setPositiveButton("ADD TASK", new DialogInterface.OnClickListener() {
  66.             public void onClick(DialogInterface dialog, int which) {
  67.                 myText = input.getText().toString();
  68.                 Toast.makeText(MainActivity.this, "HELLO "+myText+" THE KING!!", Toast.LENGTH_SHORT).show();
  69.             }
  70.         });
  71.         //negative feed back
  72.         builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
  73.             @Override
  74.             public void onClick(DialogInterface dialog, int which) {
  75.                 //Toast.makeText(context, "YOU ARE NOT SO SMART, HUH?", Toast.LENGTH_SHORT).show();
  76.                 dialog.cancel();
  77.             }
  78.         });
  79.         builder.show();
  80.     }
  81.  
  82. }
  83.  
  84.  
  85.  
  86. menu.xml
  87. ===============
  88. <?xml version="1.0" encoding="utf-8"?>
  89. <menu xmlns:android="http://schemas.android.com/apk/res/android">
  90.     <item
  91.         android:id="@+id/AddTask"
  92.         android:title="Add Task"/>
  93.     <item
  94.         android:id="@+id/Logout"
  95.         android:title="Log Out"/>
  96. </menu>
Add Comment
Please, Sign In to add comment