1. Download our Official Android App: Forums for Android!

Apps Create combobox without XML

Discussion in 'Android Development' started by IORI999, Dec 10, 2009.

  1. IORI999

    IORI999 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    1
    Joined:
    Dec 10, 2009

    Dec 10, 2009
    1
    0
    5
    How can I create a combobox programmatically without using XML?
     

    Advertisement

  2. trackaroo

    trackaroo Lurker
    Rank:
    None
    Points:
    5
    Posts:
    8
    Joined:
    Dec 12, 2009

    Dec 12, 2009
    8
    0
    5
    Check out the Spinner class.

    Code (Text):
    1.  
    2. //spinner view
    3. RelativeLayout.LayoutParams spinnerParams =
    4.     new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    5. spinnerParams.setMargins(1, 1, 1, 1);
    6. spinnerParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    7.  
    8. spinnerValueView = new Spinner(context);
    9. ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, optionsStr);          
    10. adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);                    
    11. spinnerValueView.setAdapter(adapter);  
    12. spinnerValueView.setSelection(selected, true);
    13. spinnerValueView.setOnItemSelectedListener(this);
    14. nameValueLayout.addView(spinnerValueView, spinnerParams);
    Jeff
     

Share This Page

Loading...