Android add arrow image to spinner - android

I have created a background drawable that I use for a spinner. I would like to now but an arrow image inside this background. I have tried things like android:drawableRight="arrowimage", but this does not seem to show anything. Does anyone know how I can include an image in my custom background for a spinner?

Create an XML in a drawable folder with any name for example spinner_bg.xml and add this following lines
<?xml version="1.0"
encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<layer-list>
<item>
<shape>
<gradient android:angle="90"
android:endColor="#ffffff"
android:startColor="#ffffff"
android:type="linear" />
<stroke android:width="1dp"
android:color="#504a4b" />
<corners android:radius="5dp" />
<padding android:bottom="3dp"
android:left="3dp"
android:right="3dp"
android:top="3dp" />
</shape>
</item>
<item>
<bitmap android:gravity="bottom|right"
android:src="#drawable/spinner_ab_default_holo_dark_am" />
// you can use any other image here, instead of default_holo_dark_am
</item>
</layer-list>
</item>
</selector>
Add the following lines to your styles.xml which is inside values folder
<style name="spinner_style" >
<item name="android:background">#drawable/spinner_bg</item>
<item name="android:layout_marginLeft">10dp</item>
<item name="android:layout_marginRight">10dp</item>
<item name="android:layout_marginBottom">10dp</item>
<item name="android:paddingLeft">8dp</item>
<item name="android:paddingTop">5dp</item>
<item name="android:paddingBottom">5dp</item>
</style>
Now add this style to your spinner as
<Spinner android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/spinner_style"
android:popupBackground="#cccccc" />

Aniruddha's solution worked fine for me, when I used JellyBean (4.2.2) or Kitkat (4.4). But unfortunately on Marshmallow (6.0) I have an exception, possible cause explained here: Using drawable resources
I ended up just making a trick and place spinner to LinearLayout with ImageView like this:
<LinearLayout
android:id="#+id/spinner_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<Spinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/your_arrow_drawable"/>
</LinearLayout>

This works for me. You may be missing the default state. Without posting your code it's hard to tell.
<!-- theme -->
<item name="android:dropDownSpinnerStyle">#style/mySpinnerStyle</item>
<!-- style -->
<style name="mySpinnerStyle" parent="android:Widget.Holo.Spinner">
<item name="android:background">#drawable/mySpinner_background</item>
</style>
<!-- mySpinner_background -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false"
android:drawable="#drawable/mySpinner_disabled" />
<item android:state_pressed="true"
android:drawable="#drawable/mySpinner_pressed" />
<item android:state_pressed="false" android:state_focused="true"
android:drawable="#drawable/mySpinner_focused" />
<item android:drawable="#drawable/mySpinner_default" />
</selector>

This has worked for me:
<Spinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Light"
android:spinnerMode="dropdown" />

Set your Image ICON On Spinner :
<item>
<layer-list>
<item>
<color android:color="#android:color/white" />
</item>
<item>
<bitmap android:gravity="center_vertical|right" android:src="#drawable/down_arrow" />
</item>
</layer-list>
</item>

Related

How to set background of MaterialBetterSpinner in android?

This is my Design of campaign_create.xml
Spinner :
<com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner
android:id="#+id/select_city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Select Category"
android:background="#drawable/rounded_white"
android:textColorHint="#ffffff"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_alignParentStart="true"
android:layout_marginTop="9dp" />
#drawable/rounded_white.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#f2f2f2"/>
<stroke android:width="0.1dp"
android:color="#color/white"/>
<corners android:radius="40dp"/>
</shape>
</item>
</selector>
I want to set background of spinner.
It is drawable file. i set property
android:background="#drawable/rounded_white"
but it didn't work.
How to set background of spinner?
It must be working, but you won't be able to check in preview.
Try setting values(Adapter) to spinner and run the code.
But as per your layout I guess you should take Linear or Relative Layout as parent and keeping Imageview and Spinner inside.
i am used simple spinner control and set background used below code..
<Spinner
android:id="#+id/mySpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/name"
android:background="#drawable/button_selector_blue"
/>
and button_selector_blue.xml..
<?xml version="1.0" encoding="utf-8"?>
<item android:state_pressed="false">
<layer-list>
<item>
<shape android:shape="rectangle">
<solid android:color="#color/colorPrimary"/>
<padding android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp"/>
<corners android:radius="5dp" />
</shape>
</item>
</layer-list>
</item>
create a style for spinner like this :
<style name="spinner_style" parent="Base.Widget.AppCompat.Spinner">
<item name="android:background">#drawable/spinner_custom</item>
<item name="android:layout_marginLeft">10dp</item>
<item name="android:layout_marginRight">10dp</item>
<item name="android:layout_marginBottom">10dp</item>
<item name="android:paddingLeft">8dp</item>
<item name="android:paddingTop">5dp</item>
<item name="android:paddingBottom">5dp</item>
<item name="android:popupBackground">#848484</item>
<item name="android:textColor">#000000</item>
</style>
add this style in your spinner :
<Spinner
style="#style/spinner_style"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_margin="5dp" />

Switch stops displaying after adding Thumb/Track drawables

