Custom Button with Background color - android

I want to create four custom buttons(As Roundedrects in iOS) on the top of the screen.
Using the Custom xml with shape tag , I can get it .
Custom.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#F9FAFC" />
<corners android:radius = "20dp"/>
</shape>
I am using it as:
android:background = "#drawable/custom"
But I am getting only the color(grey) set as in the custom xml for all the buttons, which shouldn't.
I want to set different Hexa(#FF0000 ... ) colors to that custom buttons.After lot of research on this site, I think it will be better to rise a question.
Any of you geeks can point the link or please help me with some code , how to achieve above requirement.
PLEASE NOTE: I don't need three states of the button as Pressed, Focused, Disabled. I already have four buttons with Square Shape. Now I want to create them as Rounded Rect shape to make it look Attractively.As for the case I can't create 4 CUSTOM files in drawable folder for 4 BUTTONS. Because in future my requirement may extend to some 8-10 buttons or even more .*
Thank you.

you need to use item tags. here is an example containing button states, solid and gradient colors, strokes, paddings and corner radius. you need to put this into selector tags.
<item android:state_enabled="false" >
<shape>
<solid
android:color="#474141" />
<stroke
android:width="1dp"
android:color="#474141" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item android:state_pressed="true" >
<shape>
<solid
android:color="#f3ae1b" />
<stroke
android:width="1dp"
android:color="#f3ae1b" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#990000"
android:endColor="#990000"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#990000" />
<corners
android:radius="4dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>

Related

Android - How to change only the background color of a button and keep the rest of button default style?

I have looked at pretty much every answer here and I can't find something that changes only the background color of my button but keeps all the rest of the Button default values.
I have tried using the PorterDuff solution many answers suggested:
button.getBackground().setColorFilter(0x39B44A, PorterDuff.Mode.MULTIPLY);
but it made the green color I wanted a very dark green color for some reason.
So I tried making a drawable file solution as was suggested by that high voted answer:
<?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="#color/green"
android:endColor="#color/green"
android:angle="270" />
<stroke
android:width="3dp"
android:color="#color/green" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item android:state_focused="true" >
<shape>
<gradient
android:endColor="#color/green_lightest"
android:startColor="#color/green_lighter"
android:angle="270" />
<stroke
android:width="3dp"
android:color="#color/green_lighter" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:endColor="#color/green"
android:startColor="#color/green"
android:angle="270" />
<stroke
android:width="5dp"
android:color="#color/blue"/>
<corners
android:radius="5dp" />
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"/>
<solid
android:color="#color/green"/>
</shape>
</item>
</selector>
But this solution doesn't work for me because I'm using other default looking buttons so this solution has me eyeballing the look of my custom button by having to experiment with different dp values in my custom button drawable file to match the default look. Here's an image of what I was getting (the Sign Up button is using the default button style). I was able to get the green I wanted but now there's a weird black border around it, which I have no idea how to remove.
So the next thing I tried was looking for the default values that are given to the default Button style. Searching through multiple files, I finally found the drawable file that gave the default Button look and it was a .9.png image file. I couldn't find any details on what the default values were online or in any of the drawbale files. I just simply want to do what android:backgroundTint="#color/green" would do. But I'm supporting API 16 and up so I can't use it.
This is very frustrating just to change the background color but keep the default style. Any help would be much appreciated.

How to make button transparent in my app yet visible

I want my buttons to be transparent as I have an image on the activity as a background.
If the buttons are not transparent, those buttons are covering the image and image background seems useless.
If I use android:background="#android:color/transparent", it is making my button completely invisible except the text on it. So, I need the button to be transparent provided it should be visible that there is a button. Code snippet would be appreciated.
<Button
android:id="#+id/new_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent" //this line makes the button completely invisible.
android:text="#string/label" />
If you just want white borders, and the text/icons in the button, but the button background transparent create the following drawable (I named it button_border.xml):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<stroke
android:color="#android:color/white"
android:width="1dp" />
<solid android:color="#android:color/transparent" />
</shape>
</item>
</selector>
And use it as a background for the button:
<Button
.
.
.
android:background="#drawable/button_border"
/>
I'd guess there is a more elegant way to do it with themes, but I'm not that far yet. In the button_border drawable you can add rounded corners and whatnot...
android:background="#A0FFFFFF"
I've hard coded the colour, but you should store it in colors.xml
Modify the first two characters, A0, to adjust how much transparency you want and the last 6 characters the color you want.
Apply selector for button, like this
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"><shape>
<solid
android:color="#color/color_1_end" />
<stroke
android:width="1dp"
android:color="#android:color/transparent" /> // transparent patametr
<corners
android:radius="4dp" />
<padding
android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape></item>
<item>
<shape>
<gradient android:angle="270"
android:endColor="#color/color_1_start"
android:startColor="#color/color_1_end" /> // maybe transparent
<stroke
android:width="1dp"
android:color="#color/color_1_start" /> //maybe transparent
<corners android:radius="4dp" />
<padding android:bottom="10dp"
android:left="10dp"
android:right="10dp"
android:top="10dp" />
</shape>
</item>
Modify this for you =)
Apply border for button
<item>
<shape>
<padding
android:bottom="1dp"
android:left="1dp"
android:right="1dp"
android:top="1dp" />
<solid android:color="#FF000000" />
<corners android:radius="3dp" />
</shape>
</item>
<item>
<shape>
<padding
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp" />
<gradient
android:angle="315"
android:centerColor="#FFFFFFFF"
android:endColor="#FFB0B0B0"
android:startColor="#FFB0B0B0" />
</shape>
</item>
<Button
android:id="#+id/btnLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="7dip"
android:background="#drawable/button_border"
android:scaleType="centerInside"
/>
Just try to use below code in java
<br/>
button.getBackground().setAlpha(int);<br/>
correct range of input parameter is between 1 and 255 and the '1' is more transparent.
common pitfall is using:<br/>
button.setAlpha(float);<br/>
that make all button transparent.

