Hamidreza Shokouhi
Hamidreza Shokouhi

Reputation: 1031

Wrong background color in some android devices

I'm developing an android application. I've defined a color in my resource as this:

  <color name="toolbar_color">#ffcb00</color>

I've set this color as background in one of layouts such as 'Relative layout' and this color is shown correctly in most of android devices. BUT in samsung model S6310 android 4.2.1, I have a trouble. My color is not shown and instead of showing this color, android device shows transparent color. I've searched about this problem and the solution is using java code to set color for my layout. but I have many flavor builds and I don't want to change my java code.

Now I have 2 questions:

1- Why is this happening?

2- What's solution for this poblem?

Upvotes: 1

Views: 2828

Answers (3)

Q2x13
Q2x13

Reputation: 534

Well to answer your question in simple terms

Why?

Well, this is simply the work of the Android Support Library. Android Support Library, now that it is backward compatible it should handle that. It should make the Application read those resources properly. But sometimes it doesn't. That mainly causes this. You can read more about how to set resources if something like that happens here and here.

Work-arounds Well there are many workarounds to this,

  1. Set the resource colour directly in the XML instead of referencing to a resource.

  2. Also you can set the resource colour or the resource using JAVA Code.

  3. It also states that adding

    android {
        ...
        defaultConfig {
            ...
            vectorDrawables.useSupportLibrary = true
        }
        ...
      }
    

to the build.gradle solves the problem. Check here.

Anyway, better do it programatically.

Upvotes: 2

Hamza
Hamza

Reputation: 684

if its on android version you should check the version and set the color there

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLYBEAN) {
// Your required color
} else {

}

Hope this helps if its the problem with the android version.

Upvotes: 0

Coder
Coder

Reputation: 843

You can use Android Studio Theme Editor and try changing background color. You can get more info on Theme editor here and here.

Upvotes: 0

Related Questions