I have an annoying problem setting a color to a layout in Android;
This is how it looks my element in layout.xml (FrameLayout (painted element) in a LinearLayout ):
<LinearLayout
android:id="@+id/farPD_mid_linearLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent">
<!-- top margin layout-->
<FrameLayout
android:id="@+id/farPD_top_margin"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/white">
</FrameLayout>
I want to paint the FrameLayout in white and I do something like this in Java:
GradientDrawable gd3 = new GradientDrawable();
gd3.setColor(Color.parseColor("#ffffff"));
FrameLayout topBorder = (FrameLayout) findViewById(R.id.farPD_top_margin);
topBorder.setBackground(gd3);
I also tried with setColors(). Alpha is 255. The problem is that it doesn't paint in WHITE! it paints in some sort of grey :|.(*and nor it's the color set in layout). Plus, if i comment the Java code, and let only the color set in the layout, which is also white, I get the same output: FrameLayout painted in GREY!
Can someone help me please? Thank you!
Not 100% related to this question, but I thought I'd post what I found out for anybody out there still struggling. I was just having an issue with the setColors function of GradientDrawable and I stumbled upon this question hoping to find help. As you said, it wouldn't take the colors that I was giving it and would choose something intermediate. In my scenario, I have a list of Cards in a RecyclerView, and I'm assigning a different gradient color to the background of each. But it always seems as if the color of one Card would propagate to the other Cards for some reason.
After struggling with it for a bit, I ended taking a look at the documentation (which I probably should have done first) and found out you may need to call .mutate() on any instance of GradientDrawable before making any changes to it. That's because, unless you specify that the instance is mutable, it may be linked to other existing GradientDrawable instances and the changes made to one may propagate to the others.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With