How to Create Android Spinner Without Down Triangle on the Right Side of the Widget

How to create android spinner without down triangle on the right side of the widget

One thing you can do is take Spinner's source code from android code base, together with related layouts and resources, and use them to create your own custom widget (probably you will only need to remove the arrow from the layout and tweak the code a little depending on your needs).

EDIT:
You're right, following that post it was actually really simple :) You have to do two things.
First, create a styles.xml file under res/values, open it and add the following:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style parent="@android:style/Widget.Spinner" name="SpinnerAsEditText">
<item name="android:background">@android:drawable/edit_text</item>
</style>
</resources>

Next, in your layout, add the Spinner like this:

<Spinner style="@style/SpinnerAsEditText" anyOtherAttributeYouNeed="..."></Spinner>

That's it, now the spinner will look like a plain EditText, without that unuseful and annoying down arrow.

Android - Remove Spinner Dropdown Arrow

This may help You

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style parent="@android:style/Widget.Spinner" name="SpinnerwithNoArrow">
<item name="android:background">@android:drawable/edit_text</item>
</style>
</resources>

Use this style in ur spinner

How to create custom spinner like border around the spinner with down triangle on the right side?

Spinner

<Spinner
android:id="@+id/To_Units"
style="@style/spinner_style" />

style.xml

    <style name="spinner_style">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">@drawable/gradient_spinner</item>
<item name="android:layout_margin">10dp</item>
<item name="android:paddingLeft">8dp</item>
<item name="android:paddingRight">20dp</item>
<item name="android:paddingTop">5dp</item>
<item name="android:paddingBottom">5dp</item>
<item name="android:popupBackground">#DFFFFFFF</item>
</style>

gradient_spinner.xml (in drawable folder)

<?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>

@drawable/spinner_arrow is your bottom right corner image

How to set dropdown arrow in spinner?

copy and paste this xml instead of your xml

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/back1"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="55dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:background="@drawable/red">

<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dropDownWidth="fill_parent"
android:background="@android:drawable/btn_dropdown"
/>

</LinearLayout>

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="55dp"
android:layout_alignLeft="@+id/linearLayout1"
android:layout_alignRight="@+id/linearLayout1"
android:layout_below="@+id/linearLayout1"
android:layout_marginTop="25dp"
android:background="@drawable/red"
android:ems="10"
android:hint="enter card number" >

<requestFocus />
</EditText>

<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="55dp"
android:layout_alignLeft="@+id/editText1"
android:layout_alignRight="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="33dp"
android:orientation="horizontal"
android:background="@drawable/red">

<Spinner
android:id="@+id/spinner3"
android:layout_width="72dp"
android:layout_height="wrap_content"
android:background="@android:drawable/btn_dropdown"
/>

<Spinner
android:id="@+id/spinner2"
android:layout_width="72dp"
android:layout_height="wrap_content"
android:background="@android:drawable/btn_dropdown"
/>

<EditText
android:id="@+id/editText2"
android:layout_width="22dp"
android:layout_height="match_parent"
android:layout_weight="0.18"
android:ems="10"
android:hint="enter cvv" />

</LinearLayout>

<LinearLayout
android:id="@+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="55dp"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/linearLayout2"
android:layout_below="@+id/linearLayout2"
android:layout_marginTop="26dp"
android:orientation="vertical"
android:background="@drawable/red" >
</LinearLayout>

<Spinner
android:id="@+id/spinner4"
android:layout_width="15dp"
android:layout_height="18dp"
android:layout_alignBottom="@+id/linearLayout3"
android:layout_alignLeft="@+id/linearLayout3"
android:layout_alignRight="@+id/linearLayout3"
android:layout_alignTop="@+id/linearLayout3"
android:background="@android:drawable/btn_dropdown"
/>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/linearLayout3"
android:layout_marginTop="18dp"
android:text="Add Amount"
android:background="@drawable/buttonsty"/>
</RelativeLayout>

Change Spinner dropdown icon

Try applying following style to your spinner using

style="@style/SpinnerTheme"

//Spinner Style:

<style name="SpinnerTheme" parent="android:Widget.Spinner">
<item name="android:background">@drawable/bg_spinner</item>
</style>

//bg_spinner.xml
Replace the arrow_down_gray with your arrow

<?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>

Android Make Spinner look like ImageView

After Nun'e Chai's comment I did made a PopupWindow that triggers on the click of an ImageButton. So thanks for that, Nun'e Chai. For those interested, below is the code:

