Home » Android » problems with alertDialog

problems with alertDialog

Posted by: admin November 1, 2017 Leave a comment

Questions:

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.");
                //newMessage.setGravity(Gravity.CENTER_HORIZONTAL);
                //add more functions here if wanted.
                builder.setView(newMessage);

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

                //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.
        //            }
        //
        //        });


                builder.show();


           }
        }

my style.xml….

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


        </style>

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


        </style>

        <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>


        </style>

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…

Answers:

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:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" 
        />

</LinearLayout>

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