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>
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
I'm trying to do a custom circular seekbar but i haven't being able to achieve this shape yet.
The problem is basically that long circle shape because i did a custom circular seekbar before:
This is the code from the second picture:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="ring"
android:thicknessRatio="16"
android:useLevel="false">
<solid android:color="#color/black" />
</shape>
</item>
<item>
<rotate
android:fromDegrees="270"
android:toDegrees="270">
<shape
android:shape="ring"
android:thicknessRatio="16"
android:useLevel="true">
<solid android:color="#color/green" />
</shape>
</rotate>
</item>
</layer-list>
I've tried changing the size but it obviously give it a different form:
Does anyone have any clue for me to get this?
I have XML progressDrawable file for my ListView. It works well, but I found that if I use one Drawable XML file to make background for few object in one time, it works wrong.
Now I'm just created few XML Drawable files for each item from my ListView.
Now, my solution is like that:
for (int i = 0; i < 3; i++) {
View v1 = getActivity().getLayoutInflater().inflate(R.layout.item_results, null);
ProgressBar bar = v1.findViewById(R.id.MyDonut);
bar.setProgressDrawable(getResources().getDrawable(i==0?R.drawable.circle1:i==1?R.drawable.circle2:R.drawable.circle3));
listView.addHeaderView(v1);
}
Here, the circle1,circle2,circle3 - similar XML Drawable files. I think that my solution is too unflexible.
My circle:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="ring"
android:thicknessRatio="10"
android:useLevel="false">
<solid android:color="#color/orange" />
</shape>
</item>
<item>
<rotate
android:fromDegrees="270"
android:toDegrees="270"
android:pivotX="50%"
android:pivotY="50%">
<shape
android:shape="ring"
android:thicknessRatio="10"
android:useLevel="true">
<solid android:color="#color/green" />
</shape>
</rotate>
</item>
<item android:left="75dp" android:gravity="center">
<rotate android:pivotX="0%" android:fromDegrees="270" android:toDegrees="270">
<shape
android:shape="line"
android:useLevel="false">
<size android:width="75dp" android:height="75dp"/>
<stroke android:width="3dp" android:color="#color/light"/>
</shape>
</rotate>
</item>
<item android:left="75dp" android:gravity="center">
<rotate android:pivotX="0%" android:fromDegrees="270" android:toDegrees="630">
<shape
android:shape="line"
android:useLevel="false">
<size android:width="75dp" android:height="75dp"/>
<stroke android:width="3dp" android:color="#color/light"/>
</shape>
</rotate>
</item>
item-results just contains:
<ProgressBar
android:progress="0"
android:id="#+id/MyDonut"
android:layout_width="150dp"
android:layout_height="150dp"
android:background="#color/light"
style="?android:progressBarStyleHorizontal"/>
What about the other solutions of this issue?
Finaly, I found a good solution. We can use drawable.mutate() to set the other Drawable, with all properties from parent Drawable.
Documentation: https://developer.android.com/reference/android/graphics/drawable/Drawable#mutate()
It is possible to use this this way:
progressBar.setProgressDrawable(getResources().getDrawable(R.drawable.circle).mutate());
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
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>