HukeLau_DABA
HukeLau_DABA

Reputation: 2526

Failed to convert @color/Blue into a drawable when setting background color

res/layout/activity_main.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:background="@color/Blue">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
     />
</RelativeLayout>

meanwhile, res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="Blue">#0000FF</color>
</resources>

as you can see, the color does start with #

The code is exactly as you see it.

Upvotes: 5

Views: 3924

Answers (2)

fubuki
fubuki

Reputation: 11

Do one thing instead of adding colour through colour.xml then to activity_main.xml just directly add the value of colour to activity_main.xml by its hash colour code directly For example in your caseandroid:background="#0000FF"

Upvotes: 0

HukeLau_DABA
HukeLau_DABA

Reputation: 2526

I have to go to File-->Invalidate Cache/Restart.

It amazes me that this bug still hasn't been fixed in Android Studio, searching on SO it looks like it has plagued other people in different ways.

Upvotes: 11

Related Questions