Define button style for Alert Dialog [duplicate] - java

This question already has answers here:
How can I change default dialog button text color in android 5
(18 answers)
Closed 3 years ago.
I'm working on an Android App. I have the need to customize Alert Dialog buttons because it shows me the buttons in an unspecified way.
The code to invoke the alert dialogue is:
new AlertDialog.Builder(context,R.style.dialog_theme)
.setTitle(R.string.dialog_title_avviso)
.setMessage(messageResId)
.setPositiveButton(R.string.dialog_button_si, (dialog, which) -> listener.onResponse(dialog, DialogResponse.OK))
.setNegativeButton(R.string.dialog_button_no, (dialog, which) -> listener.onResponse(dialog, DialogResponse.CANCEL))
.show();
In the style.xml I define the following style:
<style name="dialog_theme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowTitleStyle">#style/AlertDialogTitleStyle</item>
</style>
I would like to set the dialogue in the following mode:
Any ideas? Thanks

create alert dialog object first, and then use it to change color of text inside button.
CustomDialog builder = new CustomDialog(getActivity(), "Try Again", errorMessage);
AlertDialog dialog = builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
...
}
}).create();
dialog.setOnShowListener( new OnShowListener() {
#Override
public void onShow(DialogInterface arg0) {
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(COLOR.BLUE);
}
});
dialog.show()

Related

How to make only AlertDialog's button clickable when dialog is showing [duplicate]

This question already has answers here:
Prevent Android activity dialog from closing on outside touch
(20 answers)
Closed 2 years ago.
i have an AlertDialog on my Android app. When i show to AlertDialog i want to disable only when users click "OKAY" button. Because i reset the screen when users clicked "OKAY" button.
My problem is when i click somewhere on screen outside of AlertDialog, dialog is closing but i can not clean the screen.
This is my code;
AlertDialog.Builder builder = new AlertDialog.Builder(GameOnePlayer.this, R.style.AlertDialogTheme);
//builder.setCancelable(true);
View view = LayoutInflater.from(GameOnePlayer.this).inflate(
R.layout.layout_winner_dialog,
(ConstraintLayout)findViewById(R.id.layoutAlertDialogContainer)
);
builder.setView(view);
final AlertDialog alertDialog = builder.create();
//alertDialog.setCanceledOnTouchOutside(false);
view.findViewById(R.id.buttonAlertDialog).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
hideSystemUI();
clearScreen();
alertDialog.dismiss();
}
});
if(alertDialog.getWindow() != null){
alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
}
alertDialog.show();
I tried alertDialog.setCanceledOnTouchOutside(false); but it did'not work.
<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowCloseOnTouchOutside">false</item>
</style>
This is my style.xml
What do I have to do for this? Thanks.
Edit: I tried these advices but did not work.
builder.setCancelable(false);
alertDialog.setCancelable(false);
alertDialog.setCanceledOnTouchOutside(false);
Edit2:
Hi i found the solution on the other post.
Just added these line in the onCreate method.
this.setFinishOnTouchOutside(false);
Many thanks for all you helpers.
alertDialog.setCancelable(false);
alertDialog.setCanceledOnTouchOutside(false);
builder.setCancelable(false)
Put this line below the "builder.setView(view)" line so your dialog is not close when you touch outside the dialog or when you press back button your dialog is not closed.
You can add OnCancelListener to your dialog to listen Dialog cancellation and perform the screen reset.
builder.setOnCancelListener {
//call your method to reset the screen
}

How to implement an alert dialog in androidX [duplicate]

