Skip to content Skip to sidebar Skip to footer

How To Customize The Spinner In Android?

i am making a spinner but i am having trouble in putting the image in spinner.. i want the spinner like this: where crust and addon are given but i dont know how to do that ..can a

Solution 1:

In your Spinner you may try a selector xml (spinner_selector.xml) file in your drawable. In this selector xml you can define the images for selected and unselected state of the spinner. As following

<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_pressed="true"android:drawable="@drawable/spinner_pressed" /><itemandroid:drawable="@drawable/spinner_normal" /></selector>

then in your spinner add the selector as background,

    <Spinner
    android:id="@+id/spinner"
    android:layout_width="match_parent"
    android:background="@drawable/spinner_selector"
    android:layout_height="match_parent"
    android:layout_margin="5dp" />

Hope it helps!

For your layout problem, copy the new xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/LinearLayout1"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#FFFFFF"android:orientation="vertical"><FrameLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true" ><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginRight="5dp"android:layout_marginTop="10dp"android:gravity="center"android:orientation="vertical"android:paddingBottom="-50dp" ><!---add your views here--></LinearLayout><ImageViewandroid:id="@+id/imageView_close"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="top|right"android:clickable="true"android:background="@drawable/crss" /></FrameLayout><ScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:padding="10dp"><ImageViewandroid:id="@+id/desimage"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:src="@drawable/logo" /><TextViewandroid:id="@+id/h1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:text="Chicken Pizza Small"android:textSize="21sp"android:textStyle="bold" /><TextViewandroid:id="@+id/h2"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:text="Chicken Pizza Small Combo"android:textColor="#8c8181"android:textSize="16sp" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="5dp"android:layout_marginTop="10dp"android:orientation="horizontal"android:weightSum="10"><Buttonandroid:id="@+id/quant"android:layout_width="0dp"android:layout_height="match_parent"android:layout_marginRight="10dp"android:layout_weight="3"android:background="@drawable/mybutton"android:text="Quantity"android:textAllCaps="false"android:textColor="#ffffff" /><LinearLayoutandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_weight="7"android:background="@drawable/stroke_button"android:orientation="horizontal"android:weightSum="3"><Buttonandroid:id="@+id/incr"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@android:color/transparent"android:text="-"android:textColor="#FFA726"android:textSize="25dp" /><Viewandroid:layout_width="1dp"android:layout_height="match_parent"android:background="#c0c0c0" /><TextViewandroid:id="@+id/value"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:inputType="number"android:text="1" /><Viewandroid:layout_width="1dp"android:layout_height="match_parent"android:background="#c0c0c0" /><Buttonandroid:id="@+id/decr"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@android:color/transparent"android:text="+"android:textColor="#FFA726"android:textSize="25dp" /></LinearLayout></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="5dp"><CheckBoxandroid:id="@+id/sl"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:buttonTint="#c0c0c0"android:text="SL"android:textColor="#8c8181"tools:targetApi="lollipop" /><CheckBoxandroid:id="@+id/s"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:buttonTint="#c0c0c0"android:text="S"android:textColor="#8c8181"tools:targetApi="lollipop" /><CheckBoxandroid:id="@+id/m"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:buttonTint="#c0c0c0"android:text="M"android:textColor="#8c8181"tools:targetApi="lollipop" /><CheckBoxandroid:id="@+id/L"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:buttonTint="#c0c0c0"android:text="l"android:textColor="#8c8181"tools:targetApi="lollipop" /><CheckBoxandroid:id="@+id/f"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:buttonTint="#c0c0c0"android:text="F"android:textColor="#8c8181"tools:targetApi="lollipop" /></LinearLayout><Viewandroid:layout_width="match_parent"android:layout_height="2dp"android:layout_marginBottom="5dp"android:layout_marginTop="5dp"android:background="#c0c0c0" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="35dp"android:layout_marginLeft="7dp"android:layout_marginRight="15dp"android:layout_marginTop="5dp"android:layout_weight="1"android:background="@drawable/stroke_button"><Spinnerandroid:id="@+id/spin_1"android:layout_width="0dp"android:layout_height="fill_parent"android:layout_weight="1"android:background="@android:color/transparent"android:textAppearance="?android:attr/textAppearanceSmall" /><ImageViewandroid:id="@+id/img_1"android:layout_width="wrap_content"android:layout_height="fill_parent"android:layout_gravity="center_vertical"android:layout_margin="5dp"android:src="@drawable/spinpres" /></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="35dp"android:layout_marginLeft="7dp"android:layout_marginRight="15dp"android:layout_marginTop="5dp"android:layout_weight="1"android:background="@drawable/stroke_button"><Spinnerandroid:id="@+id/spin_2"android:layout_width="0dp"android:layout_height="fill_parent"android:layout_weight="1"android:background="@android:color/transparent"android:textAppearance="?android:attr/textAppearanceSmall" /><ImageViewandroid:id="@+id/img_2"android:layout_width="wrap_content"android:layout_height="fill_parent"android:layout_gravity="center_vertical"android:layout_margin="5dp"android:src="@drawable/spinpres" /></LinearLayout></LinearLayout><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:text="SPECIAL INSTRUCTIONS"android:textSize="16sp" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="150dp"android:layout_marginBottom="5dp"android:layout_marginTop="5dp"android:background="@drawable/stroke_button"android:padding="@dimen/fab_margin1"><EditTextandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="@android:color/transparent"android:gravity="top"android:inputType="textCapSentences|textMultiLine"android:lines="5"android:maxLines="5"android:padding="2dp"android:textColor="#c0c0c0" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="5dp"android:layout_marginTop="5dp"><Buttonandroid:id="@+id/canc"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginRight="10dp"android:layout_weight="1"android:background="@drawable/mybutton2"android:text="Cancel"android:textColor="#ffffff" /><Buttonandroid:id="@+id/ok"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_weight="1"android:background="@drawable/mybutton"android:text="Ok"android:textColor="#ffffff" /></LinearLayout></LinearLayout></ScrollView></LinearLayout>

