564

How to re size the Height of the Items in Spinner with multiple choice?

Question:

I just showing the spinner with multiple choice based on this <a href="https://stackoverflow.com/questions/5015686/android-spinner-with-multiple-choice" rel="nofollow">stackoverflow answer(see @Destil answer)</a>. Here my problem is I can't re size the Height of the items in Spinner with multiple choice. How to re size the Height of each Items?

Answer1:

As i know, you will have to use a custom adapter, and override the getDropDown and the getView methods. You will be able to customize each item customizing the layout.

Well... words are good, example better, try something like that :

public class CustomStringArrayAdapter extends ArrayAdapter<String> { private Activity myActivity; public CustomStringArrayAdapter(Context context, int layoutResourceId, int textViewResourceId, List<String> objects, Activity act) { super(context, layoutResourceId, textViewResourceId, objects); this.myActivity = act; } @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { return getCustomView(position, convertView, parent); } public View getCustomView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = myActivity.getLayoutInflater(); View row = inflater.inflate(R.layout.spinner_layout, parent, false); TextView label = (TextView) row.findViewById(R.id.spinner_textview); label.setText(getItem(position)); LinearLayout layout = (LinearLayout) row.findViewById(R.id.spinner_linear_layout); return row; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = myActivity.getLayoutInflater(); View row = inflater.inflate(R.layout.spinner_layout, parent, false); TextView label = (TextView) row.findViewById(R.id.spinner_textview); label.setText(getItem(position)); return row; } }

And the layout :

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="50dp" android:layout_gravity="center" android:gravity="center" android:background="@drawable/transparent" android:id="@+id/spinner_linear_layout"> <TextView android:layout_height="fill_parent" android:layout_width="fill_parent" android:gravity="center" android:layout_gravity="center" android:textSize="21sp" android:singleLine="true" android:id="@+id/spinner_textview" android:textColor="@color/black" /> </LinearLayout>

(But it's just an sample, if you want to resize depending on the selection, you can add a boolean mSelected on each object and change the view depending on this boolean)

Edit : Then in the MultiSpinner :

Remove this :

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, new String[] { spinnerText }); setAdapter(adapter);

and add the new custom adapter in the spinner :

MultiSpinner mMultiSpinner = (MultiSpinner) findViewById(R.id.multispinner); mMultiSpinner.setAdapter(mCustomArrayAdapter);

Recommend

  • Custom ArrayAdapter onClick position & row highlight
  • EditText Content Changes on ListView Scroll
  • How to properly add data to ArrayList - Android
  • OnScrollListener not working inside custom ListView Adapter
  • Ruby on Rails App deployed to heroku showing “We're sorry, but something went wrong”
  • Wrong row deleted from custom listview with spinner
  • Spring: No transaction manager has been configured
  • JPA flush vs commit
  • Elasticsearch script query involving root and nested values
  • Why use database factory in asp.net mvc?
  • How do I configure context broker accept post requests from my remote sensor?
  • Debug.DrawLine not showing in the GameView
  • CSS Linear-gradient formatting issue accross different browsers
  • Javascript Callbacks with Object constructor
  • How to show dropdown in excel using jrxml (jasper api)?
  • Importing jscolor library in angular 2
  • Release, debug version and Authorization Google?
  • Websockets service method fails during R startup
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • How to get next/previous record number?
  • Apache 2.4 - remove | delete | uninstall
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper way to use connect-multiparty with express.js?
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Trying to get generic when generic is not available
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • embed rChart in Markdown
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • apache spark aggregate function using min value
  • Add sale price programmatically to product variations
  • Sorting a 2D array using the second column C++
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Python/Django TangoWithDjango Models and Databases
  • java string with new operator and a literal
  • How do I use LINQ to get all the Items that have a particular SubItem?