• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Create combobox without XML

Check out the Spinner class.

Code:
//spinner view
RelativeLayout.LayoutParams spinnerParams = 
    new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
spinnerParams.setMargins(1, 1, 1, 1);
spinnerParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

spinnerValueView = new Spinner(context);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, optionsStr);          
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);                     
spinnerValueView.setAdapter(adapter);  
spinnerValueView.setSelection(selected, true);
spinnerValueView.setOnItemSelectedListener(this);
nameValueLayout.addView(spinnerValueView, spinnerParams);

Jeff
 
Back
Top Bottom