Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android DialogFragment with AppCompat theme issue

I've trying to create DialoFragment with AppCompat theme, but when i use AppCompat theme, dialog title is not shown.

I'am using defined style:

<style name="DialogFragment" parent="Theme.AppCompat.Light.Dialog"/>

When parent theme will be changed to:

<style name="DialogFragment" parent="android:Theme.Material.Light.Dialog"/>

or

<style name="DialogFragment" parent="android:Theme.Holo.Light.Dialog"/>

title is displaying properly.

Code of my dialog:

public class InfoDialog extends DialogFragment {

    public static final String TAG = InfoDialog.class.getName();

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.getWindow().setTitle(getString(R.string.dialog_title));

        return dialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogFragment);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.info_dialog, container, false);
    }
}

Any ideas what is causing the issue? Application use com.android.support:appcompat-v7:22.2.0, maybe this is platform bug?

like image 282
Wilek Avatar asked Jun 15 '15 20:06

Wilek


People also ask

Is an AppCompat widget that can only be used with a theme AppCompat?

AppCompatTextView is an AppCompat widget that can only be used with a Theme.

Is DialogFragment deprecated?

This class was deprecated in API level 28. Use the Support Library DialogFragment for consistent behavior across all devices and access to Lifecycle.

How do I destroy DialogFragment?

tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog. Dismiss the fragment and its dialog.

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.


2 Answers

Try extending AppCompatDialogFragment instead of DialogFragment. That should do the trick.

like image 152
jeka Avatar answered Oct 10 '22 05:10

jeka


Theme.AppCompat.Light.Dialog sets no title window in default.

Try something like below:

<style name="DialogFragment" parent="Theme.AppCompat.Light.Dialog">
   <item name="android:windowNoTitle">false</item>
</style>
like image 23
Sergey Avatar answered Oct 10 '22 04:10

Sergey