ToggleButton with custom background and icon - android

I created a drawable for my ToggleButtons, but I also need to show an icon.
How can I do this if there's just a background attribute?
Here's the ToggleButton drawable:
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="8dp" />
<stroke android:color="#CED2D4" android:width="1dp" />
<solid android:color="#EDEEEF"/>
</shape>
And the code to place the ToggleButton (currently using IconFont, but I have to change as the items are dynamic):
<ToggleButton
android:id="#+id/toggleCreditCard"
android:layout_width="#dimen/width_filter_facilities_button"
android:layout_height="#dimen/height_filter_facilities_button"
android:background="#drawable/filter_service_toggle_default"
android:textOff="#string/fa_credit_card"
android:textOn="#string/fa_credit_card" />

This may help you. You can create a layer-list and add two item. First item is your custom drawable, the second is your icon.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/list_bg_top"></item>
<item android:left="10dp">
<bitmap android:src="#drawable/waiting_status_btn"
android:gravity="center_vertical|left" />
</item>
Check it out: customize toggle button on Android

Related

Add Ripple / Animation effect to an Android button but keep original customization

I have a custom button, and I need to create a Ripple effect. But after creating I do not know how I could combine the two effects.
I would like to have my original style and when I click add the ripple effect.
Original code before ripple:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#EEEEEE"></solid>
<corners android:radius="5dp"></corners>
<stroke android:width="1dp" android:color="#99FFFFFF"></stroke>
</shape>
Code ripple:
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:color="#f816a463"
tools:targetApi="lollipop">
<item android:id="#android:id/mask">
<shape android:shape="rectangle">
<solid android:color="#f816a463" />
<corners android:radius="5dp" android:color="#f816a463"></corners>
<stroke android:width="1dp" android:color="#f816a463"></stroke>
</shape>
</item>
<item
android:id="#android:id/background"
android:drawable="#android:color/holo_blue_bright">
</item>
</ripple>
Code Button:
<Button
android:id="#+id/btns2teste"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/btnTestes"
android:layout_below="#+id/btnTestes"
android:layout_marginTop="49dp"
android:background="#drawable/ripple_effect"
android:text="Botao2" />
Image Button before:
When I created the new style and called in the xml of the button, I can no longer call the effect I created earlier, and it gets a simple button with ripple colors.
I would like to apply the ripple effect but keep the button state in the way I'd like it to be already custom with the border.
I need to find a way to combine the styles.

ButtonView with different background

I have designed a rounded corners button.
button_selector.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="10dp"/>
</shape>
In layout:
<Button
android:background="#drawable/button_selector"
.../>
on every click the color of the should button changes :
In activity, inside onClickListener():
btn1.setBackgroundColor(getAnswerColor(choiceArray[0]));
but if I do this, I am not getting the rounder corners.
How to get rounder corners? Please suggest.
You can make your button from this toolsite ==> Android Button Maker
You can make any kind of button with this tool with Code.
Here i edit this Answer as your requirement.
so,make two different button for with different background color and use selector for changing background onClick.For that use this code.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/click_button" android:state_pressed="true" />
<!-- When not selected, use white-->
<item android:drawable="#drawable/not_click_button" android:state_pressed="false" />
</selector>
try this way
i checked with below xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item >
<shape android:shape="rectangle" >
<corners android:radius="13dip" />
<stroke android:width="1dip" android:color="#5e7974" />
<gradient android:angle="-90" android:startColor="#8dbab3" android:endColor="#58857e" />
</shape>
</item>
</selector>
on button click add this code
StateListDrawable drawable = (StateListDrawable) v.getBackground();
drawable.setColorFilter(Color.RED,PorterDuff.Mode.SRC_ATOP);
button1.setBackground(drawable);
btn1.setBackgroundResource(R.drawable.button_selector);
GradientDrawable drawableGradient = (GradientDrawable) btn1.getBackground();
drawableGradient .setColor(Color.RED);

Make Custom Drawable Shape in Android