I added switch, I wanted to change its color, so i added following in drawables.
Thumb.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="#color/black" />
<item android:state_pressed="true" android:drawable="#color/tabAccessoryButtonSelected" />
<item android:state_checked="true" android:drawable="#color/tabAccessoryButtonSelected" />
</selector>
Track.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#color/tabAccessoryButtonSelected" />
</selector>
Layout.xml
<Switch
android:layout_width="200dp"
android:layout_height="200dp"
android:id="#+id/switch1"
android:layout_centerHorizontal="true"
android:paddingTop="50dp"
android:text="#string/notification_settings"
android:textColor="#color/white"
android:thumb="#drawable/thumb"
android:track="#drawable/track"
/>
As soon as i added this, switch has stopped displaying in the layout, the text is still there, but the Switch tool is not there.
If i remove Thumb.xml and Track.xml it starts displaying switch.
Kindly guide me what i am doing wrong here.
Thanks
I have modify it now try my solution below.
Layout.xml
<Switch
android:layout_width="200dp"
android:layout_height="200dp"
android:id="#+id/switch1"
android:layout_centerHorizontal="true"
android:paddingTop="50dp"
android:text="#string/notification_settings"
android:textColor="#color/white"
android:thumb="#drawable/thumb"
android:track="#drawable/track"
/>
track.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#drawable/color_thumb" />
<item android:drawable="#drawable/gray_track" />
</selector>
thumb.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="#drawable/gray_track" />
<item android:state_pressed="true" android:drawable="#drawable/color_thumb" />
<item android:state_checked="true" android:drawable="#drawable/color_thumb" />
<item android:drawable="#drawable/gray_track" />
</selector>
color_thumb.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<size android:height="40dp" />
<gradient android:height="40dp" android:startColor="#color/tabAccessoryButtonSelected"" android:endColor="#color/tabAccessoryButtonSelected""/>
</shape>
gray_track.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<size android:height="40dp" />
<gradient android:height="40dp" android:startColor="#color/tabAccessoryButtonSelected" android:endColor="#color/tabAccessoryButtonSelected"/>
</shape>
I know this is an old question but I had the same problem today and the issue seemed to be that the thumb drawable needs a fixed size. The track size seems to adjust itself to the size of the thumb so a thumb with no indicated size won't display and because of this the track won't display either.
If you add an actual xml drawable as a thumb and not just a color you can add a size element to it:
switch_thumb.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/colorSwitchThumb" />
<corners android:radius="#dimen/switch_corner_radius"/>
<size android:height="10dp" android:width="10dp"/>
</shape>

Android doesn't apply background color when a shape is defined

I'm trying to create a style for buttons in my app. In order to do that, I created a drawable file with the corner tag. I also created different styles for different button state (normal and disabled). When I apply the style without setting the background attribute, the button have the correct color. But when I set the background attribute with my drawable file, the text color defined in the style.xml works but not the background color. Here is the code :
values/style.xml :
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:background">#color/background</item>
<item name="android:textColor">#color/text_color</item>
</style>
<style name="ButtonStyle">
<item name="android:background">#color/button_color</item>
</style>
<style name="ButtonDisableStyle">
<item name="android:background">#color/button_color_disabled</item>
<item name="android:textColor">#color/buttonText_color_disabled</item>
</style>
drawable/button_shape.xml :
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
android:radius="5dp" />
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp" />
</shape>
main_activity :
<Button
android:id="#+id/newButton"
android:text="#string/generate_first"
style="#style/ButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="30"
android:background="#drawable/button_shape"
/>
I saw several post about the same problem and I know that it is possible to define the button's background color in the drawable/button_shape.xml but I'd like to keep the style and make it works (I want the shape and the style to be separated).
Do you have any idea about it ?
Finally and after more research, I found a solution. I created 3 xml shape files for the button corresponding to 3 different states. Then I created a 4th xml shape file which includes the 3 others.
Finally, I put a reference to the 4th shape xml file into the button style (in values/style.xml). Here is the code :
drawable/button_disabled.xml :
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#color/buttonText_color_disabled"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<corners android:radius= "5dp" />
</shape>
drawable/button_pressed.xml :
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#color/button_color_pressed"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<corners android:radius= "5dp" />
</shape>
drawable/button_enabled.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#color/button_color"/>
<padding android:left="7dp"
android:top="7dp"
android:right="7dp"
android:bottom="7dp" />
<corners android:radius= "5dp" />
</shape>
drawable/button.xml :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_enabled="false"
android:drawable="#drawable/button_disabled" />
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/button_pressed" />
<item
android:state_enabled="true"
android:drawable="#drawable/button_enabled" />
</selector>
values/style.xml :
<style name="Custom_button" parent="#android:style/Widget.Button">
<item name="android:gravity">center_vertical|center_horizontal</item>
<item name="android:textColor">#color/text_color</item>
<item name="android:textSize">16dip</item>
<item name="android:textStyle">bold</item>
<item name="android:background">#drawable/button</item>
<item name="android:clickable">true</item>
</style>
main_activity :
<Button
android:id="#+id/newButton"
android:text="#string/generate_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_below="#id/app_info"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp"
android:layout_marginBottom="50dp"
style="#style/Custom_button" />

How to set Style for a button in Android?

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>

Android - Selector with border color?

I have a seelctor that I use on my radioButtons. I need to set a border on each one. Why does the selector below not apply border?
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="#integer/short_anim_time">
<item android:drawable="#color/orange"
android:state_pressed="true"/>
<item android:drawable="#color/orange"
android:state_checked="true"/>
<item android:drawable="#color/white"
android:state_focused="true"/>
<item android:drawable="#color/white"/>
<item>
<shape>
<stroke
android:width="2dp"
android:color="#color/orange"/>
<padding
android:left="#dimen/gap"
android:top="#dimen/gap"
android:right="#dimen/gap"
android:bottom="#dimen/gap"/>
</shape>
</item>
</selector>
Simple,
put your border in it's own file.
then add
android:background="#drawable/border"
That's how I got it to work with my image views..
<ImageView
android:id="#+id/iv_icon"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:background="#drawable/border"
android:src="#drawable/foo" />

Categories

Resources