Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.text.InputType;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.widget.EditText;
- import android.widget.Toast;
- public class MainActivity extends AppCompatActivity {
- String myText="";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu)
- {
- MenuInflater inflater = getMenuInflater();
- inflater.inflate(R.menu.menu, menu);
- return super.onCreateOptionsMenu(menu);
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item)
- {
- switch (item.getItemId())
- {
- case R.id.AddTask:
- dlgAddTask();
- Toast.makeText(this, myText, Toast.LENGTH_SHORT).show();
- //insert code here
- break;
- case R.id.Logout:
- Toast.makeText(this, "LOGOUT", Toast.LENGTH_SHORT).show();
- //insert code here
- break;
- }
- return super.onOptionsItemSelected(item);
- }
- private void dlgAddTask()
- {
- //create builder
- AlertDialog.Builder builder=new AlertDialog.Builder(this);
- //set title
- builder.setTitle("Input new task");
- //set up the input
- final EditText input = new EditText(this);
- //specify the type of input expected
- input.setInputType(InputType.TYPE_CLASS_TEXT);
- //add EditText view to the Dialog
- builder.setView(input);
- // positive feed back
- builder.setPositiveButton("ADD TASK", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- myText = input.getText().toString();
- Toast.makeText(MainActivity.this, "HELLO "+myText+" THE KING!!", Toast.LENGTH_SHORT).show();
- }
- });
- //negative feed back
- builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- //Toast.makeText(context, "YOU ARE NOT SO SMART, HUH?", Toast.LENGTH_SHORT).show();
- dialog.cancel();
- }
- });
- builder.show();
- }
- }
- menu.xml
- ===============
- <?xml version="1.0" encoding="utf-8"?>
- <menu xmlns:android="http://schemas.android.com/apk/res/android">
- <item
- android:id="@+id/AddTask"
- android:title="Add Task"/>
- <item
- android:id="@+id/Logout"
- android:title="Log Out"/>
- </menu>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement