Skip to content Skip to sidebar Skip to footer

Give Spinner Items The Width Of Its Dropdown

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 ide

Solution 1:

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 classxCustomSpinnerAdapaterextendsArrayAdapter<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 ViewgetDropDownView(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 ViewgetView(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;
    }
}

Added a xml file accordingly:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="40dp"android:orientation="horizontal"
    ><ImageViewandroid:id="@+id/ivSpinnerImage"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="3"android:padding="5dp"android:src="@drawable/artifact"
        /><TextViewandroid:id="@+id/tvSpinnerText"android:layout_width="0dp"android:layout_height="match_parent"android:layout_weight="7"android:gravity="center"android:text="Accessory"android:textSize="18sp"

        /></LinearLayout>

And after that I edited my mainActivity as follows:

[..]

  private xCustomSpinnerAdapater spinnerAdapter;
    private String[] spinnerNames = {"All", "Black", "Blue", "Green", "Red"};
    private  int[] spinnerImages = {R.drawable.artifact, R.drawable.black, R.drawable.blue, R.drawable.green, R.drawable.red};


[..]

protected voidonCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spell);
        
        spinnerSpells = findViewById(R.id.spellSpinner);
        spinnerAdapter = newxCustomSpinnerAdapater(this, spinnerNames,spinnerImages);
        spinnerSpells.setAdapter(spinnerAdapter);


        spinnerSpells.setOnItemSelectedListener(newAdapterView.OnItemSelectedListener() {
            @Override
            public voidonItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(getApplicationContext(),spinnerNames[i],Toast.LENGTH_LONG).show();
               [...]
            }

            @Override
            public voidonNothingSelected(AdapterView<?> adapterView) {
                [...]
        });

The useage of layout_width = "match_parent" in the xml file within in the Linearlayout solved my problem pretty much.

With best regards CG

Solution 2:

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"?><TextViewxmlns: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 = newArrayAdapter<String>(this, 
R.layout.spinner_item,list);

Solution 3:

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);

Solution 4:

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)

Solution 5:

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

    <?xml version="1.0" encoding="utf-8"?><resources><string-arrayname="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

    SpinnerspinnerSpells= findViewById(R.id.spellSpinner);
    ArrayAdapter<String> adapter = newArrayAdapter<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(newAdapterView.OnItemSelectedListener() {
    @OverridepublicvoidonItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
     // do something [...]
     }
    
    @OverridepublicvoidonNothingSelected(AdapterView<?> adapterView) {
       // do something [...]
    });
    

I wish this will help you out

Post a Comment for "Give Spinner Items The Width Of Its Dropdown"