Home » Android » problems with alertDialog

problems with alertDialog

Posted by: admin November 1, 2017 Leave a comment


ok, I have been working on this alert dialog for a few days.

I have figured out how to do some of it but I am having problems with custom buttons.

my title is almost in the middle of the pop-up, I need it to me at the top.

my main problem is the buttons. everything that I try wont let me change them.

I want to be able to add backgrounds, borders along with other stuff.

here is the code that I am using

my function I am calling is this…

 public void userNameRequired(View view
    AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.MyDialogCustomOne));
                TextView newMessage = new TextView(this);
                newMessage.setText(" User Name is Required to be between 7 and 20 caracters long.");
                //add more functions here if wanted.

                TextView title = new TextView(new ContextThemeWrapper(this, R.style.MyTitleOne));

                //Button button = new Button(ContextThemeWrapper(this, R.style.MyCustomButton));

This is what I was using… trying to change it up.

        //        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener()
        //        {
        //            public void onClick(DialogInterface dialog, int id)
        //            {
        //                //add stuff here if you want the ok button to do something.
        //            }
        //        });



my style.xml….

<style name="MyDialogCustomOne" parent="@android:style/Theme.Dialog">
            <item name="android:background">@color/Navy</item>


        <style name="MyCustomButton">
            <item name="android:background">@color/Silver</item>
            <item name="android:text">"OK"</item>


        <style name="MyTitleOne">
            <item name="android:background">@color/Blue</item>
            <item name="android:text">"!! NOTICE !!"</item>
            <item name="android:textColor">@color/Red</item>
            <item name="android:textSize">30dp</item>
            <item name="android:gravity">center</item>


I could use some help trying to custom this alert dialog please.

I am no expert but I am learning and can use all the help I can get.

thank you…


You can custom your elements inside your xml and use the inflater to get the view to pass at your builder.

First, get the LayoutInflater :

LayoutInflater inflater = getLayoutInflater();

Then, create your custom layout, example :

your_custom_layout.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:text="Hello World!" 


Then, set your custom view to your builder :
builder.setView(inflater.inflate(R.layout.your_custom_layout, null))