How to remove arrow from the spinner when i click On spinner? - android

I tried this below code its showing normal effects.I want when i click on spinner the down should hide..
<?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="#B3BBCC" android:startColor="#E8EBEF" android:type="linear" />
<stroke android:width="1dp" android:color="#000000" />
<corners android:radius="4dp" />
<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_arrow" />
</item>
</layer-list>
</item>
</selector>
Thank is Advance...

I have made my custom spinner like this follow these step:
Step 1: create one Drawable resource file inside your drawable folder say spinnerbg.xml and write below code in your xml file.
<?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="#color/spinner"
android:startColor="#color/spinner"
android:type="linear" />
<corners android:radius="0dp" />
</shape>
</item>
<item>
<bitmap android:gravity="bottom|right"
android:src="#drawable/dropdown" />
</item>
</layer-list>
</item>
</selector>
Step:2 Inside your style.xml write below code
<style name="spinner_style" >
<item name="android:background">#drawable/spinnerbg</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>
step:3 And finallay inside your spinner use style like this.
<Spinner
style="#style/spinner_style"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</Spinner>

Related

Displaying the arrow of an Android spinner

I have a spinner, whose background is defined in a drawable file:
<style name="spinner_style" parent="android:Widget.Material.Light.Spinner">
<item name="android:background">#drawable/background_spinner</item>
</style>
And the style:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/red" />
<corners android:radius="3dp" />
</shape>
The problem is that the arrow isn't displayed. It's due to: <item name="android:background">#drawable/background_spinner</item>.
How could I add it, for example within the drawable file?
Solution:
You should define an <layer-list> to do so:
<?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="0.33dp" android:color="#0fb1fa" />
<corners android:radius="0dp" />
<padding android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp" />
</shape>
</item>
<item android:right="5dp">
<bitmap android:gravity="center_vertical|right" android:src="#drawable/arrow_down_gray" />
</item>
</layer-list>
</item>
</selector>
Then, replace the line: android:src="#drawable/arrow_down_gray" with your arrow drawable.
Finally, apply this as your style="#style/spinner_style" in your spinner.
Explaination: <layer-list> info can be found here: https://developer.android.com/guide/topics/resources/drawable-resource#LayerList and <bitmap> are used to draw an image on the screen. We choose the drawable image and set the gravity.
More info on this may be other senior experts can provide you.
Hope it helps.

How can I make this background as an xml drawable?

I want give my app a background and I want create this in an xml file, not an image.
Can someone help me? please.
I got It:
<item android:top="200px">
<shape>
<solid android:color="#FF954CFB" />
</shape>
</item>
<item android:height="10dp">
<shape>
<solid android:color="#color/colorWhite" />
</shape>
</item>
<item android:top="-500dp" android:bottom="300dp" android:left="-100dp" android:right="-100dp">
<shape>
<corners android:radius="400dp" />
<solid android:color="#color/colorWhite" />
</shape>
</item>
XML file:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="200px">
<shape>
<solid android:color="#6bcbf4" />
</shape>
</item>
<item android:bottom="900px">
<shape android:shape="oval">
<solid android:color="#FFF8F8F8"
/>
</shape>
</item>
</layer-list>

android flat button