I want to make drawable like below,
I am able to make the Rectangle and Triange shape in two different xml files. But i want to joint them to make the drawable like this.
Use layer-list to make this custom shape drawable.
/res/drawable/custom_shape.xml:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Transparent Rectangle -->
<item>
<shape android:shape="rectangle">
<size
android:width="300dp"
android:height="60dp" />
<solid android:color="#android:color/transparent" />
</shape>
</item>
<!-- Colored Rectangle -->
<item
android:bottom="20dp">
<shape android:shape="rectangle">
<size
android:width="300dp"
android:height="60dp" />
<solid android:color="#9A8585" />
</shape>
</item>
<!-- Bottom Triangle -->
<item
android:left="80dp"
android:right="120dp"
android:top="0dp"
android:bottom="30dp">
<rotate android:fromDegrees="45">
<shape android:shape="rectangle">
<solid android:color="#9A8585" />
</shape>
</rotate>
</item>
<!-- Top Border -->
<item
android:bottom="75dp">
<shape android:shape="rectangle">
<solid android:color="#EFC1B3" />
</shape>
</item>
</layer-list>
USE:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/custom_shape"/>
OUTPUT:
Hope this will help~
Use <layer-list/> for that.
Here is an example for instance.
in Drawable folder of resin project directory,
make xml file and name it as layerlist.xml and paste it below code as your requirement.
you can also add your shape drawable instead of drawable in the <item/> tag in the example. and use this xml as a background of ImageView.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap android:src="#drawable/android_red"
android:gravity="center" />
</item>
<item android:top="10dp" android:left="10dp">
<bitmap android:src="#drawable/android_green"
android:gravity="center" />
</item>
<item android:top="20dp" android:left="20dp">
<bitmap android:src="#drawable/android_blue"
android:gravity="center" />
</item>
</layer-list>
Here is the result of the use of <layer-list/>.
I hope it helps you...
I expect that you need it for a a Tabular navigator or a ViewPager. My suggestion is that
1)Use the rectangle as your background for all the cases.
2)Create the same triangle drawable but with the background color (White or transparent) for the unselected item.
3)Create a view with the triangle background for all the items
4)Manually setVisibility of the triangle view according to the selection state

How Do Create This Type Of Button In Android

How to create this type of button in android. I tried . May I know what is the correct way to achieve my objective?Maybe this question too basic, but i did't find any suitable solution.Please Help me out.
I want to create This
<Button
android:id="#+id/Btn1"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#drawable/green_circle"
android:textSize="20sp"
android:textColor="#android:color/white"
android:clickable="true"
/>
Here is green_circle.xml :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke android:width="1sp" android:color="#54d66a" />
</shape>
When i use this ,Button looks
How can i get my desire button. Any Help Appreciated
You defined 1sp (you should use "dp" btw.) circle and you got it.
I think that the easiest way to achieve what you want is using layer list xml element - just put your current drawing as the first layer and put second layer for the centered dot (just solid oval with some margins)
Here you have documentation: http://developer.android.com/guide/topics/resources/drawable-resource.html#LayerList
Simple usage (it's not your's case but I think that it show you what you should to do):
<layer-list >
<item>
<shape android:shape="rectangle">
<corners android:radius="2dp"/>
<solid android:color="#color/grey"/>
</shape>
</item>
<item android:bottom="2dp">
<shape android:shape="rectangle">
<corners android:radius="2dp"/>
<solid android:color="#color/green_light"/>
</shape>
</item>
</layer-list>
Key is android:bottom="2dp" that causes that the foreground layer is smaller than the background one.
One way to achieve your desired effect is using a Layer List Drawable. To get a Drawable similar to the image you've posted, try the following
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<stroke android:width="1sp" android:color="#54d66a" />
</shape>
</item>
<item android:top="5dp" android:left="5dp" android:right="5dp" android:bottom="5dp">
<shape android:shape="oval">
<solid android:color="#54d66a" />
</shape>
</item>
</layer-list>
1.use two different images of radio on and off actually what you want and make a xml in drawable :
<?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/check_off" />
<item android:state_checked="true" android:drawable="#drawable/checkbox_on" />
<item android:drawable="#drawable/checkbox_off" />
</selector>
2.and set this drawable as a background on your radio button.
Use an ImageButton:
<ImageButton android:src="#drawable/green_circle" />
The documentation also states how you can have different images depending on the state of the button.
put an image of green circle in drawable folder. then in xml just set background image for your button
<Button
android:id="#+id/Btn1"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#drawable/yourimage"
/>
this will work :)

What is the style used to create the "selected" effect on a ListView? How do I figure this out?

While I'm touching something in a ListView I get a blue highlight. Now I have another view elsewhere that I want to look the same; I tap on it and get a blue highlight. I thought this was a list_selector_background, but when I try that I get an orangey color.
My first question is: what is the style for the stock ListView item's background when selected?
Second (and more importantly), how do I figure this out? I'm looking for the chain of investigation, like...
List item
Look at the default theme (ABC)
Look at theme ABC for the style (QWE)
Look at style QWE and get the selected drawable (RTY)
Look at drawable RTY
Or whatever the actual investigation trail is
In your layout .xml for this view you want to add a new .xml called button_selector to the attributue android:background like so...
android:background="#drawable/button_selector"
Then create a new android .xml in the drawable folder called button_selector.xml containing this code....
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected -->
<item android:drawable="#drawable/button_selected"
android:state_focused="true" />
<!-- When not selected-->
<item android:drawable="#drawable/button_unselected" />
</selector>
Then create your button_unselected.xml and button_selected.xml in the drawable folder as well for the layout of each of the buttons.
this is an example of one of my button_unselected.xml files ....
<?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="#10000000" />
<padding
android:bottom="4dp"
android:top="4dp" />
<corners android:radius="5dp" />
</shape>
</item>
<item>
<shape android:shape="rectangle" >
<solid android:color="#20558e34" />
</shape>
</item>
</layer-list>
Hope this helps...

Categories

Resources