5

I am starting a new project, so the project is more or less "empty". I just added a MainActivity, with a MainActivityFragment. I haven't added any code at all to them.

Now, I edit the styles.xml so it looks like this:

<resources>
    <!-- Base application theme. -->
    <style name="MyTheme" parent="@android:style/Theme.Material.Light.DarkActionBar">
        <item name="android:windowNoTitle">true</item>
        <!--We will be using the toolbar so no need to show ActionBar-->
        <item name="android:windowActionBar">false</item>
        <!-- Set theme colors from http://www.google.com/design/spec/style/color.html#color-color-palette-->
        <!-- colorPrimary is used for the default action bar background -->
        <item name="android:colorPrimary">#2196F3</item>
        <!-- colorPrimaryDark is used for the status bar -->
        <item name="android:colorPrimaryDark">#1976D2</item>
        <!-- colorAccent is used as the default value for colorControlActivated
             which is used to tint widgets -->
        <item name="android:colorAccent">#FF4081</item>
        <!-- You can also set colorControlNormal, colorControlActivated
             colorControlHighlight and colorSwitchThumbNormal. -->
        <item name="toolbarStyle">@style/Widget.AppCompat.Toolbar</item>
    </style>
</resources>

So, I extend the Material theme as you can see.

I then create a toolbar xml file (mytoolbar.xml), like this:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/abc_ic_voice_search_api_mtrl_alpha"/>
</android.support.v7.widget.Toolbar>

And here is the problem:

enter image description here

I can't figure out why. I have done gradle sync, clean, rebuild, restarted IDE (Android Studio 1.2.1.1), nothing works.

Any ideas?

halfer
  • 19,824
  • 17
  • 99
  • 186
Ted
  • 19,727
  • 35
  • 96
  • 154

3 Answers3

1

There is a bug in android [pre-lollipop] OS which doesnt allow you to use attr in drawable. Here is the link to bug:

https://code.google.com/p/android/issues/detail?id=26251

Android dev team has released a fix but it works on android L and above. For workaround to this problem, refer to following solution:

How to reference style attributes from a drawable?

Community
  • 1
  • 1
Abhishek
  • 2,350
  • 2
  • 21
  • 35
  • 1
    Hmm, this is on the PC, when Im developing, its not in the Android OS, so Im not entirely sure what you mean =) The OS-version isnt relevant when coding, I can choose to target different SDK versions in the manifest/gradle files, right? Is that what you mean, the target SDK version is the issue? If I set it to 22, will it work then? Cause its not working for me. – Ted Jul 20 '15 at 09:39
  • you have also updated android OS to api 22 ? .. still not working. tried to change min api – Abhishek Jul 20 '15 at 09:45
  • The OS of the device will vary, of course. One device might be running 4.1, another 5.1 and everything in between. That should not affect the Android Studio. – Ted Jul 20 '15 at 09:54
  • well app is not crashing. its an android studio issue. – Abhishek Jul 20 '15 at 10:01
  • Following @Ted comment, I'm also having this in Android Studio 1.2.1.1 and using API 22, so it's not yet fixed. – Henrique de Sousa Jul 23 '15 at 17:01
1

Prepend android keyword to it.

?attr/colorPrimary"

becomes

?android:attr/colorPrimary

working fine in android studio 3.0 .

4b0
  • 21,981
  • 30
  • 95
  • 142
ZA BroadPeak
  • 29
  • 1
  • 9
0

There is problem on below Android OS version -21 .You can use

android:background="?attr/colorPrimary"

instead of

app:background="?attr/colorPrimary"

It will work fine.

Nitin Karande
  • 1,280
  • 14
  • 33