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 dont think you can create this dynamically without overriding the behavior of the default layout resources . To create it using resources:
Create a layout file containing a TextView and define the size,colors and other style for this. And create an ArrayAdapter object and provide that layout file in your adapter alongwith the ID of the TextView.
Your layout file will be like this: spinner_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textview"
android:layout_height="30dip"
android:layout_width="wrap_content"
android:textSize="20dip"
android:textColor="#ccddaa"
/>
</LinearLayout>
Now you can use this in your code like this:
Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_item,R.id.textview,Your_Array_of_items);
mySpinner.setAdapter(adapter);
Also you can create a custom ArrayAdapter and overriding the methods
getView() or getDropDownView()
inside this methods you can set custom color, size and fonts for your TextView
Update:
I have changed the text size and color of the spinner items dynamically by overrding the default resources of android. The snippet I have used is as given below:
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);
spinner.setAdapter(adapter);
}
private class SpinnerAdapter extends ArrayAdapter<String> {
Context context;
String[] items = new String[] {};
public SpinnerAdapter(final Context context,
final int textViewResourceId, final String[] objects) {
super(context, textViewResourceId, objects);
this.items = objects;
this.context = context;
}
@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(30);
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(30);
return convertView;
}
}
ArrayAdapter<String> ArrayAdapter_Price = new ArrayAdapter<String>(
getActivity(), R.layout.item_list_drop_down, dollers) {
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
TextView tv = ((TextView) v);
tv.setTextColor(getResources().getColor(R.color.login_text_color));
tv.setTypeface(tv.getTypeface(), Typeface.BOLD);
tv.setSingleLine();
tv.setEllipsize(TextUtils.TruncateAt.END);
tv.setTextSize(18);
return v;
}
};
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