How can I create my own theme for alertDialog?

2019-02-20 02:08发布

I want to create a different theme for all of alertDialog instances. I need my own title view instead of the usual black title background. All text should have a blue color, and set the edge of the alertDialog to a round shape.

Is it possible to create our own theme for alert dialogs, using any style or creating a class which extends AlertDialog.Builder? I need a common theme or style for my all instances of alertDialog. I am using alertDialog in many places - one for singleChoice items, one with ArrayAdapter.

My alertDialog with array adapter:

String[] items = {"Edit profile","Change user","Change password","Logout"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Loged.this,
                    R.layout.my_spinner_layout, items);

settingMenu.setAdapter(adapter, listener); 

My alertDialog with single choice items:

alertDelete = new AlertDialog.Builder(getParent());
                    alertDelete.setTitle("Delete");
                    alertDelete.setCancelable(true);
                    CharSequence[] choice = {"this user","All user"};
                    alertDelete.setSingleChoiceItems(choice, 0,

For my all alertDialog, I need a common theme, like:

enter image description here

5条回答
Animai°情兽
2楼-- · 2019-02-20 02:32

I solved my problem by creating a class which extends Dialog, and I created my own functions. for example setMessage,setTitle,setPositiveButton,setNegativeButton etc.

But am confusing on how we can replace the setAdapter() and setSingleChoice().

查看更多
迷人小祖宗
3楼-- · 2019-02-20 02:36

please check this link. here am creating my own alertDialog class and its a simple method if you reuse the alertDialog in many situation in your application

how Create setAdapter() for a AlertDialog

查看更多
Anthone
4楼-- · 2019-02-20 02:38

Here one example For Dialog:

button = (Button) findViewById(R.id.button1);

            button.setOnClickListener(new OnClickListener() {

              @Override
              public void onClick(View arg0) {

                // custom dialog
                final Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.custom);
                dialog.setTitle("Give a Title...");

                // set the custom dialog components - text, image and button
                TextView text = (TextView) dialog.findViewById(R.id.text);
                text.setText("Put here any custom text!");
                ImageView image = (ImageView) dialog.findViewById(R.id.image);
                image.setImageResource(R.drawable.ic_launcher);

                Button dialogButton = (Button) dialog.findViewById(R.id.ButtonOK);
                // if button is clickedthen dialog will closed
                dialogButton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                dialog.show();
              }
            });

Custom AlertDialog

    AlertDialog.Builder builder;
    AlertDialog alertDialog;
    Context mContext;
    LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    View urlayoutfile = inflater.inflate(R.layout.custom_dialog_xmlfile,(ViewGroup)findViewById(R.id.Yourlayout_id));
    builder = new AlertDialog.Builder(this);
    builder.setView(urlayoutfile);
    alertDialog = builder.create(); 
    alertDialog.show();
查看更多
劫难
5楼-- · 2019-02-20 02:40

You can use layout as Different themes, styles, Backgrounds

customdialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    style="@android:style/Theme.Black.NoTitleBar.Fullscreen"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:id="@+id/layoutsample"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/dialoghdrbg"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/dialogheaderimage1"
                android:layout_width="50dp"
                android:layout_height="48dp" 
                android:scaleType="fitXY"
                />

            <TextView
                android:id="@+id/dialogheadertext1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical|right"
                android:layout_marginLeft="1dp"
                android:gravity="center_vertical|right"
                android:textColor="#000000"
                android:textSize="25dp"
                android:textStyle="bold" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@drawable/dialogcontentbg"
            android:orientation="vertical" android:gravity="center">

            <TextView
                android:id="@+id/dialogmessgetext1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:textColor="#ffffff"
                android:textSize="23dp" 
                android:text="" 
                android:gravity="center" 
                />


            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center|center_horizontal"
                android:gravity="center|center_horizontal"
                android:layout_marginTop="20dip" 
                android:orientation="horizontal">

              <Button
                    android:id="@+id/dialogokbutton1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="15dp"
                    android:background="@drawable/buttonanimation"
                    android:text="Retry"
                    android:textSize="20dp"
                    android:textStyle="bold" />

                <Button
                    android:id="@+id/dialogcancelbutton1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"
                    android:background="@drawable/buttonanimation"
                    android:text="Report"
                    android:textSize="20dp"
                    android:textStyle="bold" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

customDialog.java

final Dialog favDialog = new Dialog(Myclass.this,
            android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);


favDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    favDialog.getWindow().setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    favDialog.setContentView(R.layout.reloadurlpopuplayout);

    favDialog.setCancelable(false);

    ImageView dialogImage = (ImageView) favDialog
            .findViewById(R.id.dialogheaderimage1);

    dialogImage.setBackgroundResource(R.drawable.questionmark);

    TextView dialogMesage = (TextView ) favDialog
            .findViewById(R.id.dialogmessgetext1);
    TextView dialogHeader = (TextView) favDialog
            .findViewById(R.id.dialogheadertext1);

    String descText = getString(R.string.RetryReportMessage);


    dialogMesage.setBackgroundColor(0x00000000);

    dialogMesage.setText(descText);

    dialogHeader.setText(R.string.BrockenLinkHeader);

    try {
        favDialog.show();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
查看更多
仙女界的扛把子
6楼-- · 2019-02-20 02:44

To use a specific style, use

ctw = new ContextThemeWrapper(this, R.style.MyStyle);
new AlertDialog.Builder(ctw)
    .setTitle(...)
查看更多
登录 后发表回答