Apologies for wrong indents

Solution 2:

Here, I got the solution please check out your new xml file

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="400dp"android:layout_alignParentTop="true"android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:layout_marginTop="15dp"android:background="#F4F4F4"><!--Main Body of your custom dialog--><ScrollViewandroid:id="@+id/LinearLayout1"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#FFFFFF"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:padding="10dp"><ImageViewandroid:id="@+id/desimage"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:src="@drawable/logo" /><TextViewandroid:id="@+id/h1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:text="Chicken Pizza Small"android:textSize="21sp"android:textStyle="bold" /><TextViewandroid:id="@+id/h2"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:text="Chicken Pizza Small Combo"android:textColor="#8c8181"android:textSize="16sp" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="5dp"android:layout_marginTop="10dp"android:orientation="horizontal"android:weightSum="10"><Buttonandroid:id="@+id/quant"android:layout_width="0dp"android:layout_height="match_parent"android:layout_marginRight="10dp"android:layout_weight="3"android:background="@drawable/mybutton"android:text="Quantity"android:textAllCaps="false"android:textColor="#ffffff" /><LinearLayoutandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_weight="7"android:background="@drawable/stroke_button"android:orientation="horizontal"android:weightSum="3"><Buttonandroid:id="@+id/incr"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@android:color/transparent"android:text="-"android:textColor="#FFA726"android:textSize="25dp" /><Viewandroid:layout_width="1dp"android:layout_height="match_parent"android:background="#c0c0c0" /><TextViewandroid:id="@+id/value"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:inputType="number"android:text="1" /><Viewandroid:layout_width="1dp"android:layout_height="match_parent"android:background="#c0c0c0" /><Buttonandroid:id="@+id/decr"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@android:color/transparent"android:text="+"android:textColor="#FFA726"android:textSize="25dp" /></LinearLayout></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="5dp"><CheckBoxandroid:id="@+id/sl"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:buttonTint="#c0c0c0"android:text="SL"android:textColor="#8c8181"tools:targetApi="lollipop" /><CheckBoxandroid:id="@+id/s"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:buttonTint="#c0c0c0"android:text="S"android:textColor="#8c8181"tools:targetApi="lollipop" /><CheckBoxandroid:id="@+id/m"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:buttonTint="#c0c0c0"android:text="M"android:textColor="#8c8181"tools:targetApi="lollipop" /><CheckBoxandroid:id="@+id/L"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:buttonTint="#c0c0c0"android:text="l"android:textColor="#8c8181"tools:targetApi="lollipop" /><CheckBoxandroid:id="@+id/f"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:buttonTint="#c0c0c0"android:text="F"android:textColor="#8c8181"tools:targetApi="lollipop" /></LinearLayout><Viewandroid:layout_width="match_parent"android:layout_height="2dp"android:layout_marginBottom="5dp"android:layout_marginTop="5dp"android:background="#c0c0c0" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="35dp"android:layout_marginLeft="7dp"android:layout_marginRight="15dp"android:layout_marginTop="5dp"android:layout_weight="1"android:background="@drawable/stroke_button"><Spinnerandroid:id="@+id/spin_1"android:layout_width="0dp"android:layout_height="fill_parent"android:layout_weight="1"android:background="@android:color/transparent"android:textAppearance="?android:attr/textAppearanceSmall" /><ImageViewandroid:id="@+id/img_1"android:layout_width="wrap_content"android:layout_height="fill_parent"android:layout_gravity="center_vertical"android:layout_margin="5dp"android:src="@drawable/spinpres" /></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="35dp"android:layout_marginLeft="7dp"android:layout_marginRight="15dp"android:layout_marginTop="5dp"android:layout_weight="1"android:background="@drawable/stroke_button"><Spinnerandroid:id="@+id/spin_2"android:layout_width="0dp"android:layout_height="fill_parent"android:layout_weight="1"android:background="@android:color/transparent"android:textAppearance="?android:attr/textAppearanceSmall" /><ImageViewandroid:id="@+id/img_2"android:layout_width="wrap_content"android:layout_height="fill_parent"android:layout_gravity="center_vertical"android:layout_margin="5dp"android:src="@drawable/spinpres" /></LinearLayout></LinearLayout><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:text="SPECIAL INSTRUCTIONS"android:textSize="16sp" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="150dp"android:layout_marginBottom="5dp"android:layout_marginTop="5dp"android:background="@drawable/stroke_button"android:padding="@dimen/fab_margin1"><EditTextandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="@android:color/transparent"android:gravity="top"android:inputType="textCapSentences|textMultiLine"android:lines="5"android:maxLines="5"android:padding="2dp"android:textColor="#c0c0c0" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="5dp"android:layout_marginTop="5dp"><Buttonandroid:id="@+id/canc"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginRight="10dp"android:layout_weight="1"android:background="@drawable/mybutton2"android:text="Cancel"android:textColor="#ffffff" /><Buttonandroid:id="@+id/ok"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_weight="1"android:background="@drawable/mybutton"android:text="Ok"android:textColor="#ffffff" /></LinearLayout></LinearLayout></ScrollView></RelativeLayout><LinearLayoutandroid:id="@+id/llTop"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_alignParentTop="true"android:orientation="vertical"><ImageButtonandroid:id="@+id/btnCancel"android:layout_width="20dp"android:layout_height="20dp"android:layout_below="@+id/btnBookK"android:background="@null"android:src="@drawable/close_button_image" /></LinearLayout></RelativeLayout>

