What kind of grammar it is in Android? - android

Dialog dialog = new AlertDialog.Builder(Activity01.this)
.setTitle("Login hint")
.setMessage("Here needs your login!")
.setPositiveButton(...)
.setNeutralButton(...
).create();
What kind of grammar it is? I cannot understand why those dots are one by one? And the create() is for Builder() or for setNeutralButton()?
Thanks!

Builder is a static inner class of AlertDialog. Each call returns this allowing you to chain methods. Finally you call create() to create the actual dialog. This is basic Java and has little to do with Android, besides the fact that Android uses this pattern a lot.

setTitle, setMessage are the methods of the DialogBox.
you can also write
Dialog dialog = new AlertDialog.Builder(Activity01.this);
dialog.setTitle("Login hint")
dialog.setMessage("Here needs your login!")
dialog.setPositiveButton(...)
dialog.setNeutralButton(...)
dialog .create();
if you want more clarification about this you can visit this

.setPositiveButton(...) refers you want to pass text to displayed and write the logic for click events.
Refer here:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();

Related

How would I make a basic alert?

I have a if then statement. If the if then statement is true, I want an Android system to fire a basic alert with one button that says ok.
I have done plenty of research, but nothing works.
this is how you create Alert in android
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Write your message here.");
builder.setNeutralButton(
"Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.create().show()

My neutral Ok button is not showing in my alert dialog for Samsung S5 and J700

Trying to build a mobile application and have tested across some devices, but when it comes to Samsung S5 Notebook and J700 mobile phones, both have been failing because they have failed to display the neutral button in the application, please who might have the slightest clue to solving this particular issue. Thanks.
Here is a sample code below;
new AlertDialog.Builder(context)//new Alert Dialog
.setTitle("Delete entry")//set title
.setMessage("Are you sure you want to delete this entry?")
.setNeutralButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})`enter code here`
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
just replace with this -
AlertDialog.Builder builder = new AlertDialog.Builder(context)//new Alert Dialog
.setTitle("Delete entry")//set title
.setMessage("Are you sure you want to delete this entry?")
.setNeutralButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setIcon(android.R.drawable.ic_dialog_alert);
// .show();
AlertDialog d = builder.create();
d.show();
Maybe you should try with .setPositiveButton this should work.
Check if you are using AlertDialog from support library?

Android alert dialog good usage

I couldn't find anything usefull on the internet about my problem. So my question is how do you do a good usage of Android's alert dialogs. Here is an example of code creating and showing an alert dialog just with the title "error", the text "you can't do that" and a "Ok" button :
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("Error");
alertDialogBuilder.setMessage("You can't do that");
alertDialogBuilder.setCancelable(true);
alertDialogBuilder.setPositiveButton(
getResources().getString("ok"),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
alertDialogError = alertDialogBuilder.create();
alertDialogError.show();
But now, if I have many of this alert dialogs in my application, what should I do ?
Should I set the alertDialogBuilder as an attribute so each time I want to display an error message I can call his function setMessage() and then create() and then show() ?
Should I keep an already configured alertDialog for every single error message I have so I can just call theRightAlertDialog.show() to display my message ?
Something else ?
What's the good usage/cleanest way to do this for you ?
You could do this one of two ways. The first is to create a static method, which you can place in a final utility class:
public final class AlertUtil {
public static void showErrorDialog(Context context, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Error");
builder.setMessage(message);
builder.setCancelable(true);
builder.setPositiveButton(
getResources().getString("ok"),
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.show();
}
}
Or you can use a DialogFragment which you can create with:
getSupportFragmentManager().beginTransaction().add(ErrorDialogFragment.newInstance(message), "tag").commit()`
I will say though, as a side note; if you are looking to change more than just a few fields for each of the dialogs (i.e. adding more parameters to the showErrorDialog method), then you probably should just stick to the Builder pattern. Considering that is what the Builder pattern is meant for.
Should I set the alertDialogBuilder as an attribute so each time I want to display an error message I can call his function setMessage() and then create() and then show() ?
If the title and the button functionality are the same for all of your alerts, than this would be the best strategy. Create a variable for the alertDialogBuilder, or even just the alertDialog itself, then change the message and show it each time.
Alternatively, you could create a method that builds the dialog, and takes in a string for the message text.

How can we pass any value (selected one) from dialog to activity in android?

I need help to pass values from a custom dialog to an activity.
I cannot understand what should i use. I already used intent, but dialog doesn't support intent value passing .
So anyone can help me here, i am totally stucked.If you have any basic example for it, then that will be excellent.
Thank You.
Snippet from the Google Documentation:
// Alert Dialog code (mostly copied from the Android docs
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
myFunction(item);
}
});
AlertDialog alert = builder.create();
// Now elsewhere in your Activity class, you would have this function
private void myFunction(int result){
// Now the data has been "returned" (as pointed out, that's not
// the right terminology)
}

Problem with my custom dialog which is having dynamic data

I have a usecase like repeatedly calling the same dialog box with different values. I am using the same dialog creating code for that. First time the sent data is populated to dialog box. but next time the dialog box not getting rebuilt with different values when i call the same for next time.
Code is here
dialog.setContentView(R.layout.orderdialog);
dialog.setTitle("Selected Item");
dialog.setCanceledOnTouchOutside(true);
System.out.println(selected); // here i am sending different values eachtime. But not updating in dialog.
TextView selectedItem = (TextView)dialog.findViewById(R.id.itemName);
selectedItem.setText(selected);
You can use the android alert builder to show dynamic data:
new AlertDialog.Builder(this)
.setTitle("your title name")
.setMessage("here you can write your dynamic data as string or integer")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(/* don't remember the signature by heart... */) {
// continue with delete
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(/* don't remember the signature by heart... */) {
// do nothing
}
})
.show();
Instead of calling
showDialog(id);
and creating dialog in oncreatDialog function
create the dialog and show it in you on click function itself:
like this:
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("State");
builder.setItems(m_arrStateNames, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
m_nSelStateIdx = which;
showState();
dialog.dismiss();
}
});
builder.show();
}
});

Categories

Resources