Password Prompt Comes Only first time - android

I want when user click on Uninstall Button,there prompt a password dialog. This Dialog is coming only one time.I'm using this code:
public void run() {
Looper.prepare();
while (!exit) {
// get the info from the currently running task
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(MAX_PRIORITY);
String activityName = taskInfo.get(0).topActivity.getClassName();
Log.d("topActivity", "CURRENT Activity ::" + activityName);
if (activityName.equals("com.android.packageinstaller.UninstallerActivity")) {
//Toast.makeText(context, "Uninstall Clicked", Toast.LENGTH_LONG).show();
Intent startIntent = new Intent(this.context, Alert_Dialog.class);
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.context.startActivity(startIntent);
exit = true;
} else if (activityName.equals("com.android.settings.ManageApplications")) {
Toast.makeText(this.context, "Back", Toast.LENGTH_LONG).show();
exit = true;
}
}
Looper.loop();
}//Run
I want whenever user click on Unistall Prompt should come , below is the code in onClick,
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this); alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView.findViewById(R.id.edit_text);
alertDialogBuilder
.setCancelable(false)
.setNegativeButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
/** DO THE METHOD HERE WHEN PROCEED IS CLICKED*/
String user_text = (userInput.getText()).toString();
/** CHECK FOR USER'S INPUT **/
if (user_text.equals("abc"))
{
Log.d(user_text, "HELLO THIS IS THE MESSAGE CAUGHT :)");
Toast.makeText(myContext,"PAssword Correct",Toast.LENGTH_LONG).show();
Alert_Dialog.this.finish();
//Search_Tips(user_text);
}
else{
Log.d(user_text,"string is empty");
String message = "The password you have entered is incorrect." + " \n \n" + "Please try again!";
AlertDialog.Builder builder = new AlertDialog.Builder(myContext);
builder.setTitle("Error");
builder.setMessage(message);
builder.setPositiveButton("Cancel", null);
builder.create().show();
Alert_Dialog.this.finish();
}
}
});
// .setPositiveButton("Cancel",
// new DialogInterface.OnClickListener() {
// public void onClick(DialogInterface dialog,int id) {
// Alert_Dialog.this.finish();
//
// }
//
// }
// );
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}

Once the dialog calls dismiss(), the view you set for the dialog also would be destroyed.
In your case, this line set the view,
alertDialogBuilder.setView(promptsView);
But when the dialog is closed, the view promptsView is destroyed,
The promptsView should be re-created once again you use it.
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
promptsView = new PromptsView();//new or inflate the view
//....
alertDialogBuilder.setView(promptsView);

Related

Iterate through arraylist and show alert dialog for each item in array list and then move to next item when click positive button

I am creating an auto caller app in which i have used a button which takes data from arrayList, iterate through the arraylist and ask user on each element if want to do next call or pause on that for this i have used alert dialog, but the loop is not getting pause on each element it went to the last element and show alert dialog for that contact number.
private List<ContactEntity> mContactsList = new ArrayList<>();
private ContactEntity c;
private int i = 0;
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.start_call) {
Toast.makeText(this, "Call Started" + mContactsList.size(), Toast.LENGTH_LONG).show();
startAutoCall();
}
return super.onOptionsItemSelected(item);
}
private void startAutoCall() {
if (mContactsList.isEmpty()) {
Toast.makeText(this, "Import Contacts ", Toast.LENGTH_LONG).show();
} else {
c = mContactsList.get(i);
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + c.getPersonContactNumber()));
startActivity(intent);
while(i < mContactsList.size()){
c = mContactsList.get(i);
Log.d(TAG, "startAutoCall: " + c.getId());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Auto Dialer Start");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "hello", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + c.getPersonContactNumber()));
startActivity(intent);
}
});
AlertDialog dialog = builder.create();
dialog.show();
i++;
}
}
}
It's not a good idea to build alert dialog inside a loop. Instead, create one and update the value for each element in the list on button click. Override onClickListener for the dialog so that you can control the flow better.
Here's a snippet in kotlin.
if(mContactList.isNotEmpty()){
c = mContactList.get(i)
val builder = AlertDialog.Builder(this)
builder.setTitle("Auto Dialer Start")
builder.setMessage("Your message here...")
builder.setPositiveButton("OK", null)
val dialog = builder.create()
dialog.setOnShowListener{ dialogInterface->
val btnOk = dialog.getButton(AlertDialog.BUTTON_POSITIVE)
btnOk.setOnClickListener{
//Your code here...
if(i == mContactList.size-1){
dialogInterface.dismiss()
}else {
i++
c = mContactList.get(i)
}
}
}
dialog.show()
}

how to show only the incorrect values in the dialogue box one time only

