Advertisement
amjadArabia

Menu (XMl) + AlertDialog

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