I have this button XML, which has transparent background and when clicked, the color changes, so that it shows a "click effect".
Drawable XML:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- press-->
<item android:drawable="#android:color/transparent" android:state_pressed="false" />
<!-- focused-->
<item style="#style/AppTheme" android:drawable="#color/color_primary" android:state_pressed="true" />
<!-- normal-->
<item android:drawable="#android:color/transparent" />
</selector>
My question is, how to put border on this button? Or an XML example so you have the same result. Thank you!
create a drawable, similar to this, selected.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#ff0000"/>
<stroke
android:width="1dp"
android:color="#00ff00" />
</shape>
then change this line
<item
style="#style/AppTheme"
android:drawable="#color/color_primary"
android:state_pressed="true" />
to
<item
style="#style/AppTheme"
android:drawable="#drawable/selected"
android:state_pressed="true" />
the solid is the background color and the border is the stroke, you can create drawables that are used in the diferent states of the selector
You can do something like this:
1. First create a drawable file like this:
<shape android:shape="rectangle">
<!--apply button background transparent, full opacity-->
<solid android:color="#00ffffff"/>
<!--make button border solid color, nontransparent-->
<stroke android:color="#stroke color" android:width="2dp"/>
<corners android:radius="2dp"/>
</shape>
then in your provided code replace these lines:
<!-- press-->
<item android:drawable="path to above drawable" android:state_pressed="false" />
<!-- focused-->
<item style="#style/AppTheme" android:drawable="#color/color_primary" android:state_pressed="true" />
<!-- normal-->
<item android:drawable="path to above drawable" />
as
<!-- press-->
<item android:drawable="path to above drawable" android:state_pressed="false" />
<!-- focused-->
<item style="#style/AppTheme" android:drawable="#color/color_primary" android:state_pressed="true" />
<!-- normal-->
<item android:drawable="path to above drawable" />
Related
I have a ListView, showing the default color of the selector as shown in the picture below.
I would like to change color of this selector to, lets say the color teal.
ListView.xml:
<ListView
android:drawSelectorOnTop="true"
android:listSelector="#drawable/listitem_selector"
android:choiceMode="singleChoice"
android:id="#+id/lw"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
listitem_selector.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/teal" />
<item android:state_focused="true" android:drawable="#color/teal" />
<item android:state_selected="true" android:drawable="#color/teal" />
<item android:state_active="true" android:drawable="#color/teal" />
<item android:state_activated="true" android:drawable="#color/teal" />
<item android:state_enabled="true" android:drawable="#color/teal" />
<item android:state_checked="true" android:drawable="#color/teal" />
<item android:state_single="true" android:drawable="#color/teal" />
</selector>
As you can see, I've tried a bunch of state_* attributes with no success, the selected item shows as the default color. How do I change this appearance?
I'm just doing the same thing. This works for me.
I don't set the #drawable in listview, I have set it in format of listview i.e. in layout where you have put textViews and checkBoxes to be displayed in listview.
list_view_format.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/list_view_item">
//your ckeckbox and textviews
</RelativeLayout>
/res/drawable/list_view_item.xml
<item android:state_pressed="true" >
<shape>
<solid android:color="#color/list_item_pressed"/>
</shape>
</item>
<item android:state_activated="true" >
<shape>
<solid android:color="#color/list_item_activated"/>
</shape>
</item>
<item>
<shape>
<solid android:color="#color/list_item_normal" />
</shape>
</item>
/res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="list_item_normal">#96ffdcb5</color>
<color name="list_item_activated">#ffaa66</color>
<color name="list_item_pressed">#ffaa66</color>
</resources>
I have defined the below files in res folder.
styles_apptheme.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Generated with http://android-holo-colors.com -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="ButtonAppTheme" parent="android:Widget.Holo.Light.Button">
<item name="android:background">#drawable/apptheme_btn_default_holo_light</item>
</style>
</resources>
themes_apptheme.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Generated with http://android-holo-colors.com -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="#style/_AppTheme"/>
<style name="_AppTheme" parent="Theme.AppCompat.Light">
<item name="android:editTextBackground">#drawable/apptheme_edit_text_holo_light</item>
<item name="android:buttonStyle">#style/ButtonAppTheme</item>
</style>
</resources>
In my Layout.xml file, i have defined my button as below
<Button
android:id="#+id/btnAddTitle"
android:layout_below="#id/edEnterTitleValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btn_AddTitle"
android:layout_margin="20dp"
/>
If i add the below line to the button view above,
android:background="#style/ButtonAppTheme"
Application crashes saying that drawable resource should be set for the background attribute.
So i created the below drawable file - abc.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/apptheme_btn_default_normal_holo_light" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/apptheme_btn_default_disabled_holo_light" />
<item android:state_pressed="true"
android:drawable="#drawable/apptheme_btn_default_pressed_holo_light" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/apptheme_btn_default_focused_holo_light" />
<item android:state_enabled="true"
android:drawable="#drawable/apptheme_btn_default_normal_holo_light" />
<item android:state_focused="true"
android:drawable="#drawable/apptheme_btn_default_disabled_focused_holo_light" />
<item
android:drawable="#drawable/apptheme_btn_default_disabled_holo_light" />
</selector>
If i set android:background="#drawable/abc" i dont see the style set in the button.
So please let me know how i can set the style for the button.
Thanks.
It would be rather simple if you do it this way.
First create a button_selector.xml in drawable folder.
<?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="5dp" />
<stroke android:width="1dip" android:color="#color/green_temp" />
<gradient android:angle="-90" android:startColor="#color/green_temp" android:endColor="#color/green_temp" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle" >
<corners android:radius="5dp" />
<stroke android:width="1dip" android:color="#971E05" />
<solid android:color="#58857e"/>
</shape>
</item>
<item >
<shape android:shape="rectangle" >
<corners android:radius="5dp" />
<stroke android:width="1dip" android:color="#color/bright_green" />
<gradient android:angle="-90" android:startColor="#color/green_temp" android:endColor="#color/button_green" />
</shape>
</item>
</selector>
Add these colors in colors.xml of values folder.
<!-- Green -->
<color name="green_temp">#23A96E</color>
<color name="green_dark">#159204</color>
<color name="bright_green">#02D8B0</color>
<color name="button_green">#10a54a</color>
Finally in your desired button of layout.xml put background from above selector.
<Button
android:id="#+id/btnAddTitle"
android:layout_below="#id/edEnterTitleValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btn_AddTitle"
android:background="#drawable/button_selector"
android:layout_margin="20dp"
/>
Then its all done your button will be styled with color you want.
Android Button Maker is online tool to generate buttons code for Android Apps. Android API provide Drawable Resources where XML file defines geometric shape, including colors, border and gradients.
These button is generating based on shape drawable XML code which load faster compare to normal png buttons. You can customize button properties in setting panel and get source code.
Check this link Button Maker
No need to crack brains...this tool make it simple
replace
android:background="#style/ButtonAppTheme"
with
style="#style/ButtonAppTheme"
and every thing is ok!
Try this out as alternative. create a drawable folder under res then create xml files and copypaste below code and try first then you customize as per your requirement.
button_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#00acc1"/>
<stroke android:width="2dp" android:color="#ffffff"/>
<corners android:radius="2dp"/> </shape>
button_bg_pressed.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#006064"/>
<stroke android:width="1dp" android:color="#ffffff"/>
<corners android:radius="2dp"/> </shape>
button_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/button_bg_pressed" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/button_bg_pressed" />
<item android:drawable="#drawable/button_bg" /> </selector>
And now in your button xml set the background as button_selector.xml
<Button
android:id="#+id/btnAddTitle"
android:layout_below="#id/edEnterTitleValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btn_AddTitle"
android:background="#drawable/button_selector"
android:layout_margin="20dp"/>
This will do the job for you. You can customize your entire button style by this way.
If you want to have the default animation of button by material design and change button color, try this.
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="#dimen/padding_12"
android:text="button text here"
android:theme="#style/NavyBtn" />
in style.xml
<style name="NavyBtn" parent="Theme.AppCompat.Light">
<item name="colorButtonNormal">#020e39</item>
<item name="android:textColor">#FFFFFF</item>
</style>
In my project I have a ViewPager that is not linked to the action bar and I'm having problems with setting the font size of the text for the tabs. I'm using ViewPagerIndicator (v2.4.1) in combination with ActionBarSherlock (v4.3.1).
The layout I'm using is:
<com.viewpagerindicator.TabPageIndicator
android:id="#+id/artistIndicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<android.support.v4.view.ViewPager
android:id="#+id/artistPager"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:minHeight="50dp" />
This is within a LinearLayout inside a ScrollView. The layout works as expected and I see no problems with the interaction between the ScrollView and my ViewPager.
Styling of the ViewPager and tabs is done with the following XML:
<style name="ArtistIndicatorTheme" parent="AppTheme">
<item name="vpiTabPageIndicatorStyle">#style/ArtistInfoTabPageIndicator</item>
</style>
<style name="ArtistInfoTabPageIndicator" parent="Widget.TabPageIndicator">
<item name="android:background">#drawable/tab_background</item>
<item name="background">#android:color/white</item>
<item name="android:textAppearance">#style/ArtistInfoTabPageIndicator.Text</item>
</style>
<style name="ArtistInfoTabPageIndicator.Text" parent="#style/TextAppearance.TabPageIndicator">
<item name="android:textColor">#drawable/tab_text_color</item>
<item name="android:textSize">#dimen/activity_position_artistinfo_font_size</item>
<item name="android:textStyle">bold</item>
</style>
The background of the tabs is handled by the following XMLs to create a actual tab-style look:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/tab_background_unselected" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/tab_background_selected" />
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/tab_background_unselected" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/tab_background_selected" />
</selector>
And the two shape XMLs for selected:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Bottom Line -->
<item>
<shape android:shape="rectangle" >
<solid android:color="#color/text_inverted" />
</shape>
</item>
<!-- Color of your action bar -->
<item android:bottom="2dip">
<shape android:shape="rectangle" >
<solid android:color="#color/text_inverted" />
</shape>
</item>
</layer-list>
and unselected:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Bottom Line -->
<item>
<shape android:shape="rectangle" >
<solid android:color="#color/text_inverted" />
</shape>
</item>
<!-- Color of your action bar -->
<item android:bottom="2dip" android:right="2dip">
<shape android:shape="rectangle" >
<solid android:color="#color/text_important" />
</shape>
</item>
</layer-list>
The current font size is set by the following dimension:
<dimen name="activity_position_artistinfo_font_size">15dp</dimen>
Any indication on what I'm doing wrong?
I have a list where I have defined my own list view items with a custom layout. This layout has a background with a custom drawable.
My custom layout for the ListView item:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/item"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true" >
...
</RelativeLayout>
My custom drawable item.xml:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- background: shadow -->
<item>
<shape
android:dither="true"
android:shape="rectangle" >
<corners android:radius="2dp" />
<solid android:color="#color/itemShadowColor" />
</shape>
</item>
<!-- foreground: surface -->
<item android:bottom="2dp">
<shape
android:dither="true"
android:shape="rectangle" >
<corners android:radius="2dp" />
<solid android:color="#color/itemBackgroundColor" />
</shape>
</item>
</layer-list>
Now this item is not clickable anymore.
Can you explain me why, and what I have to do to have the same behavior (selector with the blue background) like a button click?
Define a selector with its pressed and default states in res/drawable folder(one of the state will be your #drawable/item). Set it as the bg of your list row layout.
See similar question and answer :Selector on background color of TextView
Edit:
Best way to understand and apply something like google did is, to look into SDK and do similar things to that. For instance look at the btn_default_holo_dark drawable. It is a selector with states and yes it is a xml.
This is a selector taken from sdk (sdk\platforms\android-18\data\res\drawable\btn_default_holo_dark.xml)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/btn_default_normal_holo_dark" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/btn_default_disabled_holo_dark" />
<item android:state_pressed="true"
android:drawable="#drawable/btn_default_pressed_holo_dark" />
<item android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/btn_default_focused_holo_dark" />
<item android:state_enabled="true"
android:drawable="#drawable/btn_default_normal_holo_dark" />
<item android:state_focused="true"
android:drawable="#drawable/btn_default_disabled_focused_holo_dark" />
<item
android:drawable="#drawable/btn_default_disabled_holo_dark" />
</selector>
These are the images taken from sdk (sdk\platforms\android-18\data\res\drawable-xhdpi):
When you apply this drawable/selector (#drawable/btn_default_holo_dark) to any view, you are going to have its states. I hope this sample makes my answer more clear.
A noob quesiton here:
I have a button with a partially-transparent PNG background. I am trying to maintain the transparency of the button across different press states. The problem is: when I apply a gradient to the background image (to indicate the button is pressed), the gradient darkens the whole 'block of space' occupied by the image, not just the image itself.
How do I keep the transparent parts of the image transparent? Thanks in advance...
selector_button_singleplayer.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:state_pressed="false" android:drawable="#drawable/button_singleplayer" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/gradient_button_singleplayer" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/gradient_button_singleplayer" />
<item android:drawable="#drawable/button_singleplayer" />
gradient_button_singleplayer.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<bitmap android:src="#drawable/button_singleplayer"/>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:angle="90" android:startColor="#880f0f10" android:centerColor="#880d0d0f" android:endColor="#885d5d5e"/>
</shape>
</item>
</layer-list>
my button:
<Button android:id="#+id/main_menu_choose_single_player"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="25dip"
android:background="#drawable/selector_button_singleplayer"
/>
Go for ImageButton
set android:src as button_singleplayer
and your gradient_button_singleplayer.xml will be
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:angle="90" android:startColor="#880f0f10" android:centerColor="#880d0d0f" android:endColor="#885d5d5e"/>
</shape>
and your selector_button_singleplayer.xml will be
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:state_pressed="false" android:drawable="#android:color/transparent" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/gradient_button_singleplayer" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/gradient_button_singleplayer" />
<item android:drawable="#android:color/transparent" />
and your imageButton will be
<ImageButton android:id="#+id/main_menu_choose_single_player"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="25dip"
android:background="#drawable/selector_button_singleplayer"
android:src="#drawable/button_singleplayer"
/>