How can I programmatically change the Padding
and TextSize
of the items in the spinner's dropdown box?
I tried using XML but the padding and text size appears different on different screen sizes. I need to perform a couple of simple calculations based on the dimensions of the screen. I tried inflating the view, but I found no way to convert the the view back to XML or apply it to my adapter.
Here is my code for the spinner:
View view = getLayoutInflater().inflate(R.layout.list_items, null);
view.setPadding((int) (screenWidth * 0.1), (int) (screenHeight * 0.2), (int) (screenWidth * 0.1), (int) (screenHeight * 0.2));
if (portrait)
spnVerbs.setAdapter(new MySpinnerAdapter(R.layout.list_items, CHOICES));
else
spnVerbs.setAdapter(new MySpinnerAdapter(R.layout.list_items1, CHOICES));
And MySpinnerAdapter.class
class MySpinnerAdapter extends ArrayAdapter<String>
{
MySpinnerAdapter(int dropDownResource, String[] choices)
{
super(Start.this, dropDownResource, choices);
}
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
if (row == null)
row = super.getView(position, convertView, parent);
if (parent.getWidth() > 0)
{
TextView label = (TextView) row.findViewById(android.R.id.text1);
label.setIncludeFontPadding(false);
label.setTextSize(TypedValue.COMPLEX_UNIT_PX, SetTextSize(label.getText().toString(), parent.getWidth() - paddingLeft * 2 - paddingLeftExtra * 2, (int) fieldHeight - paddingTop * 2 - paddingTopExtra * 2));
label.setPadding(paddingLeft + paddingLeftExtra, 0, 0, 0);
}
return(row);
}
}