Emaborsa
Emaborsa

Reputation: 2870

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?

Upvotes: 2

Views: 6281

Answers (2)

Emaborsa
Emaborsa

Reputation: 2870

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);
}

Upvotes: 0

praveen2034
praveen2034

Reputation: 119

AppCompatActivity - Provides Material color themes, widget tinting, and app bar support to earlier devices. Use of this class requires that you use Theme.AppCompat themes for consistent visual presentation.

For more info look into this url : https://developer.android.com/topic/libraries/support-library/features#v7-appcompat

Simply : AppCompact activity uses Theme that why its changing

Activity wont have action bar where as AppCompactActivity will have action bar by default.

Upvotes: 2

Related Questions