When Should One Use Theme.Appcompat VS Themeoverlay.Appcompat

When should one use Theme.AppCompat vs ThemeOverlay.AppCompat?

Per this Theme vs Style blog post by the creator of AppCompat:

[ThemeOverlays] are special themes which overlay the normal Theme.Material themes, overwriting relevant attributes to make them either light/dark.

ThemeOverlay + ActionBar

The keen eyed of you will also have seen the ActionBar ThemeOverlay derivatives:

  • ThemeOverlay.Material.Light.ActionBar
  • ThemeOverlay.Material.Dark.ActionBar

These should only be used with the Action Bar via the new actionBarTheme attribute, or directly set on your Toolbar.

The only things these currently do differently to their parents is that they change the colorControlNormal to be android:textColorPrimary, thus making any text and icons opaque.

Android You need to use a Theme.AppCompat theme (or descendant) with the design library

try this changes:

in gradle:

compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support:support-v4:25.2.0'
compile 'com.android.support:design:25.2.0'

Also in manifest add the appcompat theme to your Navaigation drawer `Activity

<activity android:name=".activity.FormActivity"
android:theme="@style/Theme.AppCompat">
<intent-filter>
<action android:name="info.androidhive.navigationdrawer.activity.FormActivity" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

You need to use a theme.appcompat theme with Theme.AppCompat as theme, Sdk v23

Problem partially solved.

The guilty line is:

at android.support.v7.app.k.onCreate(AppCompatDelegateImplBase.java:124)

I use these separate ProGuard files to configure ProGuard. Maybe I didn't point to the correct /app/proguard directory. Thus, ProGuard ignored myapp-rules.pro where I say to keep those android.support.v7.**classes.

After fixing this, I had the error:

:app:proguardDebug
Warning: com.paypal.android.sdk.aT: can't find referenced field 'org.apache.http.HttpEntity wrappedEntity' in program class com.paypal.android.sdk.aT
Warning: uk.co.senab.photoview.PhotoViewAttacher: can't find referenced method 'float sqrt(float)' in library class android.util.FloatMath
Warning: uk.co.senab.photoview.gestures.CupcakeGestureDetector: can't find referenced method 'float sqrt(float)' in library class android.util.FloatMath
Warning: there were 1 unresolved references to program class members.
Your input classes appear to be inconsistent.
You may need to recompile the code.
(http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedprogramclassmember)
Warning: there were 2 unresolved references to library class members.
You probably need to update the library versions.
(http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedlibraryclassmember)
Exception while processing task java.io.IOException: Please correct the above warnings first.
at proguard.Initializer.execute(Initializer.java:473)
at proguard.ProGuard.initialize(ProGuard.java:233)
at proguard.ProGuard.execute(ProGuard.java:98)
at proguard.gradle.ProGuardTask.proguard(ProGuardTask.java:1074)
at com.android.build.gradle.tasks.AndroidProGuardTask.doMinification(AndroidProGuardTask.java:139)
at com.android.build.gradle.tasks.AndroidProGuardTask$1.run(AndroidProGuardTask.java:115)
at com.android.builder.tasks.Job.runTask(Job.java:48)
at com.android.build.gradle.tasks.SimpleWorkQueue$EmptyThreadContext.runTask(SimpleWorkQueue.java:41)
at com.android.builder.tasks.WorkQueue.run(WorkQueue.java:227)
at java.lang.Thread.run(Thread.java:745)
:app:dexDebug
:app:crashlyticsStoreDeobsDebug
:app:crashlyticsUploadDeobsDebug
:app:validateDebugSigning
:app:packageDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:packageDebug'.
> Unable to compute hash of
/home/guillaume/workspace/MyApp/app/build/intermediates/classes-proguard/debug/classes.jar

So I 'fixed' the warnings by adding rules

-dontwarn com.paypal.android.sdk.**
-dontwarn uk.co.senab.photoview.**

Yet the problem seems deeper, as PhotoView and Braintree/Card.io (also PayPal SDK) use methods removed with API 23. The ball is not in my count, is it?

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity

u are using the wrong context you have to use the activity context and not the application context

change:

  new AlertDialog.Builder(getApplicationContext())

to

  new AlertDialog.Builder(YourActivityName.this)

Fatal error (use theme.appcompat), even though I am using the AppCompat theme

From my experience, this could be 1 of 2 things (based on the samples you've provided):

  1. When I switched to appcompat, I had this issue a lot, until I found this line on the Android Developers Blog regarding the release of the appcompat support lib You can see the blog post Here:

Migration from previous setup
For most apps, you now only need one theme declaration, in values/:

values/themes.xml:

<style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
<!-- Set AppCompat’s actionBarStyle -->
<item name="actionBarStyle">@style/MyActionBarStyle</item>

<!-- Set AppCompat’s color theming attrs -->
<item name=”colorPrimary”>@color/my_awesome_red</item>
<item name=”colorPrimaryDark”>@color/my_awesome_darker_red</item>

<!-- The rest of your attributes -->
</style>

You can now remove all of your values-v14+ Action Bar styles.

It obviously doesn't say you HAVE to remove those other 'values' folders, but doing so helped me.


  1. If you're using appcompat, make sure your activity classes extend
    ActionBarActivity rather than Activity as mentioned in the same blog
    post:

New integration If you are not currently using AppCompat, or you are starting from scratch, here's how to set it up:

  • All of your Activities must extend from ActionBarActivity, which extends from FragmentActivity from the v4 support library, so you can
    continue to use fragments.

  • All of your themes (that want an Action Bar/Toolbar) must inherit from Theme.AppCompat. There are variants available, including Light

    and NoActionBar.

  • When inflating anything to be displayed on the action bar (such as a SpinnerAdapter for list navigation in the toolbar), make sure you
    use the action bar’s themed context, retrieved via

    getSupportActionBar().getThemedContext().

  • You must use the static methods in MenuItemCompat for any action-related calls on a MenuItem.

Hopefully one of these helps :)

EDIT:

After looking at your project, I see multiple version folders (mdpi-v11, transitions-v21, things like that). Appcompat doesn't work with these for a reason I have yet to discover. Get rid of your version folders, keep all the base ones, and you're good to go.

How do I style appcompat-v7 Toolbar like Theme.AppCompat.Light.DarkActionBar?

The recommended way to style the Toolbar for a Light.DarkActionBar clone would be to use Theme.AppCompat.Light.DarkActionbar as parent/app theme and add the following attributes to the style to hide the default ActionBar:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

Then use the following as your Toolbar:

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>

For further modifications, you would create styles extending ThemeOverlay.AppCompat.Dark.ActionBar and ThemeOverlay.AppCompat.Light replacing the ones within AppBarLayout->android:theme and Toolbar->app:popupTheme. Also note that this will pick up your ?attr/colorPrimary if you have set it in your main style so you might get a different background color.

You will find a good example of this is in the current project template with an Empty Activity of Android Studio (1.4+).

ActionBar works only with ThemeOverlay

Change your MainActivity to extend AppCompatActivity then get your action bar with this code:

 final ActionBar actionBar = getSupportActionBar();

Refactor your imports and you should be golden.



Related Topics



Leave a reply



Submit