How to have rounded corners with repeat?

I am trying for repeat for buttons. I am able to get repeat for regular background type. But i was not able to find right, left and center using the repeat xml file.
If you know some solution with the xml file please help me.
Thanks in advance
The repeat xml file i have used is
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background">
<bitmap
android:src="#drawable/barslice"
android:tileMode="repeat"
android:dither="true"/>
</item>
The button i require is:
you may get round-corner gradient buttons without even using images.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#449def" />
<stroke
android:width="1dp"
android:color="#2f6699" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#449def"
android:endColor="#2f6699"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#2f6699" />
<corners
android:radius="4dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
and simply set it as background to your button, textview or whatever you want
This will be nicely handled by 9 patch image.
you will have an image like this
and android will handle it automatically.
Just set it as background of any button.
Note: image for other states like pressed, focused and disabled should also be provided otherwise it will not change buttons state.
You can use 9-patch images. Check the following links.
http://developer.android.com/guide/topics/resources/drawable-resource.html#NinePatch
http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch

Make default Android orange as focused,pressed,selected state of a custom buttom

I have a custom button, but for the focused,pressed,selected state I wanna have the default Android orange selected state rather than make new custom images for those states.
How am I able to achieve that? What should I write on the selector for those states?
Check this code it'll give you a lead about how to change the colors for each state.
<?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="#F5B800"
android:endColor="#F5B800"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#ffcc00" />
<corners
android:radius="1dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item android:state_focused="true" >
<shape>
<gradient
android:endColor="#F5B800"
android:startColor="#F5B800"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#ffcc00" />
<corners
android:radius="1dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:endColor="#ff9900"
android:startColor="#ffcc00"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#ffcc00" />
<corners
android:radius="1dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
I am sure you want to implement that default orange color image inside your custom selector file, if this is the case then you can know the name of all the in-built drawable files used by system, from here you can know: Android Drawables
Some of button default images names are:
btn_default_pressed.9
btn_default_selected.9
btn_default_small_pressed.9
btn_default_small_selected.9
so now copy the 9-patch images inside the res/drawable folder and use it wherever you want.
Have you tried not mentioning those states? i haven't tried but off the top of my head, i would say that if you donot mention a state, android should use the default values.

Background for a button in android?

In my app I have set for a button the background to a certain color. I don't like how it looks like.The button is like a rectangle, with straight edges. I want that the button to have rounded corners. How is it possible?
Here is my button in xml :
<Button android:id="#+id/button" android:text="Participer au concours de photos"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="410dip" android:layout_centerHorizontal="true"
android:background="#drawable/orange1"/>
Thanks...
Use selector
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="#24a1dd"
android:centerColor="#158ee4"
android:endColor="#37b8ee"
android:angle="270"
android:centerY="0.88" />
<stroke
android:width="3dp"
android:color="#00ffffff"
/>
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item android:state_focused="true" >
<shape>
<gradient
android:endColor="#59bfe6"
android:centerColor="#36aced"
android:startColor="#91def7"
android:angle="270"
android:centerY="0.88" />
<stroke
android:width="3dp"
android:color="#00ffffff"
/>
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:endColor="#e1e1e1"
android:centerColor="#bdbebd"
android:startColor="#f6f2f6"
android:angle="270"
android:centerY="0.88" />
<stroke
android:width="3dp"
android:color="#00ffffff"
/>
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
Something like this should do the work. Just fix the color values. Which is the same as the the answer of this question Standard Android Button with a different color
There are two ways :
Use 9 patch drawables (png image)
here is complete tutorial 9 patch drawables: background of a customized Button
Set xml drawable as background of your buttons.
Take a look here Gradient buttons for android.
And to know more about xml drawables :-
ANDROID XML DRAWABLES

Categories

Resources