Android prompt user input dialog - java

I would like to prompt the user to give me input in my android application using a dialog.
When the user puts the figure it will go to another screen, but it does not give the user to input the value it does not waiting to the user its going to another activity directly.
What can i do to waiting the user to input the value he needs and after that it will take the user to the activity he needs. This is the code that i use.
else
{
{
// get prompts.xml view
LayoutInflater layoutInflater = LayoutInflater.from(Login.this);
View promptView = layoutInflater.inflate(R.layout.activity_input_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Login.this);
alertDialogBuilder.setView(promptView);
final EditText editText = (EditText) promptView.findViewById(R.id.editCodeSurvey);
// setup a dialog window
alertDialogBuilder.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
String surveyCode = editText.getText().toString();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create an alert dialog
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
//After Creating Dialog then we asking if the User that signed in is a manager
if(parseUser.getBoolean("isManager"))
{
//open manager Class
startActivity(new Intent(Login.this,ManagerScreen.class));
}
else{
//open Student Class to fill the survey
startActivity(new Intent(Login.this,StudentField.class));
}

You are not waiting for user input. You need to put code of launching new activity inside the Dialog box listener.
Put
//After Creating Dialog then we asking if the User that signed in is a manager
if(parseUser.getBoolean("isManager"))
{
//open manager Class
startActivity(new Intent(Login.this,ManagerScreen.class));
}
else{
//open Student Class to fill the survey
startActivity(new Intent(Login.this,StudentField.class));
}
inside onClick of OK button. It should be like this
public void onClick(DialogInterface dialog, int id) {
String surveyCode = editText.getText().toString();
//After Creating Dialog then we asking if the User that signed in is a manager
if(parseUser.getBoolean("isManager"))
{
//open manager Class
startActivity(new Intent(Login.this,ManagerScreen.class));
}
else{
//open Student Class to fill the survey
startActivity(new Intent(Login.this,StudentField.class));
}
}

public void onClick(DialogInterface dialog, int id) {
String surveyCode = editText.getText().toString();
if(parseUser.getBoolean("isManager")) {
//open manager Class
startActivity(new Intent(Login.this,ManagerScreen.class));
} else {
//open Student Class to fill the survey
startActivity(new Intent(Login.this,StudentField.class));
}
}
put the code inside on click method

Related

Alert Dialogue Box for Forget Password

The dialog box just works fine but as soon as I leave an empty field in the textfield ( for email) I get returned back to Login Activity. How can I keep showing the Dialogue Box without going back. How can I create a condition for Empty Email Field.
forgetPass.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final EditText resetMail = new EditText(v.getContext());
final AlertDialog.Builder passwordResetDialog = new AlertDialog.Builder(v.getContext());
passwordResetDialog.setTitle("Reset Password?");
passwordResetDialog.setMessage("Enter Your Email To Receive The Reset Link");
passwordResetDialog.setView(resetMail);
passwordResetDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
final String mail = resetMail.getText().toString().trim();
if (TextUtils.isEmpty(mail)) {
Toast.makeText(getApplication(), "Enter Valid Email Address.", Toast.LENGTH_SHORT).show();
return;
} else {
fAuth.sendPasswordResetEmail(mail).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Toast.makeText(Login.this, "Reset Link Sent To Your Email.", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(Login.this, "Error ! Reset Link Not Sent." + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
});
passwordResetDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Close The Dialog Box
}
});
passwordResetDialog.create().show();
}
});
First i will suggest you to create your own layout of dialog in which there should be close button to close the dialog and then add this line
passwordResetDialog.setCancelable(false);
this will prevent the dialog to close and use this function dismiss();on the click of that cross/cancel button to close the dialog.
I would suggest that you build your own Dialog with your own Buttons
then you can use the button without dismiss
Button b1 = (Button) dialog.findViewById(R.id.dialog_bt_yes);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//your code with no dismiss
}
});
Button b2 = (Button) dialog.findViewById(R.id.dialog_bt_no);
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// close dialog
dialog.dismiss();
}
});
when you are using Alert Dialog it's not possible to click the positive or negative button and not finishing the dialog. Use DialogFragment to manage the dialog ensures that it correctly handles lifecycle events such as when the user presses the Back button or rotates the screen. using custom Dialog Fragment it's a bit harder but it's more flexible
see the link to see how to have custom Dialogs:
https://guides.codepath.com/android/using-dialogfragment

How to know in dialog that another dialog is closed using this dialog inside java class in android?

