Search code examples
javaandroidviewcrashandroid-theme

Android dialog theme makes setContentView crash the application


I'm applying this theme from manifest :

    <activity
        android:name=".ui.rate.MyActivity"
        android:theme="@android:style/Theme.Dialog">
    </activity>

With this the application crash on setContentView() of MyActivity. If I remove it there is no crash but I need this theme

In the activity onCreate() i'm doing this :

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_rate);
    getWindow().setBackgroundDrawableResource(android.R.color.transparent);

Even if I remove the first and third lines the applicatio ncrash the same way. It's really the dialog theme that's causing the crash

How can I set it so that Android accept it ?


Solution

  • You can use Theme.AppCompat.Dialog as the theme of your activity to avoid compatibility problem.
    The activity will be presented as a dialog.

    <activity
        android:name=".MainActivity"
        android:theme="@style/Theme.AppCompat.Dialog">
    </activity>
    

    As for the title, you can use setTitle("Hola!"); to change it.
    If you wanna remove the title, just call:

    supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
    

    for android:theme="@style/Theme.AppCompat.Dialog",
    and:

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    

    for android:theme="@android:style/Theme.Dialog".