I am accessing a spinner in java file, setting its input which is string array having US states name. I want to give my own size to selected state in spinner? How to give it custom size?
This is the Code:
Spinner state = (Spinner) findViewById(R.id.state);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.state, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
state.setAdapter(adapter);
Xml:
<Spinner
android:id="@+id/state"
android:layout_width="wrap_content"
android:layout_height="60dip"
android:layout_marginLeft="10dip"
android:addStatesFromChildren="true" >
</Spinner>
String Array to Display in Spinner:
<string-array name="state">
<item>Select State</item>
<item>Alabama</item>
<item>Alaska</item>
<item>American Samoa</item>
<item>Arizona</item>
<item>Arkansas</item>
<item>California</item>
<item>Colorado</item>
<item>Connecticut</item>
<item>Delaware</item>
<item>District of Columbia</item>
<item>Florida</item>
<item>Georgia</item>
<item>Guam</item>
<item>Hawaii</item>
<item>Idaho</item>
<item>Illinois</item>
<item>Indiana</item>
<item>Iowa</item>
<item>Kansas</item>
<item>Kentucky</item>
<item>Louisiana</item>
<item>Maine</item>
<item>Maryland</item>
<item>Massachusetts</item>
<item>Michigan</item>
<item>Minnesota</item>
<item>Mississippi</item>
<item>Missouri</item>
<item>Montana</item>
<item>Nebraska</item>
<item>Nevada</item>
<item>New Hampshire</item>
<item>New Jersey</item>
<item>New Mexico</item>
<item>New York</item>
<item>North Carolina</item>
<item>North Dakota</item>
<item>Northern Marianas Islands</item>
<item>Ohio</item>
<item>Oklahoma</item>
<item>Oregon</item>
<item>Pennsylvania</item>
<item>Puerto Rico</item>
<item>Rhode Island</item>
<item>South Carolina</item>
<item>South Dakota</item>
<item>Tennessee</item>
<item>Texas</item>
<item>Utah</item>
<item>Vermont</item>
<item>Virginia</item>
<item>Virgin Islands</item>
<item>Washington</item>
<item>West Virginia</item>
<item>Wisconsin</item>
<item>Wyoming</item>
</string-array>
you can use
adapter.setDropDownViewResource(android.R.layout.simple_list_item_1);
or you can give it your layout:
adapter.setDropDownViewResource(R.layout.my_layout);
and your my_layout.xml should look like this:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+android:id/text1" android:layout_marginTop="5dp"
android:layout_marginBottom="5dp" android:padding="3dp"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:gravity="center_horizontal"/>
Try to use a customized adapter like this,
ArrayAdapter<String> spinner_adapter = new ArrayAdapter<String>(getParent()
.getApplicationContext(), R.layout.my_spinner_style,spinner_values) {
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTextSize(18);
return v;
}
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
View v = super.getDropDownView(position, convertView, parent);
v.setBackgroundResource(R.drawable.spinner_drop_down);
((TextView) v).setTextColor(Color
.parseColor(config.sort_by_label_color));
return v;
}
};
R.layout.my_spinner_style
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+android:id/text1"
style="?android:attr/spinnerItemStyle"
android:paddingLeft="5dip"
android:singleLine="true"
android:textColor="#ffffff"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee" />
And set this adapter to your spinner,
spinner.setAdapter(spinner_adapter);
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