Search code examples
androidandroid-activityappcompatactivity

Activity vs AppCompatActivity: Difference in UI


I'm developing an Android app. In some cases I used an Activity and in others, an AppCompatActivity. Although I noticed one thing:

Lets take this class for instance:

public class LoginActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setTheme( android.R.style.Theme_Material_Dialog_NoActionBar_MinWidth);
        }
    }
}

In this case the output is the desired one:
enter image description here

When I change the extended class from Activity to AppCompatActivity the output changes to:
enter image description here

Why is this happening?


Solution

  • After a long research, I think it is or a bug or the Android theme decided to keep the dialog white also for the dark theme. The problem is the attribute colorBackground it seems NOT to be on background_material_dark which points to @color/material_grey_850.

    Here is my solution:
    Add to your color the folloging:

    <color name="background_material_dark_public">@color/material_grey_850</color>
    

    and set it. In my case:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    
        activity.setTheme(android.R.style.Theme_Material_Dialog_NoActionBar_MinWidth);
        int color = ResourcesCompat.getColor(activity.getResources(), R.color.background_material_dark_public, null); //without theme
        activity.findViewById(android.R.id.content).setBackgroundColor(color);
    }