i have buttons with rounded corners, how to make them looks like this :
do i need to define my own style? I don't want to change my code, only XML resources, if it possible how to apply this(border radius = 0) style to my buttons?
http://android-holo-colors.com/
This service generate Holo-styles for checked components(button is your case) and then you can download zip with xml's.
Use it to fast&easy implement, or code samples.
I think the only way for you to get what you want it to write XML to make it possible.
In the drawable folder, create an xml file that will represent the shape of your button.
for example
<?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="#ff00ff" />
<stroke
android:width="1dp"
android:color="#20f8f6" />
<corners
android:radius="6dp" /> // set to 0 or erase it for sharp corners
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#ff00ff"
android:endColor="#992f2f"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#992f2f" />
<corners
android:radius="6dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
then you need to create a style in your styles.xml file
<resources>
<style name="button_text" >
<item name="android:layout_width" >fill_parent</item>
<item name="android:layout_height" >wrap_content</item>
<item name="android:textColor" >#ffffff</item>
<item name="android:gravity" >center</item>
<item name="android:layout_margin" >3dp</item>
<item name="android:textSize" >30dp</item>
<item name="android:textStyle" >bold</item>
<item name="android:shadowColor" >#000000</item>
<item name="android:shadowDx" >1</item>
<item name="android:shadowDy" >1</item>
<item name="android:shadowRadius" >2</item>
</style>
</resources>
and when you declare your button in the xml
<Button
android:background="#drawable/red_button"
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Styled Button"
style="#style/button_text" />
Check out this github post here.
Mainly check out the library provided there and added to that source code.
SnapShot:
add xml(example.xml) in folder drawable:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:topLeftRadius="4dip"
android:topRightRadius="4dip"
android:bottomLeftRadius="4dip"
android:bottomRightRadius="4dip"/>
<solid android:color="#1FC1BA"/>
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle">
<corners android:topLeftRadius="4dip"
android:topRightRadius="4dip"
android:bottomLeftRadius="4dip"
android:bottomRightRadius="4dip"/>
<solid android:color="#26262C"/>
</shape>
</item>
<item android:state_pressed="true">
<shape android:shape="rectangle">
<corners android:topLeftRadius="4dip"
android:topRightRadius="4dip"
android:bottomLeftRadius="4dip"
android:bottomRightRadius="4dip"/>
<solid android:color="#1B978F"/>
</shape>
</item>
</selector>
on your folder layout(activity_main.xml) in the button
android:background="#drawable/example"
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="exampleText"
android:layout_gravity="center"
android:background="#drawable/example"
android:textColor="#ffffffff" />
I think below code is exactly what you are looking for.
<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:startColor="#32a5cf"
android:endColor="#32a5cf"
android:angle="270" />
<stroke
android:width="7dp"
android:color="#9032a5cf"
/>
<corners
android:radius="6dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>

Android Using layer-list for button selector

How do we use a layer-list as a drawable for a button.
I have a button:
<item android:state_pressed="true">
<shape>
<gradient android:endColor="#color/white"
android:startColor="#color/grey_blue_light" android:angle="90" />
<stroke android:width="1dp" android:color="#color/aqua_blue" />
<corners android:radius="3dp" />
<padding android:left="10dp" android:top="10dp"
android:right="10dp" android:bottom="10dp" />
</shape>
</item>
<item android:state_focused="true">
</item>
<item>
</item>
Now I need a layer-list to be used as a shape when say button state is pressed:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#color/aqua_blue" />
</shape>
</item>
<item android:top="1dp" android:left="1dp" android:right="1dp" android:bottom="1dp">
<shape android:shape="oval">
<solid android:color="#color/aqua_blue" />
</shape>
</item>
How do we use this layer list in the button selector?
Step-1
create three different layer_list xml under drawable folder for three different state of button. example the name of those xml is layer1.xml, layer2.xml, layer3.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<gradient
android:angle="270"
android:startColor="#0000ff"
android:endColor="#0000dd"
android:type="linear"
/>
</shape>
</item>
</layer-list>
Step-2
create a selector xml named as btn_background.xml and pass the layer_list xml in drawable attribute
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/layer1">
</item>
<item android:state_focused="true" android:drawable="#drawable/layer2">
</item>
<item android:drawable="#drawable/layer3">
</item>
</selector>
step-3
Set the selector xml as background of the button android:background="#drawable/btn_background"
Mygod's answer is actually better than the accepted one. It works and only needs one xml file. Here's something similar that I did. It sets the background under a drawable with transparencies. The same could be done with a background and a shape:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<layer-list>
<item android:drawable="#color/Black"/>
<item android:drawable="#drawable/notes_white"/>
</layer-list>
</item>
<item>
<layer-list>
<item android:drawable="#color/White"/>
<item android:drawable="#drawable/notes_black"/>
</layer-list>
</item>
</selector>
Just replace the shape tag with your layer-list and everything will work fine.

How to apply shape and selector simultaneously for Button?

