Hi I want to set the style and size for a text in spinner programatically(Dynamically).I don\'t use any resources in my app for this.So give me some suggestions for this
I have made 2 modifications,
1-added another constructor so that one can use a resource 2-added 2 public function (setTextSize, and getTextSize) (3-Also disabled the color by putting them as comments)
public class CustomSpinner extends Activity {
String[] numbers = { "One", "Two", "Three", "Four", "Five" };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
SpinnerAdapter adapter = new SpinnerAdapter(this,
android.R.layout.simple_spinner_item, numbers);
//(you can also pass in a string array resource like this:)
/*SpinnerAdapter adapter = new SpinnerAdapter(this, R.layout.simple_spinner_item,
getResources().getStringArray(R.array.my_string_array_resource));*/
spinner.setAdapter(adapter);
}
public class SpinnerAdapter extends ArrayAdapter {
Context context;
String[] items = new String[] {};
private int textSize=40; //initial default textsize (might be a bit big)
public SpinnerAdapter(final Context context, final int textViewResourceId, final String[] objects) {
super(context, textViewResourceId, objects);
this.items = objects;
this.context = context;
}
public SpinnerAdapter(final Context context, final int resource, final int textViewResourceId ){
super(context, resource, textViewResourceId);
this.items = context.getResources().getStringArray(resource);
Toast.makeText(context, String.valueOf(this.getSpinnerTextSize()), Toast.LENGTH_LONG).show();
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(
android.R.layout.simple_spinner_item, parent, false);
}
TextView tv = (TextView) convertView
.findViewById(android.R.id.text1);
tv.setText(items[position]);
//tv.setTextColor(Color.BLUE);
tv.setTextSize(textSize);
return convertView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(
android.R.layout.simple_spinner_item, parent, false);
}
// android.R.id.text1 is default text view in resource of the android.
// android.R.layout.simple_spinner_item is default layout in resources of android.
TextView tv = (TextView) convertView
.findViewById(android.R.id.text1);
tv.setText(items[position]);
//tv.setTextColor(Color.BLUE);
tv.setTextSize(textSize);
return convertView;
}
//set the textsize
public void setSpinnerTextSize(int size){
textSize= size;
}
//return the textsize
public int getSpinnerTextSize(){
return textSize;
}
}
}