I am trying to create a button in android with gradient + alpha effect but not getting it exactly what I wish to do.
Here is the image of the button which i would like to get :-
I am using the below selector for my button style & effects :-
<?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:angle="180" android:centerColor="#657377" android:endColor="#AFFFFFFF" android:startColor="#FFFFFFFF" android:type="linear"/>
<corners android:radius="10dp" />
</shape></item>
</selector>
If anyone has any idea please kindly help me. Thanks
Related
Good evening everyone!
Is there a way to make my buttons look pressed without having to design two times each button?(one for pressed state, another for not pressed). I've designed the buttons' shapes using http://angrytools.com/android/button/, and using these shapes as the background for the buttons. I've heard about ImageButton but it doesnt seem to apply here as I need the buttons to have text.
Example of one of my background shapes:
Thanks very much in advance!
Try This Code, set this XML file as background to a button.
but_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#9df20901" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="#892379" />
</shape>
</item>
</selector>
I am trying to make an XML Button, circle shape, which is filled with a gradient color. This color needs to be changed programatically.
Here is an example of what I am trying to achieve.
My attempts to build this have ended up with a gradient background and a solid colored circle. Instead of the gradient circle and transparent background.
I am hoping to be able to use more then 2 colors or a radial gradient for the circle as well. Any help is greatly appreciated.
You can Try with making Drawable XML. Example
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="circle" >
<gradient
android:angle="90"
android:centerColor="#555994"
android:endColor="#b5b6d2"
android:startColor="#555994"
android:type="linear" />
</shape>
Arrange angle based on your requirement.
If you wants to change Color progarmatically then read this:-
How do I programmatically set the background color gradient on a Custom Title Bar?
Belated but can be helpful for someone else
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<gradient android:angle="0" android:endColor="#fff" android:gradientRadius="#dimen/_30sdp" android:startColor="#color/colorPrimaryDark" android:type="radial" />
<size android:width="#dimen/_25sdp" android:height="#dimen/_25sdp" />
</shape>
</item>
Output
Late but someone might get helped. Thanks to #Faizan
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<gradient android:angle="270" android:endColor="#D10719" android:gradientRadius="120dp" android:startColor="#0A5A1B" android:type="linear" />
<size android:width="100dp" android:height="100dp" />
</shape>
</item>
</selector>
Output:
Here when i pressed on these left and right arrow button at that time I want to see these type of effects on button. These same thing happens in Iphone/IOS by default.
Can i make this type of effect?
Here i mentioned the pic what exactly i want.
Here i used this xml files but didnt got success.
button_pressed.xml
<?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:angle="180" android:centerColor="#657377" android:endColor="#AFFFFFFF" android:startColor="#FFFFFFFF" android:type="linear"/>
<corners android:radius="10dp" />
</shape></item>
</selector>
EDIT
I used android:background="#drawable/button_pressed.xml" line but i did not got which i want Exactly.
Tried :
I used this xml file as per Piyush's Answer but i didnt get success and i am getting this effect.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="#color/transparent" />
</shape>
</item>
<item>
<shape
android:innerRadiusRatio="4"
android:shape="ring"
android:thicknessRatio="9"
android:useLevel="false" >
<gradient
android:endColor="#00000000"
android:gradientRadius="250"
android:startColor="#ffffffff"
android:type="radial" />
</shape>
</item>
</layer-list>
I agreed top and bottom part cuts because i have limited space in layout for this button. that we will think later but why its not taking effect like shadow and alpha and all that same like which i mentioned?
OUTPUT :
Please help me If any body has idea about this.
Thanks in Advance.
The shape has to be radially drawn with fading from white to black outwards.
Try this:
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:startColor="#ffffffff"
android:endColor="#00000000"
android:gradientRadius="100"
android:type="radial"/>
</shape>
Also, you cannot directly set this as a background to your button. You will have to create a selector drawable file in which you specify different backgrounds based on the state of the button. In this case, you need this background to be set only when button is pressed. The selector will look something like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/button_pressed"/>
<item android:state_selected="true" android:drawable="#drawable/button_pressed"/>
<item android:state_focussed="true" android:drawable="#drawable/button_pressed"/>
<item android:drawable="#android:color/transparent" />
</selector>
PS: It is not recommended to replicate iOS design when designing an Android app. For Android design guidelines, please refer: http://developer.android.com/design/building-blocks/buttons.html
can try this regarding for circular shape, can set your colors accordingly.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="#color/transparent" />
</shape>
</item>
<item>
<shape
android:shape="ring"
android:useLevel="false" >
<gradient
android:endColor="#00000000"
android:gradientRadius="200"
android:startColor="#ffffffff"
android:type="radial" />
</shape>
</item>
Is there anyway to change the appearance of a button when the user clicks on it? for example change the color or make it dark? Like using CSS in ASP.net to hover over a word or a link.
Try this. Save this as a drawable xml resource and set this as a background of your button. Please explore the other options of Selector class.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false"><shape>
<gradient android:angle="270" android:endColor="#BB908C8B" android:startColor="#BB908C8B" />
<corners android:bottomLeftRadius="9dp" android:topRightRadius="9dp" />
</shape></item>
<item android:state_pressed="true"><shape>
<gradient android:angle="270" android:endColor="#BB54504F" android:startColor="#BB54504F" />
<corners android:bottomLeftRadius="9dp" android:topRightRadius="9dp" />
</shape></item>
</selector>
Look at the link http://android.onyou.ch/2011/01/25/simple-custom-button-using-a-selector-xml-layout/
I believe State List is what you're looking for.
I am quite new to Android dev, but not Java dev, so doing the logic behind a button is no issue, but styling it is not as easy as css. I have read a couple tutorials on shapes / styles so I kind of know how to do the custom borders and round corners, but I was hoping to see some really good quality examples, like the buttons on the Twitter app http://i.stack.imgur.com/Gip2s.png or the 'debossed' ones on the facebook app.
What I suppose I am really interested in, are using shadows to create effects. Are these done with images, or can you style this in the app?
thanks
for rounded corners create a shape drawable eg. ronded_corner.xml and the angle must be a multiple of 45 degree
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#SomeGradientBeginColor" android:endColor="#SomeGradientEndColor"
android:angle="225"/>
<corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp" android:topRightRadius="7dp"/>
</shape>
then set this Background by android:background:#drawable/ronded_corner
Whenever you create the button in the layout just set the background property of the button as XML, Put this XML file in the drawable folder.
sample XML code i will post here. you can modify and learn in that only.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="270"
android:startColor="#B80000"
android:endColor="#900405"
android:type="linear"
/>
<stroke android:width="1dp" android:color="#900405"/>
</shape>
For rounded corner button, define inset as shown below and adjust radius.
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="4dp"
android:insetTop="6dp"
android:insetRight="4dp"
android:insetBottom="6dp">
<ripple android:color="?attr/colorControlHighlight">
<item>
<shape android:shape="rectangle"
android:tint="#0091ea">
<corners android:radius="10dp" />
<solid android:color="#1a237e" />
<padding android:bottom="6dp" />
</shape>
</item>
</ripple>
</inset>
For more information http://www.zoftino.com/android-button