2

My current Alert Dialog looks like the one of the right, I want it to look like the one on the left.

enter image description here

I think I can do that by changing the theme of the custom Dialog, but I am not sure if that is correct, and if it is so, then how to proceed? Is there anyway I can customly change the positive/negative button?

I dont want to create my custom dialog as the way the button show in alert dialog is perfect for me.

Something like the one on the right in http://developer.android.com/guide/topics/ui/dialogs.html

This is what I have done so far,

    AlertDialog.Builder builder = new AlertDialog.Builder(LoginSignupActivity.this);
    LayoutInflater inflater = getLayoutInflater();  
    builder.setView(inflater.inflate(R.layout.newusersignup, null)).setTitle("Sign up");
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {}
    //etc

and my xml looks like

<TextView
    android:id="@+id/newUsersTxtName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Enter your name"
    android:textColor="@color/white"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/newUserTxtPassword"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/newUsersName"
    android:text="Enter password"
    android:textColor="@color/white" />

<EditText
    android:id="@+id/newUsersName"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/newUsersTxtName"
    android:ems="10"
    android:textColor="@color/white"
    android:inputType="text" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/newUserPassword"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/newUserTxtPassword"
    android:ems="10"
    android:textColor="@color/white"
    android:inputType="textPassword" />

<EditText
    android:id="@+id/newUserEmail"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/newUserTxtEmail"
    android:ems="10"
    android:textColor="@color/white"
    android:inputType="textEmailAddress" />

<TextView
    android:id="@+id/newUserTxtEmail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/newUserPassword"
    android:text="Enter your email"
    android:textColor="@color/white"
    android:textAppearance="?android:attr/textAppearanceSmall" />

Akshat Agarwal
  • 2,837
  • 5
  • 29
  • 49

4 Answers4

2

I would make the changes via a custom style in "styles.xml" e.g.

    <?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AlertDialogCustom" parent="@android:style/AlertDialog">
    <item name="android:textColor">#00FF00</item>
    <item name="android:typeface">monospace</item>
    <item name="android:textSize">10sp</item>
</style>

Source

It is also possible to do so programmatically:

AlertDialog.Builder customBuilder = new AlertDialog.Builder(new ContextThemeWrapper(this,android.R.style.Theme_Dialog));

customBuilder.setTitle(R.string.popup_error_title);
customBuilder.setNegativeButton("Exit application", new DialogInterface.OnClickListener() {  
                public void onClick(DialogInterface dialog, int which) {  
                    MyActivity.this.finish();
                }  
        });
AlertDialog dialog = customBuilder.create();
    dialog.show();

Button b = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
if(b != null)
        b.setBackgroundDrawable(getResources().getDrawable(R.drawable.my_button))

Source

EDIT: the styles.xml file will be placed in the \main\res\values folder

Community
  • 1
  • 1
doydoy
  • 4,021
  • 3
  • 20
  • 33
1

You can define your own AlertDialog style as doydoy did, then simply create AlertDialog.Builder in this way:

AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.style_name);

Otherwise, you can try to define your own app theme and override android:alertDialogStyle without touching the code to get a uniform user experience. https://sites.google.com/site/androidhowto/how-to-1/customize-alertdialog-theme

Alexander Zhak
  • 9,140
  • 4
  • 46
  • 72
Questing
  • 19
  • 3
0

use this method and you are able to change size color and some other features

dialog = alertdialogbuilder.create();

        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dlg) {
                dialog.getButton(Dialog.BUTTON_POSITIVE).setTextSize(20); // set text size of positive button
                dialog.getButton(Dialog.BUTTON_POSITIVE).setTextColor(Color.RED); set text color of positive button

                dialog.getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL); // set title and message direction to RTL
            }
        });
        dialog.show();
Hamid
  • 51
  • 7
-1

This worked for me, its basically applying the Holo theme to my particular activity

res/values/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
        <style name="default_activity_theme" parent="@android:style/Theme.Holo"/>
</resources>

AndroidManifest.xml

<activity android:name="MyActivity"
          android:theme="@style/default_activity_theme"/>
Akshat Agarwal
  • 2,837
  • 5
  • 29
  • 49
  • I am getting downvotes on my solution above. This is a three year old question (and answer) so i suspect either Android has changed such that this is no longer valid, or someone is just deciding to go on a downvoting spree. – doydoy Nov 03 '16 at 11:25