This question already has answers here:
How to change the color of a CheckBox?
(26 answers)
Closed 9 years ago.
I have been coding an Android Application, and using CheckBox for GUI.
Generally, the style of CheckBox is a green colored tick, but I want to make it red colored cross.
How to do it.? Please Help.
You can set any color of tick mark of Checkbox. but you need to make the custom checkbox for that.
Look here and here.
Source : Android: Set color of CheckBox
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#drawable/cbchk_blue"
android:state_focused="false">
<shape android:shape="rectangle">
<stroke android:width="2dp" android:color="#color/blue_dark" />
<gradient android:endColor="#color/white" android:startColor="#color/blue_dark" android:type="sweep" />
<size android:height="30dp" android:width="30dp" />
</shape>
</item>
<item android:state_checked="true"
android:drawable="#drawable/cbchk_blue"
android:state_focused="true">
<shape android:shape="rectangle">
<stroke android:width="2dp" android:color="#color/white" />
<gradient android:endColor="#color/white" android:startColor="#color/blue_dark" android:type="sweep" />
<size android:height="30dp" android:width="30dp" />
</shape>
</item>
<item android:state_checked="false"
android:drawable="#drawable/cbunchk_blue"
android:state_focused="false">
<shape android:shape="rectangle">
<stroke android:width="2dp" android:color="#color/blue_dark" />
<size android:height="30dp" android:width="30dp" />
</shape>
</item>
<item android:state_checked="false"
android:drawable="#drawable/cbunchk_blue"
android:state_focused="true">
<shape android:shape="rectangle">
<stroke android:width="2dp" android:color="#color/white" />
<size android:height="30dp" android:width="30dp" />
</shape>
</item>
Take a Custom Drawable selector with one image for unchecked state and another for checked state and set it as drawable resource for your Checkbox
Example:
Create a xml file in res/drawable folder
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="true" android:drawable="#drawable/crossimagecheckbox" />
<item android:state_checked="false" android:drawable="#drawable/uncheckecheckbox" />
</selector>
You need to create a selector for checkbox.
in layout checkbox add the following:
android:background="#drawable/checkbox_selector"
in checkbox_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
android:drawable="#drawable/normal_picture" />
<item android:state_checked="true"
android:drawable="#drawable/checked_picture" />
</selector>
Now create normal_picture and checked_picture according to your requirement.
I believe this link is highly relevant. Read Jean's answer:
Checkboxes being children of Button you can just give your checkbox a
background image with several states as described here, under "Button
style":
http://developer.android.com/reference/android/widget/Button.html
...and exemplified here:
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
Related
i want to have a half drawable ring just like an arc.
i have a code that just creates the ring but i am confused how to make it a half ring.
how do i achieve this half ring drawable?
<shape android:shape="ring"
android:innerRadius="15dp"
android:thickness="20dp"
android:useLevel="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/blue"/>
<size android:height="200dp" android:width="200dp"/>
</shape>
This is the image that exactly looks like what am trying to achieve.
click here
you can create like this
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:end="0dp"
android:left="-500dp">
<shape>
<corners android:radius="500dp" />
<solid android:color="#color/colorPrimary" />
</shape>
</item>
<item
android:end="100dp"
android:bottom="100dp"
android:top="100dp"
android:start="-400dp">
<shape>
<corners android:radius="500dp" />
<solid android:color="#color/white" />
</shape>
</item>
</layer-list>
if you went to perfect view then create svg file for this
Here's what I have :
Buttonshape.xml :
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#FF008AB8"/>
<stroke android:color="#0299D0"/>
<corners android:radius="15dp"/>
</shape>
</item>
<item android:drawable="#drawable/b_popcorn"></item></layer-list>
BlueFragment.xml :
<Button
android:id="#+id/BtePlay" ...
android:background="#drawable/Buttonshape"
/>
Everything is working greate, my button corner is rounded and it apply the drawable, but I have a question :
Is it possible to add more items in my buttonshape.xml and select a specific drawable in my BlueFragment.xml
For example doing something like this :
Buttonshape.xml :
...
<item android:drawable="#drawable/b_popcorn"></item></layer-list>
<item android:drawable="#drawable/b_2"></item></layer-list>
and in my BlueFragment.xml :
<Button
android:id="#+id/BtePlay" ...
android:background="#drawable/Buttonshape|b_popcorn"
/>
<Button
android:id="#+id/BtePlay2" ...
android:background="#drawable/Buttonshape|b_2"
/>
Something like this to set only one drawable for each button instead of making 50+ xml files for each buttons ...
Thanks
we have only option that is using selector, but its only depend on view state
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/bg_checked" android:state_checked="true"/>
<item android:drawable="#drawable/bg_unchecked" android:state_checked="false"/>
</selector>
so I think we can not do any thing like you expected, so sorry for that !
this is my first time posting. And I've been using this website as a great resource.
But I have come upon an issue. I've tried applying a shadow effect to my ImageButton but it doest work? What I'm trying to achieve is the shadow you can see on the floating bubble (Material Design). I have a circle ImageButton, but I wish to add a shadow around it. How can I achieve that?
This is circle_button.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#C62666"/>
</shape>
This is circle_button_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#E92D6C"/>
</shape>
This is my selector which combines both drawable's into one. button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/circle_button"
android:state_focused="true"
android:state_pressed="true" />
<item android:drawable="#drawable/circle_button_pressed"
android:state_focused="false"
android:state_pressed="true" />
<item android:drawable="#drawable/circle_button_pressed"
android:state_focused="true" />
<item android:drawable="#drawable/circle_button"
android:state_focused="false"
android:state_pressed="false" />
</selector>
And this is how I apply it to my ImageButton
<ImageButton
android:id="#+id/btn_apply"
android:layout_width="68dp"
android:layout_height="68dp"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/scrollView1"
android:layout_marginRight="45dp"
android:layout_marginTop="94dp"
android:adjustViewBounds="true"
android:background="#drawable/button"
android:scaleType="fitCenter"
android:src="#drawable/apply_two" />
So again? How can I add a shadow to my button? Thanks for your time.
may help you :
You can use a Button:
<ImageButton
android:id="#+id/fab"
android:background="#drawable/ripple"
android:stateListAnimator="#anim/anim"
android:src="#drawable/ic_action_add"
android:elevation="4dp"
/>
where the ic_action_add is your icon.
drawable/ripple.xml is:
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="?android:colorControlHighlight">
<item>
<shape android:shape="oval">
<solid android:color="?android:colorAccent" />
</shape>
</item>
</ripple>
anim/anim.xml is:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="true"
android:state_pressed="true">
<objectAnimator
android:duration="#android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueFrom="#dimen/button_elevation"
android:valueTo="#dimen/button_press_elevation"
android:valueType="floatType" />
</item>
<item>
<objectAnimator
android:duration="#android:integer/config_shortAnimTime"
android:propertyName="translationZ"
android:valueFrom="#dimen/button_press_elevation"
android:valueTo="#dimen/button_elevation"
android:valueType="floatType" />
</item>
</selector>
Dimens.xml is :
<resources>
<dimen name="fab_size">56dp</dimen>
<dimen name="button_elevation">2dp</dimen>
<dimen name="button_press_elevation">4dp</dimen>
With the elevation attribute you should set the Outline via code:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layoutfab);
//Outline
int size = getResources().getDimensionPixelSize(R.dimen.fab_size);
Outline outline = new Outline();
outline.setOval(0, 0, size, size);
findViewById(R.id.fab).setOutline(outline);
}
}
I'm currently developing an android app where I use transparent png's as buttons for the user interface.
The buttons look kinda like this:
When the user presses the button I want to automatically tint the non-transparent pixels in the image to a darker color.
Currently I use an xml selector with different drawables for each state. This obviously doesn't scale well since I need to make several versions of each image in photoshop.
Any solutions? I heard that you can use the setColorFilter method on ImageView's to achieve this, but a full explanation would be great!
Thanks!
set this image as source to ImageButton and set ImageButton's state (backgriund) using xml state list, something like this
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape
>
<gradient
android:startColor="#android:color/background_light"
android:endColor="#android:color/darker_gray"
android:angle="90"/>
<stroke
android:width="1dp"
android:color="#999999" />
<corners
android:radius="9dp"/>
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp"/>
</shape>
</item>
<item android:state_focused="true">
<shape>
<gradient
android:endColor="#android:color/transparent"
android:startColor="#android:color/transparent"
android:angle="270"/>
<stroke
android:width="1dp"
android:color="#989797" />
<corners
android:radius="9dp"/>
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp"/>
</shape>
</item>
<item>
<shape>
<gradient
android:endColor="#android:color/transparent"
android:startColor="#android:color/transparent"
android:angle="90"/>
<stroke
android:width="1dp"
android:color="#999999" />
<corners
android:radius="9dp"/>
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp"/>
</shape>
</item>
</selector>
I'm trying to change the color of a horizontal progress bar (foreground). I came across this example and am trying to model my XML file off it. However, I get a compiler error at the following statement:
myProgressBar.setProgressDrawable(R.drawable.progress_horizontal);
The error is "The method setProgressDrawable(Drawable) in the type ProgressBar is not applicable for the arguments (int)."
I believe the reason is inside the R.java file I see the following line:
public static final int progress_horizontal=0x7f02002f;
So, do I define this XML file as a drawable and not an integer, or is there another way to solve this?
Thanks.
Edit: Including XML file
<item android:id="#android:id/background">
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#ff9d9e9d"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:angle="270"
/>
</shape>
</item>
<item android:id="#android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#80ffd300"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:angle="270"
/>
</shape>
</clip>
</item>
<item android:id="#android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#ffffd300"
android:centerColor="#ffffb600"
android:centerY="0.75"
android:endColor="#ffffcb00"
android:angle="270"
/>
</shape>
</clip>
</item>
The R class is generated by Android compiler, all fields of the inner class in R.java refer to the resources in res folder by android:id xml attribute. The Resouce class can retrieve the resource object by using the resource id. So, you can get Drawable object through Resource.getDrawable(int).
Call this on your progress bar xml element
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background"> //---this is progress background
<shape>
<corners android:radius="5dip" />
<gradient
android:startColor="#E6E6E6"
android:centerColor="#E6E6E6"
android:centerY="0.75"
android:endColor="#E6E6E6"
android:angle="270"/>
</shape>
</item>
<item android:id="#android:id/progress"> //----this is progress status
<clip>
<shape>
<gradient
android:startColor="#FF0040"
android:centerColor="#FFFF00"
android:endColor="#00FF00"
android:angle="0" /> //-This varies colors linearly
</shape>
</clip>
</item>
</layer-list>