Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hex color values in Android layouts are not being displayed accurately

I have a background image that has a consistent color all along the right side. I want the background color of the Activity to match so that the image can display full height with the correct aspect ratio for the width (anchored from the left). I don't want the color switch that is currently showing:

Imgur

I used a color dropper tool to get the hex value of the purple used in the image (the left side of the screenshot). It's hex value is #4A2D70. I then created a color resource:

<color name="purpleBackground">#4A2D70</color>

and set it as the background color of the RelativeLayout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/purpleBackground">

The graphical layout preview matches the screenshot above, and when I run this in the emulator or on the device, the purple used for the RelativeLayout that shows is not the same hex value as the one I specify. I used the color dropper tool again to compare from the emulator, and this is what I get: #391C5D

I've also tried playing around with alpha settings and setting the RelativeLayout background to white and using this purpleBackground as the background color of the ImageView.

To reiterate my main question: Why don't the hex color values translate directly? Is there a solution or workaround?

Edit: This is the theme I'm using:

<style name="CustomTheme" parent="@android:Theme.Holo.Light">
  <item name="android:actionBarStyle">@style/CustomTheme.ActionBar</item>
  <item name="android:editTextStyle">@style/CustomTheme.EditText</item>
</style>
like image 688
Ben Jakuben Avatar asked Dec 01 '25 03:12

Ben Jakuben


2 Answers

Try it with white #FFFFFF and you should be able to see if the color is being interpreted wrong, or if the picker is giving you the wrong value.

like image 94
David C Adams Avatar answered Dec 03 '25 00:12

David C Adams


I never did figure out why this didn't work, but I did use a simple and effective workaround with a background image. I simply copied a very thin slice of the right side of the background image for the Activity. Then I set it as the android:background attribute of the RelativeLayout that holds the ImageView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="@drawable/background_slice" >

Side note: the ImageView is set as follows to match height and maintain its aspect ratio. It's anchored to the left side so I end up with the space to fill on the right side, which is where the background_slice shows through.

<ImageView
    android:id="@+id/backgroundImageView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:scaleType="fitStart"
    android:src="@drawable/background"
    android:contentDescription="@string/content_desc_background" />
like image 26
Ben Jakuben Avatar answered Dec 03 '25 00:12

Ben Jakuben