Change the background of the AlertDialog - java

i want to know how to change the design of my AlertDialog, the buttons the background.
thank you.
new AlertDialog.Builder(this)
.setTitle("Exit")
.setMessage("Do you want to exit")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_menu_zoom)
.show();
}

AlertDialog.Builder alertDialog_ImageSelector=new AlertDialog.Builder(mcontext);
View imageGuideLineInfo=getActivity().getLayoutInflater().inflate(R.layout.your_alyout,null);
alertDialog_ImageSelector.setView(imageGuideLineInfo);
TextView tv_guidelines=(TextView)imageGuideLineInfo.findViewById(R.id.tv_guidelines);
final AlertDialog adinfo=alertDialog_ImageSelector.create();
adinfo.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//here you can change the background of alertDialog.
adinfo.show();
if you want to change the background of buttons then you need to create the buttons in your_view.xml and bind them here and setBackground.

use this android inbuilt theme
AlertDialog.Builder builder = new AlertDialog.Builder(this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);

Related

Exit button that can ask you "Do you want to close it"?

I'm a beginner in java and I have a question for you do you know how I can create the button of exit? This button can ask me before I close the application "Do you want to close this application? or "Are you sure to close it?" I need to do it for my project and I need help. Pls send me some code.
Your question is very broad, however, an AlertDialog is what you are looking for, this is the implementation:
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();
alert.show();
1.- Search how create a dialog on Android.
Link -> https://developer.android.com/guide/topics/ui/dialogs?hl=es-419
2.- In your view put a back button with onClickListener, insert showDialog created inside.
3.- Implement the override onBackPressed method, insert showDialogCreated inside.
Example onBackPressed
public void onBackPressed() {
var dialog = CustomDialog.newInstance();
dialog.setCancelable(false);
dialog.show(this.getSupportFragmentManager(), "TAG");
dialog.setOnClickListener((whichViewID, tag, args) => {
// Your Logic
// If is pressed positive button call super.onBackPressed else dialog.dismiss()
});
}

Android how to make ok button on dialog not all caps

For an alert dialog in Android, how do you make the positive button not have all capital letters. The text is "OK" instead of "Ok".
The accepted solution above won't work in Lollipop and above. Here's the working solution.
After showing the dialog, I'm setting the button all caps false. Make sure you do it after dialog.show(). Else, you'll get Null Pointer Exception.
AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
builder.setTitle("Title");
builder.setMessage("message");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Do Something
}
});
AlertDialog dialog = builder.create();
dialog.show();
dialog.getButton(AlertDialog.BUTTON_POSITIVE).setAllCaps(false);
Use the DialogInterface.BUTTON_POSITIVE or DialogInterface.BUTTON_NEGATIVE to customize the action buttons.
val builder = MaterialAlertDialogBuilder(requireContext())
builder.setTitle(getString(R.string.alert_title))
builder.setMessage(getString(R.string.alert_msg))
builder.setPositiveButton(getString(R.string.action_yes)) { _, _ ->
// todo: your action
}
builder.setNegativeButton(getString(R.string.action_no), null)
val dialog = builder.create()
dialog.show()
dialog.getButton(DialogInterface.BUTTON_POSITIVE).isAllCaps = false
dialog.getButton(DialogInterface.BUTTON_NEGATIVE).isAllCaps = false
You can set it to be anything you want - Eg.:
AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
builder.setTitle("Title");
builder.setMessage("message");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
return;
}
});
AlertDialog dialog = builder.create();
dialog.show();
Reference: http://developer.android.com/reference/android/app/AlertDialog.Builder.html#setPositiveButton(int, android.content.DialogInterface.OnClickListener)
Or even :
builder.setPositiveButton("Ok", null);
For the basic usage.
using androidx.appcompat.app.AalertDialog fixed for me.

redirect after ok is pressed in android

I have the following code:
if(isSolved()){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("You solved the puzzle! Congratulations!")
.setCancelable(false)
.setPositiveButton("Thanks.", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent intent=new Intent(context, MainMenuActivity.class);
startActivity (intent);
}
});
}
It's a puzzle and when the puzzle it resolved it shows the above message:"Congrats! you finished the game!". But when I press ok it doesn't do anything. I would want after pressing ok to redirect to another page. i'm using java with eclipse.
You need to add OnClickListener to the PositiveButton and handle your redirect in onclick:
builder.setPositiveButton("Thanks.", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Do your redirect here
}
});
builder.setMessage("Congrats! you finished the game!")
.setCancelable(false)
.setPositiveButton("Thanks.", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Do something
}
});
AlertDialog.Builder.setPositiveButton()
You can use alert.setButton("Okay", listener);
And in your listener catch the click event.

Alert dialog not working correctly - android

I am using an alert dialog to ask the user for confirmation of an item addition to an array. I am printing the array size before and after the alert dialog and it seems that both printings are done before the alert dialog shows up.
Log.d("before", ""+wayPoints.length);
AlertDialog.Builder ab = new AlertDialog.Builder(getContext());
ab .setTitle("Add entry");
ab .setMessage("Are you sure you want to add this entry?");
ab .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{wayPoints = ArrayHandler.addAtIndex(wayPoints, node, 1);}
});
ab .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
});
ab.setIcon(android.R.drawable.ic_dialog_alert);
ab.show();
Log.d("after", ""+wayPoints.length);
The ArrayHandler.addAtIndex method handles the addition of a new item to the provided array.
The problem is it is printing both logging lines before the alert dialog shows up. I need the alert dialog to be done before the printing of the second log.
This is correct behavior. The show method does not wait or block until the dialog is dismissed. Code execution continues after the show method immediately after the dialog is shown. (Actually, the dialog isn't even displayed yet - that happens after you yield control).
You already have setPositiveButton and setNegativeButton handlers, just put the code you want to execute in those handlers when user presses a button.
If you want certain code to run when the dialog is closed regardless of which button is tapped, use setOnDismissListener and put your code there.
Use:
AlertDialog alert1 = ab.create();
alert.show();
Instead of:
ab.show();
Try this
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( context);  
// set title
alertDialogBuilder.setTitle("Your Title");
  // set dialog message
alertDialogBuilder .setMessage("Click yes to exit!") .setCancelable(false) .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, close
// current activity
MainActivity.this.finish(); }
}) .setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
wayPoints = ArrayHandler.addAtIndex(wayPoints, node, 1);
Log.d("after", ""+wayPoints.length);
} });
});  
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();  
// show it
alertDialog.show();

Alertdialog inside AlerDialog

I need to show AlertDialog#1 with custom layout R.layout.add_new_entry with button mybtn and this button should open another AlertDialog#2 with single choice list and after choosing one of them this dialog (AlertDialog#2) should send index of shoosen into first dialog (AlertDialog#1) and set some values into EditText field in AlerDialog#1. Is it possible to make?
My dialog#1 with custom layout:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.add_new_entry, null))
.setTitle(dialogTitle)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int id) {
// My functions...
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// My functions...
}
});
builder.create().show();
I'v spent all night on it and i can't find any good examples or articles how to open dialog inside dialog.
Thanks

Categories

Resources