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>
Related
This is an example of the buttons that I need to create
I want to create a button with the bottom border, I was able to create a button using layer-list, but now for each button I have to create a separate layer-list. Are there any ways to create one template and reuse it?
This is the code to create a button with a layer-list
background_button.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="#793838" />
<corners android:radius="10dp" />
</shape>
</item>
<item android:bottom="3dp">
<shape android:shape="rectangle">
<corners android:radius="10dp" />
<solid android:color="#4056b6" />
</shape>
</item>
</layer-list>
Use below link to create custom buttons for android.
http://angrytools.com/android/button/
You must do it programmatically, once you have created the layout of a button you can reuse it several times..
here one example but you can find more!
Creating multiple buttons programmatically: Android
I want to create a family of buttons. They all look the same, except for their color. The brute force way of doing this would be to copy/paste the layout into N files, then change the colors in each one.
What’s the more elegant way of achieving this? In the sample code below, I'd like to use different colors instead of #color/round_green_button_pressed_background and #color/round_green_button_unpressed_background, but everything else should ideally stay in one place.
Doing this programmatically seems even worse than copy/pasting the files, so hopefully, there’s an answer that involves pure XML-based layouts.
Sample code: button_green.xml.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<size android:width="120dp" android:height="120dp"/>
<solid android:color="#color/round_green_button_pressed_background"/>
</shape>
</item>
<item>
<shape>
<size android:width="120dp" android:height="120dp"/>
<solid android:color="#color/round_green_button_unpressed_background"/>
</shape>
</item>
</selector>
My buttons use two XML files to do some fancy color switching when pressed, but I have a problem with the color drawable (dunno what to call it)...
Here's a button
<Button
android:background="#drawable/main_loginbtn"
android:textColor="#color/main_loginbtn"
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:onClick="login"
android:text="Login" />
(I know I shouldn't use hardcoded strings, but I will change this later :)
Here's #drawable/main_loginbtn
<?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="#FFFFFF"/>
<stroke android:width="2dp"
android:color="#00BFFF"/>
</shape>
</item>
<item>
<shape android:shape="rectangle" >
<solid android:color="#00BFFF"/>
<stroke android:width="2dp"
android:color="#FFFFFF"/>
</shape>
</item>
</selector>
And finally, here's the #color/main_loginbtn file
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#00BFFF"/>
<item android:color="#FFFFFF"/>
</selector>
If I remove the textColor reference to #color/main_loginbtn, it will fix the problem and the text will reappear on the button (in black of course). So i'm pretty sure the problem is in the color drawable.
Normally, I wouldn't care about this, but it messes up the scaling on some buttons because of wrap_content, when there's no text inside the buttons.
Thanks for your time!
UPDATE
I tried creating a second random color drawable, and tested it on a TextView's textColor attribute, and the same problem arose... The entire TextView text disappeared.
So i'm thinking it's a problem with the selector?
Oh, and I also messed up in this question: Nothing "disappears" per say, but rather I can't see the text in the eclipse layout UI. When I run it in the emulator, everything works fine...
You need to set the transparency of your color.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#FF00BFFF"/>
<item android:color="#FFFFFFFF"/>
</selector>
The UI editor is assuming your colors are transparent if you don't (in other words it's defaulting to alpha = 00 if you don't set it).
I'm trying to programmatically add a background resource to a button but as soon as I do that, the highlight effect of when the button is clicked goes away. How do I restore the highlight effect while still using the background?
button_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#layout/avail_button_solid" />
<item android:drawable="#layout/avail_button_shape" />
</layer-list>
avail_button_solid.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ababab" />
</shape>
avail_button_shape.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff" />
<stroke android:width="1dp" android:color="#ff000000" />
</shape>
Java code:
b.setBackgroundResource(R.layout.button_layout);
The code sets the background but takes away the highlighting effect.
Buttons (and many other Android widgets) use a <selector> drawable (the analog in the SDK is a StateListDrawable). This is actually a Drawable container that maps different drawables to different states: pressed, focused, disabled, normal, etc. You should make one of these yourself if you want to replace the button background.
Read more about it here: http://developer.android.com/design/style/touch-feedback.html
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