Give spinner items the width of its dropdown

android spinner dropdown style example
android drop down list without spinner
android spinner dropdown position
spinner android
androidx spinner
android spinner dropdown width match_parent
android-spinner dropdown height
android material spinner

I am having an issue with a spinner in my layout.

The issue is that my items have very small width and on a small screen it is pretty hard do click them.

Check the picture for idea.

Is there no easy way of setting the item width to the actual dropdownmenue width?

<android.support.v7.widget.AppCompatSpinner
    android:id="@+id/spellSpinner"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:layout_marginStart="10dp"
    android:layout_marginEnd="10dp"
    android:dropDownWidth="match_parent"
    android:spinnerMode="dropdown"
    android:popupBackground="#fff"
    android:background="#8A8A8A"/>

My Java Code:

spinnerSpells = findViewById(R.id.spellSpinner);
ArrayAdapter < CharSequence > adapter = ArrayAdapter.createFromResource(spellActivity.this, R.array.dropdownCategory, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerSpells.setAdapter(adapter);

spinnerSpells.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  @Override
  public void onItemSelected(AdapterView << ? > adapterView, View view, int i, long l) {
    getSpellList(spinnerSpells.getSelectedItem().toString(), switchSpells.isChecked());
    // do some other stuff [...]
  }

  @Override
  public void onNothingSelected(AdapterView << ? > adapterView) {
    getSpellList(spinnerSpells.getSelectedItem().toString(), switchSpells.isChecked());
   // do some other stuff [...]
  }
});

With best regards CG

P.S. I really did not find any post in the forum about that matter.

To be honest they answer did not really help me, gave me good hint though. I found a way how to solve my problem on my own (also implemented images infront of the labels).

Basically it is the right call to inflate the spinner with a dedicated xml file. I post my code:

Created a new Class for my adater:

package edmt.dev.androidgridlayout;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class xCustomSpinnerAdapater extends ArrayAdapter<String> {

    Context mContext;
    String[] spinnerNames;
    int[] spinnerImages;

    public xCustomSpinnerAdapater( Context mContext, String[] spinnerNames, int[] spinnerImages) {
        super(mContext, R.layout.x_custom_spinner, spinnerNames);
        this.mContext = mContext;
        this.spinnerNames = spinnerNames;
        this.spinnerImages = spinnerImages;
    }


    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

            LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(R.layout.x_custom_spinner,null);
            TextView tvSpinnerText = row.findViewById(R.id.tvSpinnerText);
            ImageView ivSpinnerImage = row.findViewById(R.id.ivSpinnerImage);

            tvSpinnerText.setText(spinnerNames[position]);
            ivSpinnerImage.setImageResource(spinnerImages[position]);

        return row;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.x_custom_spinner,null);
        TextView tvSpinnerText = row.findViewById(R.id.tvSpinnerText);
        ImageView ivSpinnerImage = row.findViewById(R.id.ivSpinnerImage);

        tvSpinnerText.setText(spinnerNames[position]);
        ivSpinnerImage.setImageResource(spinnerImages[position]);

        return row;
    }
}

I am having an issue with a spinner in my layout. The issue is that my items have very small width and on a small screen it is pretty hard do click them. Check the  And then change your declaration of the spinner to use the R.layout.spinner_item: ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.planets_array, R.layout.spinner_item); spinner.setAdapter(adapter); This is the result: If you want to customize the dropdown list items you will need to create a new layout file. Let’s call it

use a custom XML file for your spinner item.

say spinner_item.xml and give the customized text size and color if u want

<?xml version="1.0" encoding="utf-8"?>

<TextView  
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" 
android:layout_height="wrap_content"
android:textSize="20sp"
android:gravity="left"  
android:textColor="#FF0000"         
android:padding="5dip"
/>

use this file to show your spinner items like:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
R.layout.spinner_item,list);

The items in the Spinner come from the Adapter associated with this view. android:dropDownWidth, Width of the dropdown in spinnerMode="dropdown". this ViewGroup's drawable states also include its children's drawable states. Set the width of the spinner's popup window of choices in pixels. Spinners. Spinners provide a quick way to select one value from a set. In the default state, a spinnershows its currently selected value. Touching the spinner displays a dropdown menu with all otheravailable values, from which the user can select a new one. You can add a spinner to your layout with the Spinnerobject.

Create a separate layout for spinner textview

R.layout.spinner_text

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1"
    android:maxLines="1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="your_size_in_dp" />

and set your adapter as follows

ArrayAdapter < CharSequence > adapter = ArrayAdapter.createFromResource(spellActivity.this, R.array.dropdownCategory, R.layout.spinner_text);

Description: If one puts multiple items in a Spinner and sets its width to give the exposed dropdown menu the width of the largest item,  Spinner allows you to select an item from a drop down menu For example. When you are using Gmail application you would get drop down menu as shown below, you need to select an item from a drop down menu.

Use attribute in xml

    android:dropDownWidth="fill_parent| match_parent| wrap_content"
    android:spinnerMode="dropdown"

For More Click : https://developer.android.com/reference/android/widget/Spinner.html#setDropDownWidth(int)

