Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set dialog fragment title to display from right

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

like image 600
oferiko Avatar asked May 06 '12 11:05

oferiko


People also ask

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

Is dialog fragment deprecated?

Stay organized with collections Save and categorize content based on your preferences. This class was deprecated in API level 28.

What method do you override when displaying a dialog?

To create a dialog window, override the onCreateDialog() protected method which is defined in the Activity base class.

What is DialogFragment used for?

DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.


2 Answers

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" />
like image 159
Samutz Avatar answered Oct 03 '22 20:10

Samutz


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.

like image 41
Ali Behzadian Nejad Avatar answered Oct 03 '22 19:10

Ali Behzadian Nejad