I have a AlertDialog with a custom view that contains two EditText fields. At some point the dialog is show using the following code:
val builder = AlertDialog.Builder(this)
builder
.setView(R.layout.dialog)
.setMessage("message")
.setPositiveButton("Oke"){ dialog, id -> println("oke")}
.setNegativeButton("Cancel"){ dialog, id -> println("cancel")}
val dialog = builder.create()
dialog.show()
When the user pressed a EditText the SoftKeyboard show up but the Dialog is not being shifted up. I have tried settings the softinputmode of the dialog.window to SOFT_INPUT_ADJUST_PAN or SOFT_INPUT_ADJUST_RESIZE like this.
dialog.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)
but this doesn't seem to do anything.
The Activity in which the AlertDialog is shown had a translucent status bar theme.
So instead of initializing the builder with the classic AlertDialog.Builder(this) which uses the context's theme, you can use a dialog theme with no translucent status bar to it:
val builder = AlertDialog.Builder(this, android.R.style.Theme_DeviceDefault_Light_Dialog_NoActionBar)
This theme ensures compatibility with pre-lollipop devices too, but other ones like android.R.style.Theme_Material_Dialog should do the trick too.
Try this:
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
Related
When app displays dialog with long text via MaterialAlertDialogBuilder buttons are cropped on some devices.
my code:
MaterialAlertDialogBuilder(requireContext())
.setTitle(getString(R.string.placeholder))
.setMessage(getString(R.string.lorem_ipsum))
.setPositiveButton(getString(R.string.ok)) { dialog, _ ->
dialog.dismiss()
}
.show()
How do I make them displayed properly?
Screenshot
Screenshot from another device
As TheLibrarian mentioned, for longer texts you'll need to include a scrollable custom layout. But you can still use an AlertDialog.Builder and just call setView() with your scrollable TextView layout:
https://developer.android.com/develop/ui/views/components/dialogs#CustomLayout
Finally I got a solution - it happened to be caused by windowTranslucentStatus in my theme file, thanks to this i was able to continue using dialog with setMessage method for long messages.
I'm using an AlertDialog to show the result of a game. My problem is that the dialog seems to changes the depiction of my normal activiymain layout while the dialog is open.
That's the layout without the dialog
That's the layout while the dialog is open
As you can see the Textviews that i use as buttons are colored weirdly.
My Dialog is created like this:
AlertDialog.Builder = new AlertDialog.Builder(MainActivity.this);
turnout.setTitle("Dialog");
The mainactivity layout is a relative layout and the textviews are colored in #ffffff (meaning white)
By default Android dims the background (displays the "grey shadow" behind the dialog) whenever a dialog is displayed. Changing this default behavior may decrease the readability of a dialog and confuse a user.
That being said, you can disable the background by setting the parameter android:backgroundDimEnabled to false in your dialog's style.
You add the theme to your styles.xml:
<style name="NoDimAlertDialog" parent="ThemeOverlay.AppCompat.Dialog.Alert">
<item name="android:backgroundDimEnabled">false</item>
</style>
Make sure to inherit the default AlertDialog's style.
Additionally you can control the transparency of the dim, using android:backgroundDimAmount. This parameter takes a value from 0 (no dim) to 1 (background completely black). By default Android seems to use 0.6.
<style name="LessDimAlertDialog" parent="ThemeOverlay.AppCompat.Dialog.Alert">
<item name="android:backgroundDimAmount">0.25</item>
</style>
Next, pass the style name to theAlertDialog.Builder's constructor in your MainActivity.java:
new AlertDialog.Builder(this, R.style.NoDimAlertDialog)
.setTitle("Some title")
.setMessage("Some message")
.show();
You may as well consider using a Fragment to display your AlertDialog. Android comes with handy DialogFragment class for that scenario. A simple DialogFragment in your case may look like that:
class MyDialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getContext(), R.style.NoDimDialog)
.setTitle("Some title")
.setMessage("Some message")
.create();
}
}
Then you add your Fragment in MainActivity.java:
new MyDialogFragment().show(getSupportFragmentManager(), "DialogTag");
I have a dialog that comes up in my app and I wanted to stray away from using the default dialog, to give something slightly more customized. In my dialog layout, I included the following:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/My_Custom_Background">
This does basically what I want it to, it changes the background as expected. However, this only applies to the layout of the contents of the dialog box: the dialog also has a title and the title part of the dialog box is still the default Android theme, then everything under it is customized as I wanted. Is there away to extend the custom background to the entire dialog box?
You need to remove the title bar
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.dialog);
Make sure to call requestWindowFeature() before the setContentView() otherwise you get a FATAL EXCEPTION
you can create your dialog as below
Dialog mDialog = new Dialog(mContext);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
mDialog.setContentView(R.layout.your_custom_dialog_layout);
mDialog.setCancelable(false);
mDialog.show();
and inside your custom layout, you can set the custom drawable as a background.
An alternate is to remove your dialog titlebar using this...
yourdialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
and design whole dialog with title inside your layout...
I am working on an android application that allows user to add an edit text dynamically. I have been able to add the edit text using an alert dialog. But I need to make the dialog box transparent .I need to be able to see the imageview in the background. Color.Transparent wont work because my background is an image
Try this:
Dialog mDialog = new Dialog(mContext, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
or
mDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
You can try this
<EditText
android:id="#+id/purpose_textfield"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#null"/>
Instead of Using alert Dialog Box, i prefer you to use Dialog box:
And use this code:
Dialog dialog = new Dialog(getBaseContext, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
I want to use a custom color for the Dialog in ICS-Devices. The default one is blue (see image), how can I set this to a color that fits my app color scheme, e.g. red ?
Try this:
alert.setTitle(Html.fromHtml("<font color='#FF0000'>User Agent</font>"));
You can try this:
AlertDialog alert = builder.create();
alert.show();
alert.getListView().setBackgroundColor(Color.RED); //This is for Color
alert.setCancelable(true);