Spinner custom style * 1,Spinner TextView style in: item [Select] * 2 Set various properties in the spinner layout to achieve the effect, and increase the listening. spinnerMode="dialog" 2,Drop down width: default width and spinner Almost. Fully customized spinner: basically, it's all code implementation. In Android, you can use “ android.widget.Spinner ” class to render a dropdown box selection list. Spinner is a widget similar to a drop-down list for selecting items. In this tutorial, we show you how to do the following tasks : Render a Spinner in XML, and load the selection items via XML file also. Render another Spinner in XML, and load

  1. spinner array list Let it be 'dropdownCategory.xml'

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string-array name="dropdownCategory">
            <item>All</item>
            <item>Black</item>
            <item>Blue</item>
            <item>Green</item>
            <item>Red</item>
        </string-array>
    </resources>
    
  2. layout having a spinner

    <android.support.v7.widget.AppCompatSpinner
     android:id="@+id/spellSpinner"
     android:layout_width="0dp"
     android:layout_height="match_parent"
     android:layout_weight="1"
     android:layout_marginStart="10dp"
     android:layout_marginEnd="10dp"
     android:dropDownWidth="wrap_content"
     android:spinnerMode="dropdown"
     android:popupBackground="#fff"
     android:background="#8A8A8A"/>
    
  3. In activity.java class within onCreate

    Spinner spinnerSpells = findViewById(R.id.spellSpinner);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(spellActivity.this, R.array.dropdownCategory, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerSpells.setAdapter(adapter);
    
    spinnerSpells.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
     // do something [...]
     }
    
    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {
       // do something [...]
    });
    

I wish this will help you out

The items in the Spinner come from the Adapter associated with this view. from the spinner widget for positioning the dropdown in spinnerMode="dropdown". Set the width of the spinner's popup window of choices in pixels. Measure the view and its content to determine the measured width and the measured height. You can use the image editor like photoshop to increase or decrease the size of the dropdown image or to change the color of the image. I have set the drop down arrow image with the height and width as 40 (40 * 40 image) Size of your image should increase or decrease with respective to the original size of the spinner.

Only measure this many items to get a decent max width. Use a dropdown anchored to the Spinner for selecting spinner options. */ If it's a huge data set. This tutorial will give you a hands on experience in using Android Spinner as a drop down menu, passing data using android bundle and showing popup notification using android toast. We will create an android application that consists of a simple spinner that allows selecting an item from a drop down list. We will display static data in the spinner.

Spinner spinner = new Spinner(this); //Make sure you have valid layout WRAP_CONTENT)); ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this, android. setDropDownViewResource(R.layout.spinner_dropdown_item); private void createDynamicSpinner() { //Remove this line from top in your code and add  Spinner text size are called as spinner inside items font size which is easily changeable if we are setting up textview items all we have do is defining text size in textview layout xml. So here is the complete step by step tutorial for How to change spinner text size in android programmatically.

In Android, Spinner is used to select one value from a set of values. In the default state, a spinner shows its currently selected value. Touching the spinner  A dropdown menu is a toggleable menu that allows the user to choose one value from a predefined list: The .dropdown class indicates a dropdown menu. To open the dropdown menu, use a button or a link with a class of .dropdown-toggle and the data-toggle="dropdown" attribute. The .caret class creates a caret arrow icon ( ), which indicates that

Comments
  • it depends on layout you are setting in your spinner adapter. can you post java code where you are setting adapter to spinner?
  • sure I will edit my post.
  • make it wrap content : android:dropDownWidth="match_parent"
  • @DiwakarSingh it is not about the width of the dropdown.. it is about the width of the label where which you can click in the dropdown menu.
  • This is not working aswell. As I said before It is not about getting the spinner to work. It is about the ability to click on the labels which at the moment (see picture) is just the small width of the words and not the whole width.
  • This is not working aswell. As I said before It is not about getting the spinner to work. It is about the ability to click on the labels which at the moment (see picture) is just the small width of the words and not the whole width. Thanks for your help though.
  • Its not about making the dropdownWidth longer. Its about the items be clickable at any point of the dropdown, not just the text. Sorry this does not help me.
  • @Christian.gruener could you please just try changing "ArrayAdapter < CharSequence > adapter" to "ArrayAdapter < String > adapter" I guess the array file you have created those could be string array may be something like this "<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="Course"> <item>Under Graduate</item> <item>Post Graduate</item> </string-array> </resources>" could you please check this.. just a sec I will share my answer
  • And using ArrayAdapter<String> you don't need to convert array item to string after it is selected as you are using '' spinnerSpells.getSelectedItem().toString() "
  • Chainging it to <String> did give me an error since It expects to get a CharSequence. Honestly, I am really confused by that and did not figure out what the problem is or why the arraystring has to be CharSequence.
  • Is it possible to reach you through mail.. So that i can share you some similar code which i have used in most applications i have done. And you might have got any errors in logs please if its available please share those also.
  • Sadly this did not help. " new ArrayAdapter<String>(spellActivity.this, R.array.dropdownCategory, android.R.layout.simple_spinner_item);" The fat one just give me errors since Java "expect resources of type layout.."