Reputation: 8230
I created a spinner programmatically:
ArrayAdapter<ServiceObject> medicineArrayAdapter = new ArrayAdapter<ServiceObject>(MedicineActivity.this, android.R.layout.simple_spinner_dropdown_item, medicines);
medicineArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spinner = new Spinner(MedicineActivity.this, getSpinnerAttrs(), 0);
spinner.setAdapter(medicineArrayAdapter);
spinner.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
spinner.setVisibility(View.VISIBLE);
buttonLayout.addView(spinner, index);
Where getSpinnerAttrs():
AttributeSet as = null;
Resources r = getResources();
XmlResourceParser parser = r.getLayout(R.layout.fragment_medicine);
int state = 0;
do {
try {
state = parser.next();
} catch (XmlPullParserException xppe) {
xppe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
if (state == XmlPullParser.START_TAG) {
if (parser.getName().equals("Spinner")) {
as = Xml.asAttributeSet(parser);
break;
}
}
} while(state != XmlPullParser.END_DOCUMENT);
return as;
and Spinner in fragment_medicine is:
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/btn_dropdown"
android:spinnerMode="dropdown"
android:visibility="invisible"/>
/>
It shows normally But when I click on it, it doesn't open . . .
Upvotes: 2
Views: 408
Reputation: 8230
Well at the end I did it in another way.
I just added the attributes programmatically without reading them from an xml file as such:
spinner.setBackgroundResource(android.R.drawable.btn_dropdown);
And it worked perfectly.
Upvotes: 1