This question already has answers here:
You need to use a Theme.AppCompat theme (or descendant) with this activity
(63 answers)
Closed 3 years ago.
I have migrated my project to androidX, and I want to implement an alert dialog with positive and negative feedback from the user.
I am using this code:
AlertDialog.Builder builder1 = new AlertDialog.Builder(getApplicationContext());
builder1.setMessage("Write your message here.");
builder1.setCancelable(true);
builder1.setPositiveButton(
"Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Log.d("MSG", "onClick: YES");
}
});
builder1.setNegativeButton(
"No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
Log.d("MSG", "onClick: No");
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
But I get this error when running the app:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
You can use the MaterialAlertDialogBuilder provided by the Material Components library.
Just use:
new MaterialAlertDialogBuilder(context)
.setTitle("Dialog")
.setMessage("Write your message here. ....")
.setPositiveButton("Ok", /* listener = */ null)
.setNegativeButton("Cancel", /* listener = */ null)
.show();
The MaterialAlertDialogBuilder requires a Material theme and will result in an androidx.appcompat.app.AlertDialog.

How to change the color of buttons in Alert Dialog? [duplicate]

This question already has answers here:
How can I change default dialog button text color in android 5
(18 answers)
Closed 4 years ago.
i am working in a social app and while the user wants to edit their feeds i want to give them a pop up alert dialog from where the user can edit their post. I tried the following code but the result that gave was not good
i want to change the color and want a better design .. what can be done for this
private void EditCurrentPost(String description)
{
AlertDialog.Builder builder = new AlertDialog.Builder(ClickPostActivity.this);
builder.setTitle("Edit post");
final EditText inputField = new EditText(ClickPostActivity.this);
inputField.setText(description);
builder.setView(inputField);
builder.setPositiveButton("Update", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
ClickPostRef.child("description").setValue(inputField.getText().toString());
Toast.makeText(ClickPostActivity.this,"Post Updated",Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
Dialog dialog = builder.create();
dialog.show();
dialog.getWindow().setBackgroundDrawableResource(android.R.color.holo_purple);
}
There is a very simple way to make dialog with custom layout: Please review the code below:
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.dialog_news_description);//Your custom layout
TextView sometextview = dialog.findViewById(R.id.textView);// Textview in your custom layout
Button somebutton = dialog.findViewById(R.id.button_done);// Button in your layout
somebutton.setOnClickListener(new View.OnClickListener() {//on button click listener
#Override
public void onClick(View view) {
//DO your job....
//then...
dialog.dismiss();//dismiss the dialog
}
});
dialog.show();
You cannot change the default AlertDialog layout but you can inflate a custom layout to it. To start with you can check this post http://android-coding.blogspot.com/2011/07/create-custom-dialog-using.html

Android AlertDialog showing up blank [duplicate]

This question already has an answer here:
Dialog opens blank
(1 answer)
Closed 5 years ago.
I'm trying to make an AlertDialog in onCreateView() inside a Fragment. Here's the code I'm using:
AlertDialog.Builder builder = new AlertDialog.Builder(getContext()));
builder.setTitle("blah blah blah");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//do something
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
getActivity().onBackPressed();
}
});
builder.create().show();
But whenever this executes, I get a completely white dialog where nothing is visible. If I click around the box, I eventually find where the positive button is because the code in setPositiveButton's OnClickListener gets executed. Here's a screenshot:
As you can see, the rest of my app is in the background and I'm left with a useless dialog box. I've tried changing the theme, but that hasn't made any difference. I can't seem to find anyone else with this problem, so any information as to why this is happening is greatly appreciated!
Try this
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AlertStyle);
Add in Style.xml file
<style name="AlertStyle" parent="Theme.AppCompat.Light.Dialog">
<item name="android:colorAccent">#f3f3f3</item>
<item name="android:textColor">#f3f3f3</item>
<item name="android:textColorPrimary">#f3f3f3</item>
</style>
Remove the create() method from the builder.create().show() like that
builder.show();
Try this for all version
AlertDialog.Builder builder;
if(Build.VERSION.SDK_INT >= 21)
builder = new AlertDialog.Builder(this, android.R.style.Theme_Material_Light_Dialog_Alert);
else
builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle("title");
builder.setMessage("Message");
builder.create().show();

How to style AlertDialogs android

I need help with the dialog interface in android. i don't get it.. i' ve searched here to, i got many answers but with every code i used from here my application crashed..
So I make a notes app an you can choice a with an alert dialog which type of note you want.
The dialog window is black. So can someone show me how to chage the color maybe simple in white so that i understand how it works?
here is my code:
private void showNewNoteChoices() {
final CharSequence[] items = {
getResources().getString(R.string.text_note_type),
getResources().getString(R.string.log_note_type),
getResources().getString(R.string.important_type),
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select type of note");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
loadNoteFragment(item, newNoteTitles, null);
}
});
AlertDialog alert = builder.create();
alert.show();
}
I know that i have to make an xml file for the specific layout and i have to make an style . Can someone show me how I can make my dialog interface in white?
I would recommend you using Dialog. You can style it the way you want.
For example, you create a custom dialog with custom layout like this;
final Dialog dialog = new Dialog(MainActivity.this);
dialog .setContentView(R.layout.custom_layout);
Then you can create views and listeners;
Button button = (Button) dialog.findViewById(R.id.button);
//Other views and listeners etc..
Finally you show it;
dialog.show();

Categories