I use the following function to resize the text on a spinner when an item is selected:
mySpinner.post(new Runnable()
{
public void run()
{
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
TextView tempView = ((TextView) parent.getChildAt(0));
((TextView) parent.getChildAt(0)).setTextSize(TypedValue.COMPLEX_UNIT_PX, SetTextSize(tempView.getText().toString(), tempView.getWidth() - tempView.getPaddingLeft() - tempView.getPaddingRight(), (int) fieldHeight - tempView.getPaddingTop() - tempView.getPaddingBottom()));
}
@Override
public void onNothingSelected(AdapterView<?> parent)
{
// Do nothing
}
});
}
});
The problem is, before it resizes the text to the correct size, the text is drawn in the incorrect size. What I need to do is call my function SetTextSize as soon as the item is selected. I cannot find anyway to do this. setOnItemSelectedListener won't do because text is displayed in the incorrect size before the event runs.
I have changed:
mySpinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, CHOICES));
To:
mySpinner.setAdapter(new MyArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item, CHOICES));
and added the following class:
public class MyArrayAdapter extends ArrayAdapter
{
public MyArrayAdapter(Context context, int textViewResourceId, Object[] objects)
{
super(context, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
((TextView) convertView).setTextSize(TypedValue.COMPLEX_UNIT_PX, SetTextSize(((TextView) convertView).getText().toString(), convertView.getWidth() - convertView.getPaddingLeft() - convertView.getPaddingRight(), (int) fieldHeight - convertView.getPaddingTop() - convertView.getPaddingBottom()));
return convertView;
}
}
Unfortunately, now my program crashes immediately when it starts.
I have created the following class:
class MySpinnerAdapter extends ArrayAdapter<String>
{
MySpinnerAdapter()
{
super(MainActivity.this, R.layout.row, R.id.label, CHOICES);
}
public View getView(int position, View convertView, ViewGroup parent)
{
View row = super.getView(position, convertView, parent);
TextView label = (TextView)row.findViewById(R.id.label);
if (label != null)
label.setTextSize(TypedValue.COMPLEX_UNIT_PX, SetTextSize(label.getText().toString(), label.getWidth() - label.getPaddingLeft() - label.getPaddingRight(), (int) fieldHeight - label.getPaddingTop() - label.getPaddingBottom()));
return(row);
}
}
I now have 2 problems. Firstly, this line causes my program to crash:
label.setTextSize(TypedValue.COMPLEX_UNIT_PX, SetTextSize(label.getText().toString(), label.getWidth() - label.getPaddingLeft() - label.getPaddingRight(), (int) fieldHeight - label.getPaddingTop() - label.getPaddingBottom()));
Secondly, the styles that are in "row.xml" are applied to my spinner. If I don't specify any styles, then my program crashes. I simply do not want the xml file to alter the way my spinner looks because I do not know at compile time. I would like the spinner to use the default values that it was using before I created the XML file, and anything else I can then change in code.