Abe Abe - 9 months ago 100
Android Question

Right to left single choice list dialog

How to make single choice list dialog right to left? More specifically I use rtl language and want the radio button to be placed and aligned at right side. Should I implement a custom dialog, a custom adapter or there is much simpler solution?
Any help or suggestions is appreciated.

Answer Source

Building dialog code:

final String items[] = {"1", "2"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("title")
       .setSingleChoiceItems(new ArrayAdapter<String>(this,
                                R.layout.rtl_list_item,
                                R.id.text, items),
                             0, 
                             new DialogInterface.OnClickListener() {
                                 @Override
                                 public void onClick(DialogInterface dialog, int which) {

                                 }
                    });

builder.create().show();

Now create your layout file, Your custom xml in your layout folder will be as

layout/rtl_list_item.xml

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical|right"
    android:textDirection="rtl"
    android:textAlignment="gravity"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:drawableRight="?android:attr/listChoiceIndicatorSingle"
/>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download