The following is directly from the application I'm currently working on. It calls AlertDialog, which then leads the user to another action (after entering the password). Please let me know if I can clarify anything about this.
Edit: The entire method is now included.
@Override public boolean onOptionsItemSelected(MenuItem item) { LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE); final int menuItem = item.getItemId(); if ((menuItem == R.id.survey) || (menuItem == R.id.syncMode)) { View layout = inflater.inflate(R.layout.survey_password_dialog, (ViewGroup) findViewById(R.id.layout_root)); final EditText password = (EditText) layout.findViewById(R.id.passwordText); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setView(layout) .setCancelable(false) .setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); final AlertDialog alertDialog = builder.create(); alertDialog.setOnShowListener(new DialogInterface.OnShowListener() { @Override public void onShow(DialogInterface dialog) { Button b = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (password.getText().toString().equalsIgnoreCase("the_password")) { if (menuItem == R.id.survey) { Intent intent = new Intent(AsthmaAppActivity.this, SurveyActivity.class); startActivity(intent); alertDialog.dismiss(); } else {
source share