And if you use transparent background image it would look good ; And don't forget to write the following code in the builder

AlertDialogyourDialog= builder.Create() ;
yourDialog.getWindow().setBackgroundDrawable(newColorDrawable(Color.TRANSPARENT));
yourDialog.show();

This code works, I tried. Hope it helps!

Solution 3:

here is my solution

<?xml version="1.0" encoding="utf-8"?><ScrollViewxmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/LinearLayout1"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#FFFFFF"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"android:padding="10dp"><ImageViewandroid:id="@+id/desimage"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:src="@drawable/logo" /><TextViewandroid:id="@+id/h1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:text="Chicken Pizza Small"android:textSize="21sp"android:textStyle="bold" /><TextViewandroid:id="@+id/h2"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:text="Chicken Pizza Small Combo"android:textColor="#8c8181"android:textSize="16sp" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="5dp"android:layout_marginTop="10dp"android:orientation="horizontal"android:weightSum="10"><Buttonandroid:id="@+id/quant"android:layout_width="0dp"android:layout_height="match_parent"android:layout_marginRight="10dp"android:layout_weight="3"android:background="@drawable/mybutton"android:text="Quantity"android:textAllCaps="false"android:textColor="#ffffff" /><LinearLayoutandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_weight="7"android:background="@drawable/stroke_button"android:orientation="horizontal"android:weightSum="3"><Buttonandroid:id="@+id/incr"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@android:color/transparent"android:text="-"android:textColor="#FFA726"android:textSize="25dp" /><Viewandroid:layout_width="1dp"android:layout_height="match_parent"android:background="#c0c0c0" /><TextViewandroid:id="@+id/value"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:gravity="center"android:inputType="number"android:text="1" /><Viewandroid:layout_width="1dp"android:layout_height="match_parent"android:background="#c0c0c0" /><Buttonandroid:id="@+id/decr"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="1"android:background="@android:color/transparent"android:text="+"android:textColor="#FFA726"android:textSize="25dp" /></LinearLayout></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="5dp"><CheckBoxandroid:id="@+id/sl"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:buttonTint="#c0c0c0"android:text="SL"android:textColor="#8c8181"tools:targetApi="lollipop" /><CheckBoxandroid:id="@+id/s"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:buttonTint="#c0c0c0"android:text="S"android:textColor="#8c8181"tools:targetApi="lollipop" /><CheckBoxandroid:id="@+id/m"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:buttonTint="#c0c0c0"android:text="M"android:textColor="#8c8181"tools:targetApi="lollipop" /><CheckBoxandroid:id="@+id/L"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:buttonTint="#c0c0c0"android:text="l"android:textColor="#8c8181"tools:targetApi="lollipop" /><CheckBoxandroid:id="@+id/f"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:buttonTint="#c0c0c0"android:text="F"android:textColor="#8c8181"tools:targetApi="lollipop" /></LinearLayout><Viewandroid:layout_width="match_parent"android:layout_height="2dp"android:layout_marginBottom="5dp"android:layout_marginTop="5dp"android:background="#c0c0c0" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="35dp"android:layout_marginLeft="7dp"android:layout_marginRight="15dp"android:layout_marginTop="5dp"android:layout_weight="1"android:background="@drawable/stroke_button"><Spinnerandroid:id="@+id/spin_1"android:layout_width="0dp"android:layout_height="fill_parent"android:layout_weight="1"android:background="@android:color/transparent"android:textAppearance="?android:attr/textAppearanceSmall" /><ImageViewandroid:id="@+id/img_1"android:layout_width="wrap_content"android:layout_height="fill_parent"android:layout_gravity="center_vertical"android:layout_margin="5dp"android:src="@drawable/spinpres" /></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="35dp"android:layout_marginLeft="7dp"android:layout_marginRight="15dp"android:layout_marginTop="5dp"android:layout_weight="1"android:background="@drawable/stroke_button"><Spinnerandroid:id="@+id/spin_2"android:layout_width="0dp"android:layout_height="fill_parent"android:layout_weight="1"android:background="@android:color/transparent"android:textAppearance="?android:attr/textAppearanceSmall" /><ImageViewandroid:id="@+id/img_2"android:layout_width="wrap_content"android:layout_height="fill_parent"android:layout_gravity="center_vertical"android:layout_margin="5dp"android:src="@drawable/spinpres" /></LinearLayout></LinearLayout><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="5dp"android:text="SPECIAL INSTRUCTIONS"android:textSize="16sp" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="150dp"android:layout_marginBottom="5dp"android:layout_marginTop="5dp"android:background="@drawable/stroke_button"android:padding="@dimen/fab_margin1"><EditTextandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="@android:color/transparent"android:gravity="top"android:inputType="textCapSentences|textMultiLine"android:lines="5"android:maxLines="5"android:padding="2dp"android:textColor="#c0c0c0" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="5dp"android:layout_marginTop="5dp"><Buttonandroid:id="@+id/canc"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginRight="10dp"android:layout_weight="1"android:background="@drawable/mybutton2"android:text="Cancel"android:textColor="#ffffff" /><Buttonandroid:id="@+id/ok"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:layout_weight="1"android:background="@drawable/mybutton"android:text="Ok"android:textColor="#ffffff" /></LinearLayout></LinearLayout></ScrollView>

here is stroke_button.xml

<?xml version="1.0" encoding="utf-8"?><shapexmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle"><solidandroid:color="#FFFFFF" /><cornersandroid:radius="20dp" /><strokeandroid:width="1dp"android:color="#c0c0c0" /></shape>

now change the images the way u want...enjot..happy coding

Post a Comment for "How To Customize The Spinner In Android?"