Create a spinner programmatically in Android
Asked Answered
P

2

22

I want to create a spinner without using XML. I am new in android and my knowledge is limited. By now I have this code (see above) and I want my spinner in one of the tabs of my TabActivity. There is no obvious error but when I open my activity the tab is empty. I would appreciate some help.

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    ArrayList<String> spinnerArray = new ArrayList<String>();
    spinnerArray.add("one");
    spinnerArray.add("two");
    spinnerArray.add("three");
    spinnerArray.add("four");
    spinnerArray.add("five");

    Spinner spinner = new Spinner(this);
    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
    spinner.setAdapter(spinnerArrayAdapter);

}
Pole answered 20/4, 2013 at 11:33 Comment(1)
What does it mean you want a "spinner in on of the tabs"? Can you show an example of what you are trying to get?Gingergingerbread
S
37

You need to add the Spinner to a layout.

First create a container for the Spinner and then create the Spinner and add it to your container. Next set content of you Activity to your container.

This could be done like this, in your onCreate method:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    LinearLayout layout = new LinearLayout(this);

    ArrayList<String> spinnerArray = new ArrayList<String>();
    spinnerArray.add("one");
    spinnerArray.add("two");
    spinnerArray.add("three");
    spinnerArray.add("four");
    spinnerArray.add("five");

    Spinner spinner = new Spinner(this);
    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
    spinner.setAdapter(spinnerArrayAdapter);

    layout.addView(spinner);

    setContentView(layout);
}

EDIT:

Just to clarify: if the Spinner isn't added to the content of the Activity inside a layout, it isn't visible, so that's why you don't get any errors or anything, because there isn't any errors in your code, per se ;-)

Separation answered 20/4, 2013 at 11:42 Comment(2)
i use a TabActivity and my activity layout has a TabHost. I want to use the spinner only in one tab and thats why i didnt put a spinner in the xml. How is it possible to create another layout(activity_my) to put the spinner and use it only in this tab??Pole
To add the spinner to a specific fragment you would need to add the spinner to the specific fragments layout instead of the activity's layout. If this doesn't make sense open a new question for that.Separation
V
0
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout layout = new LinearLayout(this);

        // The following can also be done using a loop
        ArrayList<String> spinnerArray = new ArrayList<String>();
        spinnerArray.add("one");
        spinnerArray.add("two");
        spinnerArray.add("three");
        spinnerArray.add("four");
        spinnerArray.add("five");


        Spinner spinner = new Spinner(MainActivity.this);
        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
        spinner.setAdapter(spinnerArrayAdapter);
        layout.addView(spinner);
        setContentView(layout);
    }
    }
Valuation answered 10/7, 2019 at 16:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.