In my Android application I create the layout programatically (ie an xml layout file not used for creating the component layout), in this case how can I create a spinner component programatically? Especially the setAdapter portion of spinner component, Is it possible create an ArrayAdapter with out using an XML layout reference like ‘android.R.layout.spinner_item’?
Spinner sp_gender = new Spinner(this);
ArrayList<String> spinnerArray = new ArrayList<String>();
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
android.R.layout.spinner_item, new String[] {
"Male",
"Female"
});
sp_gender.setAdapter(spinnerArrayAdapter);
ll_main.addView(sp_gender);
In the above code I want to remove the xml file reference portion ‘android.R.layout.spinner_item’ because I create the layout completely in the activity class.
No its not possible. Because ArrayAdapter has no default constructor. also u need to specify the structure of the view and also on which it should draw the view thats why arrayadapter takes layout id and context as one of its parameter
ArrayAdapter(Context context, int textViewResourceId)is the constructor of ArrayAdapter with least parameter.