Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android spinner and array adapter programmatically

Tags:

android

I am trying to programmatically create a Spinner using an ArrayAdapter. This is being done in a closed source jar as part of an Android library project and I do not have any access to resources.

I want to know how to customize the layout and the text view that displays the spinner.

--EDIT--
Presently I am using the following code

ArrayAdapter<T> spinnerData = new ArrayAdapter<T>(this, Android.R.layout.simple_spinner_item, T[]);

spinner.setAdapter(spinnerData);

What is happening is that I cannot control the size of the fonts, colours or anything else. When I click on the spinner to select something all the options are in really small text. I want to be able to change the way it renders on screen.

like image 887
kapad Avatar asked Oct 26 '12 12:10

kapad


People also ask

What is spinner in Android with example?

Android Spinner is a view similar to the dropdown list which is used to select one option from the list of options. It provides an easy way to select one item from the list of items and it shows a dropdown list of all values when we click on it.

How can I use more than one spinner in Android?

Clearly each of your adapters need to adapt a different set of String s, so you need to create an ArrayAdapter for each Spinner . A single OnItemSelectedListener will work for the 3 Spinners (as long as you set them). You can call getId() on the AdapterView<?>

How do you populate a spinner?

In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one. To populate the spinner with a list of choices, you then need to specify a SpinnerAdapter in your Activity or Fragment source code.


1 Answers

Just create a custom ArrayAdapter and define how the spinner textview should look in that getDropDownView method of the adapter

public class CustomizedSpinnerAdapter extends ArrayAdapter<String> {

 private Activity context;
 String[] data = null;

 public CustomizedSpinnerAdapter(Activity context, int resource, String[] data2)
 {
     super(context, resource, data2);
     this.context = context;
     this.data = data2;
 }
  ...

 @Override
 public View getDropDownView(int position, View convertView, ViewGroup parent)
 {   
     View row = convertView;
     if(row == null)
     {
         //inflate your customlayout for the textview
         LayoutInflater inflater = context.getLayoutInflater();
         row = inflater.inflate(R.layout.spinner_layout, parent, false);
     }
     //put the data in it
     String item = data[position];
     if(item != null)
     {   
        TextView text1 = (TextView) row.findViewById(R.id.rowText);
        text1.setTextColor(Color.WHITE);
        text1.setText(item);
     }

     return row;
 }
...
}

and set this adapter for the spinner

Spinner mySpinner = (Spinner) findViewById(R.id.mySpinner);
final String[] data = getResources().getStringArray(
            R.array.data);
final ArrayAdapter<String> adapter1 = new CustomizedSpinnerAdapter(
            AddSlaveActivity.this, android.R.layout.simple_spinner_item,
            data);
mySpinner.setAdapter(adapter1); 
like image 111
user936414 Avatar answered Nov 03 '22 11:11

user936414