I have applied a shape for a button like:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient android:startColor="#DD000000" android:endColor="#DD2d2d2d" android:angle="90"></gradient>
<corners android:radius="15dip"></corners>
</shape>
Now I want to use a selector like:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/active"
android:state_pressed="true" />
<item android:drawable="#drawable/passive"/>
for this Button as well. Is it possible ...???
use this way:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>.......</shape>
</item>
..........
..........
</selector>
Detailed to the point answer
Create a color resource in
res/values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="yellow" type="color">#F7B500</item>
<item name="yellow_dark" type="color">#AC7E00</item>
<integer-array name="androidcolors">
<item>#color/yellow</item>
<item>#color/yellow_dark</item>
</integer-array>
</resources>
Create a drawable at
res/drawable/bg_yellow_round.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/yellow" />
<stroke
android:width="2dp"
android:color="#color/yellow" />
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
</shape>
Create another drawable, you want for transition at same place and name it
res/drawable/bg_yellow_dark_round.xml
<solid android:color="#color/yellow_dark" />
<stroke
android:width="2dp"
android:color="#color/yellow_dark" />
<corners
android:bottomLeftRadius="20dp"
android:bottomRightRadius="20dp"
android:topLeftRadius="20dp"
android:topRightRadius="20dp" />
Now create a color state list at
res/color/btn_selector_yellow.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="#android:integer/config_shortAnimTime">
<item android:drawable="#color/yellow" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="#drawable/bg_yellow_dark_round" android:state_pressed="true"/>
<item android:drawable="#drawable/bg_yellow_round"/>
</selector>
Now set it to your button as following
<Button
android:id="#+id/button1"
android:layout_width="248dp"
android:layout_height="44dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp"
android:layout_marginTop="20dp"
android:background="#color/btn_selector_yellow"
android:text="AZ_ is so cool" />
Now this will do transition from
to
.
shape.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/star_off"/>
<corners android:radius="5dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:exitFadeDuration="#android:integer/config_mediumAnimTime">
<item android:drawable="#color/tab_focused" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="#color/tab_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/shape"/>
</selector>
You can also create a shape that is using a selector inside. If your shape is just changing its color in different states, this is a lot cleaner.
color/color_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#color/blue_dark" android:state_pressed="true" />
<item android:color="#color/blue_light" />
</selector>
drawable/shape.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/color_selector" />
<corners android:bottomLeftRadius="6dip" android:bottomRightRadius="6dp" />
<padding android:bottom="0dip" android:left="0dip" android:right="0dip" android:top="0dip" />
</shape>
Well I know it's way too late
But here is a solved example
<TextView
android:id="#+id/txt_out_going_calls"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="04dp"
android:layout_weight="1"
android:background="#drawable/header_text_view_selector"
android:gravity="center"
android:text="#string/outgoing_calls_tab_button_text"
android:textColor="#color/home_text_color" />
and my header_text_view_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#color/home_fragment_tab_color_selected"/>
<corners android:radius="25dip" />
<padding android:bottom="08dip" android:left="9dip" android:right="9dip" android:top="08dip" />
</shape>
</item>
<item android:state_selected="false">
<shape>
<solid android:color="#color/home_fragment_tab_color_simple"/>
<corners android:radius="25dip" />
<padding android:bottom="08dip" android:left="9dip" android:right="9dip" android:top="08dip" />
</shape>
</item>
</selector>
So basically i m creating a rounded textview with selector. Here i m handling only state_selected and not_selected. Hope it helps
This is my way, and it works!
<item android:state_pressed="true">
<shape android:shape="oval">
<gradient android:centerX=".6"
android:centerY=".40"
android:endColor="#color/colorPrimary"
android:gradientRadius="20"
android:startColor="#color/colorPrimary"
android:type="radial" />
<stroke android:width="1dp"
android:color="#FFFFFF" />
<size android:width="55dp"
android:height="55dp" />
</shape>
</item>
<item android:state_focused="false">
<shape android:shape="oval">
<gradient android:centerX=".6"
android:centerY=".40"
android:endColor="#android:color/transparent"
android:gradientRadius="20"
android:startColor="#android:color/transparent"
android:type="radial" />
<stroke android:width="1dp"
android:color="#FFFFFF" />
<size android:width="55dp"
android:height="55dp" />
</shape>
</item>
My example is a circle button with state_pressed.
code bellow:
<?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="oval">
<solid android:color="#color/light_primary_color" />
</shape>
</item>
<item>
<shape android:shape="oval">
<solid android:color="#color/accent_color" />
</shape>
</item>
</selector>
Use your shape name as you use any image and use it selector as you use image. Try out you will not face any problem. Is that what you were asking?
To be more reusable, you can set states on single properties. Avoid duplicating your shapes
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item>
<shape android:shape="rectangle" >
<corners android:radius="5dp"/>
<solid
android:state_enabled="false"
android:color="#color/transparent"
/>
<solid
android:state_enabled="true"
android:color="#color/background"
/>
<stroke
android:width="#dimen/dividerHeight"
android:color="#color/dividerLight"
/>
</shape>
</item>
</selector>
I was able to set background programmatically once disabled using this method.

Categories

Resources