'Android : change the position of alert dialog icon

I have an alert dialog in android xml file.my alert dialog has an icon in the left side of it.I want to change the position of the icon to the right side. I dont want to use custum dialog too

like this :

enter image description here



Solution 1:[1]

For setting layout direction of alert dialog to RTL you can use OnShowListener method. after setting title , message , .... use this method.

 dialog = alertdialogbuilder.create();

        dialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dlg) {

          dialog.getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL); // set title and message direction to RTL
            }
        });
        dialog.show();

Solution 2:[2]

a) If you have it as a drawable in the dialog title (TextView), just use drawableRight

android:drawableRight="@drawable/yourIcon"

b) If it is an ImageView/ImageButton in a RelativeLayout, use alignParentRight

android:layout_alignParentRight="true"

c) If it is an ImageView/ImageButton in a LinearLayout, put it after the TextView

<LinearLayout
        android:layout_width="300dp"
        android:layout_height="300dp">

        <TextView
            android:text="some text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ImageView
            android:src="@drawable/yourIcon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

</LinearLayout>

Hope this helps you.

Solution 3:[3]

All you need is to create a style tag in res/values/styles and write :

<style name="alertDialog LayoutDirection">
    <item name="android:layoutDirection">rtl</item>
</style>

and then add this after alert dialog initializing in your activity :

alertDialog.setView(R.style.alertDialog);

for example :

AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
            alertDialog.setMessage("??? ????")
                    .setCancelable(true);
            alertDialog.setPositiveButton("???", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Toast.makeText(MainActivity.this, "yes", Toast.LENGTH_SHORT).show();
                }
            });
            alertDialog.setNegativeButton("??", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    Toast.makeText(MainActivity.this, "nooo", Toast.LENGTH_SHORT).show();
                }
            });

            alertDialog.show();
            alertDialog.setView(R.style.alertDialog);
            

Solution 4:[4]

Im afraid you have no other choice then customizing. The common dialogs are pretty static in UI changes. You can set another title, buttons or change the image but you can't reposition them.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Sharky
Solution 3 Hamed samsami
Solution 4 Sharky