2

I want to customize my title in my DialogFragment, I know how to How to set the title to my DialogFragment , but I want more not just a title , I need a customized title(color,fontSize ...), So I defined a TextView and customized it

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.height_fragment,container,false);

    TextView title =  new TextView(getContext());
    title.setText("Your Height Information");
    title.setGravity(Gravity.CENTER);
    title.setTextSize(30);
    title.setBackgroundColor(Color.GRAY);
    title.setTextColor(Color.WHITE);

    getDialog().setCustomTitle(title); //Error Can not resolve method setCustomTitle(android.widget.TextView)

    return view;
}

but there is an error on this line

getDialog().setCustomTitle(title);

getDialog().setCustomTitle(title);

I searched a lot before posting this question but I couldn't find how can I customize my title of DialogFragment. Thank you for taking time to help me

Sergey Emeliyanov
  • 5,158
  • 6
  • 29
  • 52
S S abbasi
  • 29
  • 1
  • 6

4 Answers4

4

"getDialog" does not have a method "setCustomTitle".This method can only be used for AlertDialog.Builder. Try it like this:

getDialog().setTitle("Your Height Information");
TextView textView=(TextView) getDialog().findViewById(android.R.id.title);
textView.setTextSize(30);
1

I never could easily customize the Title of my DialogFragment. So, instead, I use to remove the title and add a TextView to Dialog layout (and use it like a Title). This way, you can customize how you want and easily.

Dialog Code:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    // Remove TITLE
    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);


    View dialogView = dialogView = inflater.inflate(R.layout.height_fragment, null);

    // Do your stuff

    return dialogView;
}

height_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/dialog_title"
        android:layout_width="match_parent"
        android:layout_height="56dp"

        <!-- THIS VIEW IS YOUR TITLE... CUSTOMIZE LYKE YOU WANT -->
        android:textColor="@color/white"
        android:textSize="18sp"
        android:textStyle="bold"
        android:layout_gravity="top"

        android:text="@string/TITLE"
        android:gravity="center_vertical"
        android:paddingLeft="16dp" />

    <!-- Real Content goes below the title -->
</FrameLayout>
guipivoto
  • 18,327
  • 9
  • 60
  • 75
0

You need to connect your xml to your class. findViewById tells your class which TextView to use. Notice how it must match the android:id in the xml:

public class DialogFragmentTest extends DialogFragment {

    public final static String TITLE = "title";
    public final static String MESSAGE = "message";

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog, null);

        // Inflate and set the layout for the dialog
        // Pass null as the parent view because its going in the dialog layout
        builder.setView(view);

        Bundle args = getArguments();
        String title = args.getString(TITLE, "default title");
        String msg = args.getString(MESSAGE, "default message");

        TextView tvTitle = (TextView)view.findViewById(R.id.dialog_title);
        tvTitle.setText(title);

        TextView tvMessage = (TextView)view.findViewById(R.id.dialog_message);
        tvMessage.setText(msg);

        Button btnDone = (Button)view.findViewById(R.id.dialog_button);
        btnDone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });

        return builder.create();
    }
}

dialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="300dp"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/dialog_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="title"/>
    <TextView
        android:id="@+id/dialog_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/dialog_title"
        android:layout_marginTop="10dp"
        android:text="message"/>
    <Button
        android:id="@+id/dialog_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/dialog_message"
        android:layout_marginTop="20dp"
        android:layout_centerHorizontal="true"
        android:text="done"/>


</RelativeLayout>

This is then called from your activity with:

DialogFragmentTest bd = new DialogFragmentTest();
Bundle b = new Bundle();
b.putString(DialogFragmentTest.TITLE, "my title");
b.putString(DialogFragmentTest.MESSAGE, "my message");
bd.setArguments(b);
bd.show(getFragmentManager(), "my title");
bradkratky
  • 1,577
  • 1
  • 14
  • 28
0

Please, use custom dialog, based on DialogFragment:

public class MyCustomDialog extends DialogFragment{
    private Context context;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View dialogView = inflater.inflate(R.layout.custom_dialog, null, false);
        TextView tvTitle = (TextView) getDialog().findViewById(android.R.id.title);
        tvTitle.setTextSize(25);
        tvTitle.setTextColor(Color.GREEN);

        return dialogView;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        Dialog dialog = new Dialog(this.context, R.style.CustomDialog);
        dialog.setTitle("custom dialog title");
        dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        return dialog;
    }

    public static MyCustomDialog newInstance() {

        MyCustomDialog myCustomDialog = new MyCustomDialog();
        return myCustomDialog;
    }
}
Anh Pham
  • 2,108
  • 9
  • 18
  • 29