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.
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.
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<?>
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With