In acitivity_main.xml:

<ImageButton
android:id="@+id/ibtnSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:contentDescription="@string/checkbox_content_description"
android:src="@drawable/checkbox_unchecked"
android:background="@drawable/transparent_background" />

transparent_background.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@android:color/transparent" />
<item android:state_pressed="true" android:drawable="@android:color/transparent" />
<item android:drawable="@android:color/transparent" />
</selector>

spinner_popup.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinnerLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ImageButton
android:id="@+id/simgUnchecked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/checkbox_unchecked"
android:contentDescription="@string/checkbox_content_description"
android:background="@drawable/transparent_background" />

<ImageButton
android:id="@+id/simgChecked"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/checkbox_checked"
android:contentDescription="@string/checkbox_content_description"
android:background="@drawable/transparent_background" />

<ImageButton
android:id="@+id/simgError"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/checkbox_error"
android:contentDescription="@string/checkbox_content_description"
android:background="@drawable/transparent_background" />

<ImageButton
android:id="@+id/simgPartly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/checkbox_partly"
android:contentDescription="@string/checkbox_content_description"
android:background="@drawable/transparent_background" />

</LinearLayout>

In MainActivity.java:

private Point p;
private ImageButton spinnerButton;
private PopupWindow spinner;

protected void onCreate(Bundle savedInstanceState) {
...

addListenerToSpinnerButton();
}

private void addListenerToSpinnerButton(){
spinnerButton = (ImageButton) findViewById(R.id.ibtnSpinner);
spinnerButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if(p != null)
showSpinner(MainActivity.this, p);
}
});
}

// Get the x and y position after the button is drawn on screen
// (It's important to note that we can't get the position in the onCreate(),
// because at that stage most probably the view isn't drawn yet, so it will return (0, 0))
@Override
public void onWindowFocusChanged(boolean hasFocus){
int[] location = new int[2];
ImageButton btn = (ImageButton) findViewById(R.id.ibtnPopup);

// Get the x, y location and store it in the location[] array
btn.getLocationOnScreen(location);

// Initialize the Point with x, and y positions
p = new Point();
p.x = location[0];
p.y = location[1];
}

// The method that displays the spinner
private void showSpinner(final ActionBarActivity context, Point p){
// Inflate the spinner.xml
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.spinnerLayout);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.spinner, viewGroup);

// Creating the PopupWindow
spinner = new PopupWindow(context);
spinner.setContentView(layout);
spinner.setWindowLayoutMode(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
spinner.setFocusable(true);

// Clear the default translucent background
// TODO: Fix deprecated to BitmapDrawable(Resource, Bitmap)
spinner.setBackgroundDrawable(new BitmapDrawable());

// Displaying the spinner at the specified location, + offsets
spinner.showAtLocation(layout, Gravity.NO_GRAVITY, p.x, p.y);

// Getting a reference to the ImageButtons, and close the spinner when clicked
ImageButton optionUnchecked = (ImageButton) layout.findViewById(R.id.simgUnchecked);
optionUnchecked.setOnClickListener(spinnerOnClickListener);
optionUnchecked.setTag(R.drawable.checkbox_unchecked);

ImageButton optionChecked = (ImageButton) layout.findViewById(R.id.simgChecked);
optionChecked.setOnClickListener(spinnerOnClickListener);
optionChecked.setTag(R.drawable.checkbox_checked);

ImageButton optionError = (ImageButton) layout.findViewById(R.id.simgError);
optionError.setOnClickListener(spinnerOnClickListener);
optionError.setTag(R.drawable.checkbox_error);

ImageButton optionPartly = (ImageButton) layout.findViewById(R.id.simgPartly);
optionPartly.setOnClickListener(spinnerOnClickListener);
optionPartly.setTag(R.drawable.checkbox_partly);
}
private OnClickListener spinnerOnClickListener = new OnClickListener(){
@Override
public void onClick(View v){
// Get the id of the ImageButton that is clicked
ImageButton btn = (ImageButton) v;
int id = (Integer) btn.getTag(); // We are sure it's an Integer, so the cast from Object to int is safe

// Change the ImageButton that triggered the spinner to the same Image
spinnerButton.setImageResource(id);

// Close the spinner
if(spinner != null)
spinner.dismiss();
}
};

...

Trying to get my custom spinner look to work

If you are using your own drawable you need to use:

@drawable/spinner_updated

The @android: directs the app to look in Android's built-in resources not your own.



Related Topics



Leave a reply



Submit