Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Custom AlertDialog

I have created a custom alertdialog by the following code:

AlertDialog.Builder builder;
AlertDialog alertDialog;

LayoutInflater inflater = (LayoutInflater)ActivityName.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_layout,(ViewGroup)findViewById(R.id.layout_root));

                builder = new AlertDialog.Builder(getParent());
                builder.setView(layout);
                alertDialog = builder.create();
                alertDialog.show();

Problem is the pop up is surrounded with the default Dialog background having a own void space of a title(as the title is not set). How do i remove this. I have tried putting custom style through ContextThemeWrapper like builder = new AlertDialog.Builder(new ContextThemeWrapper(getParent(), R.style.CustomDialogTheme));

But its not working. How do i do that?!!! Thanks in Advance. Custom style xml is given below:

<style name="CustomDialogTheme" parent="android:style/Theme.Dialog.Alert">
            <item name="android:windowIsFloating">false</item>
            <item name="android:windowNoTitle">true</item>
        </style>

This is the output on the emulator

like image 456
IronBlossom Avatar asked Jan 21 '12 11:01

IronBlossom


People also ask

How can I see custom AlertDialog in android?

If you want a custom layout in a dialog, create a layout and add it to an AlertDialog by calling setView() on your AlertDialog. Builder object. By default, the custom layout fills the dialog window, but you can still use AlertDialog. Builder methods to add buttons and a title.

What is custom dialogue box in android?

The custom dialog uses DIALOG to create custom alert in android studio. Dialog display a small window i.e a popup which draws the user attention over the activity before they continue moving forward. The dialog appears over the current window and display the content defined in it.

How do you create a dialog in Kotlin?

Open Android Studio and import the starter project. Create a new class CustomDialog. kt to contain our dialog. Our CustomDialog is initialized with a context as it is needed by AlertDialog.

How many types of dialogs are there in android?

There are two types of dialogs: basic and full-screen.


2 Answers

use following

Dialog dialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar);

Inflate your layout and set the view to content of dialog and

dialog.setContentView(view);
like image 150
jeet Avatar answered Sep 21 '22 04:09

jeet


AlertDialog dialog = new AlertDialog.Builder(this)
.setView(getLayoutInflater().inflate(R.layout.custom_dialog, null))
.create();

In order to listen for UI events:

View view = getLayoutInflater().inflate(R.layout.custom_dialog, null);
Button btn = (Button)view.findViewById(R.id.the_id_of_the_button);
btn.setOnClickListener(blah blah);
AlertDialog dialog = new AlertDialog.Builder(this)
  .setView(view)
  .create();
like image 33
NagarjunaReddy Avatar answered Sep 23 '22 04:09

NagarjunaReddy