8

I'm writing an app in Hebrew (which is rtl). when I set my title for the dialog fragment it appears on the left. I tried to make it display on right by doing:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
{
    //set's up the title for this dialog fragment
    getDialog().getWindow().setGravity(Gravity.RIGHT);
    getDialog().setTitle(R.string.find_Address_text);

but it doesn't affect the position of the title. how can i achieve this? TNX

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
oferiko
  • 1,927
  • 2
  • 16
  • 17
  • I used this method: http://stackoverflow.com/questions/14439538/how-can-i-change-the-color-of-alertdialog-title-and-the-color-of-the-line-under/23278774#23278774 – mrd abd Apr 24 '14 at 20:12

2 Answers2

18

Hide the default title and make a custom one in the layout.

To hide the default title:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setStyle(STYLE_NO_TITLE, 0); // remove title from dialogfragment
}

Then add a TextView to the top of layout with your preferred styling:

<TextView
 android:id="@+id/dialog_title"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:gravity="right|center"
 android:text="@string/dialog_title" />
Samutz
  • 2,310
  • 4
  • 24
  • 29
  • The 2 cents question is: How would it be possible to theme this view like an actual dialog title? and use phone theme? – Waza_Be Jan 23 '13 at 15:50
  • @Waza_Be Personally I'm now using HoloEverywhere for my apps so that the overall theme is the same across the various APIs. Other than that the only thing I can think of is maybe you can make a copy of the title's view if you can find it (android.R.something) from the API and then set the gravity. I don't know for sure that that's possible though. – Samutz Feb 06 '13 at 17:28
  • i added getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); to remove title padding space – amorenew Sep 24 '16 at 17:01
6

I know this is an old thread with an answer. But for other people who may have the same question:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dlg = super.onCreateDialog(savedInstanceState);

    dlg.setTitle("Title");
    TextView titleTextView = (TextView) dlg.findViewById(android.R.id.title);
    titleTextView.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);

    return dlg;
}

This way we have title bar and we don't need to make a custom one.

Ali Behzadian Nejad
  • 8,804
  • 8
  • 56
  • 106