I have dialog class, in that class I have one method that opens a dialog.
providerDialog.showProvidersDialog(customCategory.getCustCategoryId());
In that method I open another dialog (that is written in another class) to fill some information. Now dialog open and user filled details and I dismissed dialog from that method. Now I want to know in this current dialog class that another last opened dialog is closed so that I can get that details which I share using singleton and want to show in this class.
Current class method:
private void showCategoryInformationDialog() {
LayoutInflater li = LayoutInflater.from(context);
informationPromptsView = li.inflate(R.layout.row_category_information_layout, null);
android.app.AlertDialog.Builder alertDialogBuilder = new android.app.AlertDialog.Builder(context, R.style.dialogBoxStyle);
// alertDialogBuilder.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
alertDialogBuilder.setView(informationPromptsView);
inputBillProvider = informationPromptsView.findViewById(R.id.inputBillProvider);
inputConsumerNumber = informationPromptsView.findViewById(R.id.inputConsumerNumber);
name = informationPromptsView.findViewById(R.id.name);
inputBillProvider.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
alertDialogBuilder.setPositiveButton(context.getString(R.string.add), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialogBuilder.setNegativeButton(context.getString(R.string.cancel), new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialogBuilder.show();
}
//I want to know that this below dialog is closed providerDialog.showProvidersDialog(customCategory.getCustCategoryId());
Another last opened dialog code:
Link: Cancel/dismiss alertdialog builder from any other method in same class in android?

How to Create A "Do you want to Continue?" Alert Box

In my application I have a button that when pressed I want it to display an Alert Dialog Box that asks if you want to continue. It will have two buttons: a "Continue" and "Do Not Continue". I am putting the method that opens up the dialog box within the method that opens the new Activity like so:
case R.id.bRegister:
try{
//the method for opening the alert box goes somewhere here but i don't know where yet.
Class ourClass = Class.forName("org.health.blablablabla.app.RegisterData");
Intent ourIntent = new Intent(MainActivity.this,ourClass);
finish();
startActivity(ourIntent);
overridePendingTransition(R.animator.fadein,R.animator.fadeout);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
This is currently what I have for the Alert Dialog Box method:
private void showWarning(){
AlertDialog.Builder warning = new AlertDialog.Builder(this);
warning.setTitle("Existing Data");
warning.setMessage("There is already existing data. If you continue all previous data will be deleted. Are you sure you want to continue?");
warning.setPositiveButton("Continue",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1)
{
arg0.dismiss();
}
});
warning.setNegativeButton("Do Not Continue",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
});
}
My question is where to put the method in the first block of code, and how do I make it so that when the "Do Not Continue" button is pressed, the New Activity "RegisterData" doesn't open up.
you can make an YesNoSampleActivity and use AlertDialog.Builder like this:
public class YesNoSampleActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Put up the Yes/No message box
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder
.setTitle("Erase hard drive")
.setMessage("Are you sure?")
.setIcon(android.R.drawable.ic_dialog_alert)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//Yes button clicked, do something
Toast.makeText(YesNoSampleActivity.this, "Yes button pressed",
Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("No", null) //Do nothing on no
.show();
// Continue code after the Yes/No dialog
// ....
}
}

Returning a result from multi-use dialog android

So in my android application, I have a helper class which can create a dialog box using the following code:
public void CreateAlert(String title, String message)
{
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
// Setting Dialog Title
alertDialog.setTitle(title);
// Setting Dialog Message
alertDialog.setMessage(message);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which) { }
});
// Showing Alert Message
alertDialog.show();
}
In my registrar activity, I have an ASyncTask that runs a particular task, following which it processes a boolean variable with the below function:
private void Process(boolean Success)
{
if(Success)
{
appHelper.CreateAlert("Success!", "Well Done!");
// Progress to the activity
startActivity( new Intent(context, Menu.class) );
getActivity().finish();
}
else
{
appHelper.CreateAlert("Failure!", "Please try again.");
}
}
However as you can see from the above code, I am wanting to start an activity based on a succesful result. However, I need the application to wait for the user to click the okay button on the dialog before progressing to the next activity.
What is the best way for this to be done?
EDIT: I should note that the two functions are in different classes, CreateAlert is in a class called AppHelper where as Process is inside Registrar.class, an activity.
Check again the status of boolean success in the click listener of alertDialog's OK button
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which) {
if(Success)
{
// Progress to the activity
context.startActivity( new Intent(context, Menu.class) );
context.getActivity().finish();
}
else
{
// Don't do anything
}
});
and update your Process like below
private void Process(boolean Success)
{
if(Success)
{
appHelper.CreateAlert("Success!", "Well Done!");
}
else
{
appHelper.CreateAlert("Failure!", "Please try again.");
}
}

How to raise a Toast on top of a AlertDialog?

I am validating an AlertDialog, and I would like to raise a Toast on top of the AlertDialog display.
I have this code, but the Toast is displayed on the activity
new AlertDialog.Builder(this).setTitle(R.string.contact_groups_add)
.setView(addView).setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
if (wrapper.getTitle().length()>0)
{
processAdd(wrapper);
} else {
Toast.makeText(getApplicationContext(), "Name is required", Toast.LENGTH_SHORT).show();
}
}
}).setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// ignore, just dismiss
}
}).show();
Instead of using AdvertDialog.Builder, you can create a custom dialog which will behave like a dialog, but is in fact a normal activity. Your toasts should be drawn normally on top of this.
Had this problem myself as well, when I wanted to show a validation message within a dialog.
The answer that seanhodges gave is probably the cleaner and better way. But a seperate activity wasnt practical for me, so i came up with this solution.
Anyway, you can use the AlerDialog.Builder, and show a toast.
If you override the OnClickListener of the button the you want to trigger the toast, you can show a toast on top of a dialog.
An example;
public void showToastOnDialog(final Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Dialog title");
builder.setMessage("Dialog message");
builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do nothing, you will be overriding this anyway
}
});
builder.setNegativeButton(android.R.string.cancel,
new OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// You can implement code here, because you wont be
// overriding this
}
});
final AlertDialog dialog = builder.create();
// Make sure you show the dialog first before overriding the
// OnClickListener
dialog.show();
// Notice that I`m not using DialogInterface.OnClicklistener but the
// View.OnClickListener
dialog.getButton(Dialog.BUTTON_POSITIVE).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast toast = Toast.makeText(context,
"I`m a toast on top of a dialog.",
Toast.LENGTH_LONG);
toast.show();
// Because you are overriding the OnClicklistener, the
// dialog will not auto dismiss
// after clicking
dialog.dismiss();
}
});
}
Try this:
AlertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_PHONE);

Categories