I have a button that I want to be round, so I made an xml file and set it as its background. The button is now round but I want to be able to change its color programmatically instead of hardcoding it into the xml file. How can I do this?
Here is my xml file for round button.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#ffcb05"/>
</shape>
You can change color of view by using ColorFilter. It is very easy and quick.
button.getBackground().setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
This code will color button into red color.
Try to get button background set color using getPaint :
((ShapeDrawable)yourbutton.getBackground()).getPaint().setColor(getResources().getColor(R.color.colorToSet));
Try this:
GradientDrawable backgroundShape = (GradientDrawable)btn.getBackground();
backgroundShape.setColor(Color.BLACK);
Related
I have a set of buttons in Android which all use the same custom drawable as the background, When a button is pressed, I'd like the drawable that is used for the background to change to a different custom drawable. The code I used to do this is below:
selectedButton.setBackgroundResource(R.drawable.roundedbuttongreen)
// Change the background resource to the roundedbuttongreen drawable
the XML for the custom drawable called roundedbuttongreen.xml is:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="10dp">
<solid android:color="#4CAD71"/> <!-- this one is ths color of the Rounded Button -->
<corners
android:bottomLeftRadius="50dp"
android:bottomRightRadius="50dp"
android:topLeftRadius="50dp"
android:topRightRadius="50dp"/>
</shape>
The problem that occurs is that the colour of the button is changed but not to the correct colour the colour it changes to instead is grey, it should be green. The screenshot is below:
image of the button after the background resource has been changed, it should be green
If anyone could let me know what I'm doing wrong that would be great
I suggest you to add checked status on custom shape like this then add this shape as a background for your buttons .
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button"
android:state_checked="false"/>
<item android:drawable="#drawable/roundedbuttongreen"
android:state_checked="true"/>
</selector>
I hope it will work with you .
I had a look through my xml code for my layout and in particular my buttons, The issue was that the buttons backgroundTint property had a value which was grey, as a result when I tried to change the background drawable, the colour of the button was always grey.
To fix the issue, I changed the value of the background tint to green and the issue was resolved
After the call of EditText.setBackgroundColor(Color.RED) I see only a red rectangle with text inside it, but I want to change only the text background and to keep the standard bottom border which is grey when the field has no focus and blue if it has focus. Can I do it programmatically?
I want to change the text background to red if the input is invalid and to transparent if it is valid.
I think the best way is to work with drawable/shape.xml as background and upon logic code situation call EditText.setBackground(some_other_shape.xml). Here is an example for shape file xml demonstrating how to use:
Border color ("stroke"): In this example some custom color from colors.xml
Fill color ("solid"): In this example Android default transparent color
Even image icon inside
<!--Example for custom shape file xml design-->
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#mipmap/icon_image" />
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#android:color/transparent" />
<stroke android:width="1dp" android:color="#color/orange_primary" />
</shape>
</item>
</layer-list>
So just prepare several shapes for each situation you need. Another approach is to work with layout params etc. but I think this one is faster and gives more control for custom design in easy to understand code.
I want to remove the underlining decoration when I am inputting text into my edit text. And something more, is it possible to get rid of this padding or margin that is between the inputted text and the blue border below it?
Right now the edittext looks like this:
Create a edit_text_background.xml in your drawables folder with following code:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#android:color/white"></solid>
</shape>
Then simply set android:background="#drawable/edit_text_background" field to the same drawable file.
editText.setBackroundDrawable(null);
I'm trying to draw LinearLayout with round corner.
I want changing this layout's color in runtime, using Colorfilter.
but ColorFilter can't apply layout, only apply view..
I don't know how can achieve this.
draw LinearLayout wiht round corner
and change layout's color in runtime
how can I do that?
I should be very grateful to you if you might help me :)
(Sorry for short English, because I'm foreigner :| )
I think you need to change the background color of linear layout with round corner dynamicaly right?
GradientDrawable gd = new GradientDrawable();
gd.setColor(Color.parseColor("Your color code"));
gd.setCornerRadius(60);
your_layout.setBackgroundDrawable(gd);
If you take this example it shows how to apply a round corner and shadow using an xml drawable:
Android LinearLayout : Add border with shadow around a linearLayout
You can apply this background to your layout during runtime with
yourLayout.setBackgroundResource(int resid);
Please follow this code,
//this code in your activity, java file
LinearLayout linearLayout=(LinearLayout)findViewById(R.id.your_linear_layout_id);
linearLayout.setBackgroundResource(R.drawable.drawable_file_in_xml);
//drawable_file_in_xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient android:startColor="#444444" android:endColor="#202020" android:angle="-90"/>
<corners android:radius="2dp"/>
</shape>
In this code you can set round corner <corners android:radius="2dp"/>.
How do i get the button styles of the standard next/previous buttons and the gradient backgrounds like in the image below
gradient backgrounds like in the image below
Hi, for gradient backgrounds you can apply below Background Drawable code.
Change color codes according to your needs !
Create gradientbg.xml and paste code given below,
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#817f81"
android:endColor="#ccc9cc"
android:angle="90" />
</shape>
this will make gradient background that you need !
Thanks.
Got it.. extracted both from the AccountsAndSyncSettings.apk :) Its part of the Android system, so i guess there shouldn't be any copyright issues right?
You can add the image to the button using android:drawableRight attribute of <Button>