How to have rounded corners with repeat? - android

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

Related

How can i allow user to change button's color in an android app

I am developing a calculator app for android and i use drawable files to manage the color of sets of buttons.
i need to allow user to change the calculator theme by providing several drawable files then the user can choose between them.
I have no idea how to do this except making multiple layout then i can use (if) statement in the activity.
is there an easy way to this
i use somthing like this to define the button background
<?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" >
<corners android:radius="9dip" />
<stroke android:width="1dip" android:color="#009e9e9e" />
<gradient android:angle="-90" android:startColor="#ae104f" android:endColor="#8e0d40" />
</shape>
</item>
<item>
<layer-list>
<item android:right="5dp" android:top="5dp">
<shape>
<corners android:radius="8dp" />
<solid android:color="#bd848484" />
</shape>
</item>
<item android:bottom="1dp" android:left="2dp">
<shape>
<gradient android:angle="270"
android:endColor="#ae104f" android:startColor="#8e0d40" />
<stroke android:width="1dp" android:color="#BABABA" />
<corners android:radius="9dp" />
<padding android:bottom="10dp" android:left="10dp"
android:right="10dp" android:top="10dp" />
</shape>
</item>
</layer-list>
</item>
</selector>
Thanks
You can store user's color file or sharedpreferences. and you app starting read this color, and store static value so set backcolor(static color value) for all button. If i were you, i do that. It'll work.

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.

Custom Button with Background color

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>

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.

Custom Buttons in Android: How to get border/edge/frame when I read the background from xml?

Using Android Shapes in xml I have defined a gradient which I use as the background for a button.
This all works nice, but there's no edge surrounding the button. I would like it to look similar to the normal Android button but I need more flexibility to control the color and look.
The shape is defined as follows:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient android:startColor="#FFFFFF"
android:endColor="#00FF00"
android:angle="270" />
<corners android:radius="3dp" />
<stroke android:width="5px" color="#000000" />
</shape>
I would expect the border to be set in the xml. Why doesn't "stroke" fix it? Stroke doesn't seem to do anything.
I checked the Android Developer spec, but couldn't find the answer there:
http://developer.android.com/guide/topics/resources/drawable-resource.html
I have also looked through all the properties of the Android Button, but as expected there's no such parameter, probably since it's built into the normal Android button. Btw, I checked ImageButton properties too.
Can someone please help?
I know there's the alternative to make an image with proper edges and use an ImageButton, but there really should be a way to fix this programmatically.
Thanks!
Anna
I had this problem a while ago. While I don't quite remember why I made each decision, the way I solved it was to use a a shape layer-list. This lets you stack one shape on top of another. For example, the following XML creates a shape with a solid black outline 2px wide, with a 'grey to white to grey' gradient across the middle:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp"/>
<solid android:color="#FF000000"/>
<corners android:radius="3dp"/>
</shape>
</item>
<item>
<shape>
<padding android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp"/>
<gradient android:startColor="#FFB0B0B0"
android:centerColor="#FFFFFFFF"
android:endColor="#FFB0B0B0"
android:angle="315"/>
</shape>
</item>
</layer-list>
If you want to be able to change that color dynamically at runtime, then things get a lot messier. Again, the details of why I had to do things a certain way are hazy, but I ended up having to create a custom view class which contained a custom ShapeDrawable. I started off looking at the examples from the ApiDemos app which comes with the SDK - it's a very good resource.
EDIT: Another reason your stroke might not be appearing is that you forgot the android: before the color="...." bit.
I've had the same problem, what i observed is stroke is not applying to button as border at design time but at run time i see the border.
I jst used the following same code
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#color/black" />
<stroke android:width="1px" android:color="#color/red" />
</shape>
as steve hanley said abvoe you miss the android: for the color attribute.
Hope this helps somebody....
Probably much to late, but you have to add an extra ff before the color.
<stroke android:width="5px" color="#ff000000" />
Greets
Use ImageButton
<ImageButton
android:layout_width="40dp"
android:layout_height="30dp"
android:src="#drawable/left_arrow"
android:background="#drawable/button_selector"
android:gravity="center"/>
Use drawable selector to ImageButton and define your properties
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="#color/startColor"
android:endColor="#color/endColor"
android:angle="270" />
<stroke
android:width="0.5dp"
android:color="#color/borderColor" />
<corners
android:topLeftRadius="5dp"
android:bottomRightRadius="5dp"/>
</shape>
</item>
<item android:state_focused="true" >
<shape>
<gradient
android:startColor="#color/startColor"
android:endColor="#color/endColor"
android:angle="270" />
<stroke
android:width="0.5dp"
android:color="#color/borderColor" />
<corners
android:topLeftRadius="5dp"
android:bottomRightRadius="5dp"/>
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#color/startColor"
android:endColor="#color/endColor"
android:angle="270" />
<stroke
android:width="0.5dp"
android:color="#color/borderColor" />
<corners
android:topLeftRadius="5dp"
android:bottomRightRadius="5dp"/>
</shape>
</item>
<item android:state_enabled="false" android:drawable="#drawable/button_disabled" />
<item android:state_focused="true" android:drawable="#drawable/button_highlighted"/>
<item android:state_pressed="true" android:drawable="#drawable/button_highlighted"/>
<item>
<shape>
<gradient android:startColor="#fdfdfd" android:endColor="#f0f0f0" android:angle="270" />
<stroke android:width="1dp" android:color="#56390a" />
<corners android:radius="3dp" />
<padding android:left="10dp" android:top="10dp" android:right="10dp" android:bottom="10dp" />
</shape>
</item>
Adding stroke color #56390a will solve the problem.

Categories

Resources