I am taking input from user in the edittext. Now I want to show the desired output in the other text box, but if user inputs wrong values, a dialog box should open mentioning all the incorrect values...box is opening again and again till it detects all the incorrect values. eg-if i add three wrong values in the edit box it is opening box for 3 times.
String s=editText1.getText().toString();
String z[]=s.split("\\s");
editText2.setText("");
String a = "";
String b = " Not valid";
for(int i=0;i<z.length;i++)
{
int j=Integer.parseInt(z[i]);
if(j>=65 && j<=97)
{
editText2.setText(editText2.getText() + "" + String.valueOf((char) j));
}
else {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
a += z[i]+"\t";
alertDialogBuilder.setTitle("Error");
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setMessage(a+b)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
boolean is_open_dialog=false;
for(int i=0;i<z.length;i++)
{
int j=Integer.parseInt(z[i]);
if(j>=65 && j<=97)
{
editText2.setText(editText2.getText() + "" + String.valueOf((char) j));
}
else {
is_open_dialog = true;
a += z[i]+"\t";
}
}
if(is_open_dialog){
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("Error");
alertDialogBuilder.setCancelable(false);
alertDialogBuilder.setMessage(a+b)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}

EditText dialog input always null (Android)

The following code runs in the onClick method for a button and brings up a dialog box. I can enter text and press ok but my String filename, as shown in the Log.d, is always null. I do not understand why.
How do I get the text inputted in the dialog box to save?
My code runs in an activity(no fragment) and String filename and EditText input are both class member variables.
// Get filename from user
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Enter filename");
// EditText to get user input
this.input = new EditText(this);
dialog.setView(input);
dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
filename = input.getText().toString();
}
});
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// cancelled
}
});
dialog.show();
Log.d(LOG_TAG, "Filename is : " + filename);
I based the code on android prompt user's input using a dialog but my issue is different.
you try to Log the string before you actually input data into it.
try to put the log inside the positive onClick and I think you will see that it does save.
like so:
// Get filename from user
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Enter filename");
// EditText to get user input
this.input = new EditText(this);
dialog.setView(input);
dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
filename = input.getText().toString();
Log.d(LOG_TAG, "Filename is : " + filename);
}
});
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// cancelled
}
});
dialog.show();

How to show alert message in an android app

I am trying to show an alert message.
But when I click the specific button to show the alert message the app stops unfortunately.
my code for alert message is here...
search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Cursor res = myDB.getAllData();
if (res.getCount() == 0) {
showMessage("Error", "Nothing found");
return;
}
StringBuffer buffer = new StringBuffer();
while (res.moveToNext()) {
buffer.append("EMAIL: " + res.getString(0) + "\n");
buffer.append("full_name : " + res.getString(1) + "\n");
buffer.append("district : " + res.getString(2) + "\n");
buffer.append("phone_num : " + res.getString(3) + "\n");
}
showMessage("Data", buffer.toString());
}
});
}
public void showMessage(String title, String message)
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
Try this code :
AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
builder1.setMessage("Write your message here.");
builder1.setCancelable(true);
builder1.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder1.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
instead of
AlertDialog.Builder builder = new AlertDialog.Builder(this);
use this
AlertDialog.Builder builder = new AlertDialog.Builder(YourActivity.this); // for activity
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // for fragment
I have tested showMessage method. and it worked without no problem. I guess you must check your database connection ,cursor and buffer. However This code is for showing an alert message:
/**
* #param context
* #param title
* #param message
*
* Caution:Error--> android.view.WindowManager$BadTokenException:
* Unable to add window -- token null is not for an application
*
* --> Instead of getApplicationContext(), just use
* ActivityName.this
*/
public static AlertDialog showDialog(final Context context, String title,
String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(true);
builder.setMessage(message);
builder.setTitle(title);
builder.setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
return builder.show();
}
public static void showAlert(final Activity activity, String title,
String message, final boolean finish) {
new AlertDialog.Builder(activity).setTitle(title).setMessage(message)
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
if (finish) {
activity.finish();
}
}
}).show();
}

Android: How to show a dialog after user clicks "Okay" on a previous dialog

How come the AlertDialog that has the title "Location was saved to file" doesn't show up? It is the one that should be displayed after the user presses Okay on the first dialog.
I think it has something to do with threads, but I'm not sure.
SimpleDateFormat timeStampFormat = new SimpleDateFormat("MMMMM-dd-yyyy");
final EditText input = new EditText(EncounterActivity.this);
input.setWidth(75);
input.setText("Bear-Encounter-GPS-" + timeStampFormat.format(new Date()) + ".txt");
new AlertDialog.Builder(EncounterActivity.this)
.setTitle("Save GPS Location")
.setMessage("Please enter a filename")
.setView(input)
.setIcon(R.drawable.gps)
.setPositiveButton("Save", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
try {
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()){
File fn = new File(root, input.getText().toString());
FileWriter gpxwriter = new FileWriter(fn);
BufferedWriter out = new BufferedWriter(gpxwriter);
out.write(ll.toUTMRef().toString());
out.close();
AlertDialog.Builder builder = new AlertDialog.Builder(EncounterActivity.this);
builder.setIcon(R.drawable.gps);
builder.setTitle("Location was saved to file");
builder.setMessage("Your GPS coordinates were saved to " + fn.getAbsolutePath())
.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alert = builder.create();
alert.show();
}
} catch (IOException e) {
//ProfitBandit.alert(Shipment.this, "Couldn't write the file.");
Log.v("IOException", "Could not write file " + e.getMessage());
}
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Do nothing.
}
}).show();
You CAN display an alert dialog from an alert dialog if you use the pattern showDialog(int) getInstanceSomeDialog and onCreateDialog(int) for both dialogs. So in my aboutAlertDialog I have:
builder.setPositiveButton("View EULA", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) { //cancels itself?
showDialog(DIALOG_EULA_SHOW);
}
});
which in turn displays an EULA in yet another AlertDialog. OR you could just toss up a Toast as in:
Toast.makeText(Main.this,"Location Saved.", Toast.LENGTH_SHORT).show();
where Main is the activity class.

Categories

Resources