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();
Related
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.
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()
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
I have an AlertDialog as below, I don't know how to test it with Robotium in Android Studio. Can anyone give me a hint for that?
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder.setTitle("Select");
final String[] items = {"Take a picture using carmera", "Choose a picture from Album"};
alertDialogBuilder.setItems(items, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if (i == 0) {
...
...
See this answer to a similar question:
This works for me:
solo.clickOnView(solo.getView(android.R.id.button1));
where the 'Positive' button is android.R.id.button1, the 'Negative'
button is android.R.id.button2 and 'Neutral' is android.R.id.button3.
It means that for your AlertDialog you would need to use the solo.clickOnView(solo.getView(dialogId)) method.
Check out also this answer to a similar question:
lets say you have some code like this
solo.clickOnView(view1);
solo.clickOnView(view2);
and you know the dialog can appear between these two steps of your test, you can place in code that is something like:
if(solo.waitForView(dialogView, 1000, false)){
solo.clickOnView(dialogDismissButton);
solo.clickOnView(view2) //retry the step above
}
I created a dialogBox in my Android application.i want to show with image in that dialog Box.But i Cannot create image.kindly help me.
Thanks in Advance
here my Coding;
public void createbtnteam_adelaide()
{
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setMessage("What kind of Banner do you Want to Create?");
//my new code
ImageView image = (ImageView) alertDialog.findViewById(R.drawable.team_brisbane);
image.setImageResource(R.drawable.team_brisbane);
alertDialog.setButton("Text Only", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// here you can add functions
}
});
alertDialog.setButton2("Text+Image", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// here you can add functions
}
});
alertDialog.show();
}
}
i have Small icon in team_gwsid(imageid).
The Android Developer site has an excellent article on how to create dialogs in Android, including custom dialogs. I think you solve your problem by having a look at that, it's pretty easy to do.
Here's the link: http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog
By the way (not related to this question): I see you've asked six questions here on StackOverflow, and haven't accepted any of them. This is usually what you do when someone helps you to solve your problems.
There ia a alertDialog.setView(View view) method that enables you to set a custom view.
This does not affect the buttons or the titlebar.
In standard dialog the icon is shown only if you set title too. You have to use setTitle to see result of setIcon. Icon is shown in title of the dialog.
You can use Set icon or SetTitle to Display the Images in DialogBox. Read this Tutorial
public void createbtnteam_adelaide()
{
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setIcon(R.drawable.team_adelaide);
alertDialog.setTitle("What kind of Banner do you Want to Create?");
alertDialog.setButton("Text Only", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// here you can add functions
}
});
alertDialog.setButton2("Text+Image", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which) {
// here you can add functions
}
});
alertDialog.show();
}