Issue
I have this Button in my Relative Layout:
<Button
android:id="@+id/gpsButton"
android:layout_width="50sp"
android:layout_height="50sp"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/tableLayout1"
android:background="@drawable/round_button"
android:gravity="center_vertical|center_horizontal"
android:text="GPS"
android:textColor="#ffffff" />
It is linked to this shade:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#FF0000"/>
<stroke android:width="2sp" android:color="#fff" />
</shape>
My question is how can I change the colour from the button in my activity?
I've tried it but the app crashed -.- Here is a snippet of my code:
RelativeLayout relLayout = (RelativeLayout) findViewById(R.id.reli);
GradientDrawable bgShape = (GradientDrawable) relLayout.getBackground();
bgShape.setColor(Color.parseColor("#FFFF00"));
What do I wrong? I don't understand how to do this with the view.
Thank you in advance :)
Solution
You can easily change the background color of your Button
programatically by adding a color filter. For that, set a white background in the XML file. Then, to make the button red, you can do this:
gpsButton.getBackground().setColorFilter(Color.RED, Mode.MULTIPLY);
The red
and white
will be multiplied which will result as red
. Of course, you can replace the Color.RED
by the color you want.
Answered By - G.T.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.