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);
}
}
Related
i create this xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<gradient
android:startColor="#color/light_blue"
android:centerColor="#color/dark_blue"
android:endColor="#color/light_purple"
>
</gradient>
<corners
android:radius="30dp">
</corners>
</shape>
</item>
</selector>
and i trying to set this file to my button background.but problem is background dose not accept colors of xml file.it is use backgroundTint colors and not the colors i set above.
THIS IS MY ACTIVITY_XML
<Button
android:id="#+id/temp_l"
android:layout_width="250dp"
android:layout_height="33dp"
android:background="#drawable/temp_shape"
app:layout_constraintBottom_toTopOf="#+id/scrollView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.143" />
i searched in youtube and saw that eveyone did what i did but they get result and im not.
sorry about my english.its not very well.
I guess you are using android:backgroundTint="#null" with android namespace, but you need to set app:backgroundTint="#null" with app namespace instead
Side note:
You should wrap the xml drawable into a layer-list as long as you have no need to the selector, so you might change it to:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient
android:centerColor="#055D93"
android:endColor="#color/light_purple"
android:startColor="#color/light_blue" />
<corners android:radius="30dp">
</corners>
</shape>
</item>
</layer-list>
Preview
I'm in the process of developing a compound view that I'm trying to get to look like the following image. I've marked (with a red arrow) the part that I'm having trouble with):
However, at present I'm unable to get the arc to cleanly start and finish (as shown below):
My code for this inner (dashed) progress drawable is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/secondaryProgress">
<rotate
android:fromDegrees="90"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="90">
<clip>
<shape
android:shape="oval"
android:useLevel="true">
<size
android:width="60dp"
android:height="60dp" />
<stroke
android:width="13dp"
android:color="#android:color/darker_gray"
android:dashWidth="3dp"
android:dashGap="5dp" />
</shape>
</clip>
</rotate>
</item>
<item android:id="#android:id/progress">
<rotate
android:fromDegrees="90"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="90">
<clip>
<shape
android:shape="oval"
android:useLevel="true">
<size
android:width="60dp"
android:height="60dp" />
<stroke
android:width="13dp"
android:color="#android:color/white"
android:dashWidth="3dp"
android:dashGap="5dp" />
</shape>
</clip>
</rotate>
</item>
</layer-list>
The surrounding progress drawables that I've made work fine with android:shape="ring" and removing the <clip> tags, but I can't seem to get the same clean clockwise progress behaviour using an android:shape="oval".
In case it helps, my code for setting the drawable to my ProgressBar is as follows:
<ProgressBar
android:id="#+id/progressbar_dashed_gauge"
style="#style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="150dp"
android:layout_height="150dp"
android:indeterminate="false"
android:max="100"
android:scaleX="0.75"
android:scaleY="0.75"
android:progress="80"
android:progressDrawable="#drawable/dashed_gauge"
android:secondaryProgress="80"
/>
Any help with getting this dashed ProgressBar to show progress in a manner similar to my other ProgressBar views would be incredibly appreciated!
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 !
I have a rectangle box that I set the spinner background as but I would like to keep the down arrow that comes along with the spinner. Here is my code for the rectangle:
<?xml version="1.0" encoding="UTF-8" ?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff" />
<stroke android:width="2dp" android:color="#80000000" />
<padding android:left="2dp" android:top="2dp" android:right="2dp" android:bottom="2dp" />
See the picture below:
On the left is what i currently have, on the right is what I'm trying to achieve or at least have a down arrow in there somewhere. Any ideas on how to create this background?
I checked your code with the help of uiautomator and the spinner dropdown arrow does not show up with the background set.
You could use a LayerList and have a bitmap with gravity right. Now the bitmap is part of the background set to spinner.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff"/>
<stroke
android:width="2dp"
android:color="#80000000"/>
<padding
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp"/>
</shape>
</item>
<item>
<bitmap
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:src="#mipmap/ic_launcher" />
</item>
</layer-list>
Result :
Of course there is no border around the bitmap. You can check this link How to change the spinner background design and color for android? and see if it fits your requirement
Add Spinner inside a RelativeLayout.
In my case, the icon disapper because of parent layout's height and width.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="30dp"
android:background="#drawable/startbg"
android:layout_marginLeft="5dp"
>
<Spinner
android:id="#+id/timerSpin"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:spinnerMode="dropdown"
android:backgroundTint="#color/white"
android:layout_centerInParent="true"
android:textColor="#color/white"
android:pointerIcon="arrow"
/>
</RelativeLayout>
Check the below XML code to achieve what you need. It is similar to what Raghunandan posted / answered, but with some minor tweaks. Hope this will help you,
<?xml version="1.0" encoding="UTF-8" ?>
<selector xmlns:android = "http://schemas.android.com/apk/res/android">
<item>
<layer-list>
<item>
<shape android:thickness="2dp">
<gradient android:angle = "90" android:endColor = "#ffffff" android:startColor = "#ffffff" android:type = "linear"/>
<stroke android:width = "1dp" android:color = "#80000000"/>
<corners android:radius = "5dp"/>
<padding android:bottom = "3dp" android:left = "3dp" android:right = "3dp" android:top = "3dp"/>
</shape>
</item>
<item>
<rotate
android:fromDegrees = "90"
android:pivotX = "85%"
android:pivotY = "50%"
android:toDegrees = "90">
<shape
android:shape = "line"
android:thickness="2dp"
android:top = "1dp">
<stroke
android:color="#80000000"
android:width = "1dp"/>
</shape>
</rotate>
</item>
<item>
<bitmap
android:gravity = "center|right" android:src = "#drawable/abc_btn_check_to_on_mtrl_015"/>
</item>
</layer-list>
</item>
</selector>
use the required drawable in the placeholder images i added in the code.
Hope this will help you,
Good Luck
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