Question

I am creating a spinner programmatically:

 ArrayAdapter<String> myArrayAdapter = new ArrayAdapter<String>(MyActivity.this, android.R.layout.simple_spinner_dropdown_item, myList);
                myArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

                Spinner spinner = new Spinner(MyActivity.this);
                spinner.setAdapter(myArrayAdapter);
                spinner.setLayoutParams(new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT));

                buttonLayout.addView(spinner);

Can anybody help?

enter image description here

UPDATE:

The following is the code of the static elements:

<TextView
    android:id="@+id/txt_user"
    android:text="@string/txt_user_lbl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/txt_2"
    android:text="@string/txt_disease_lbl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/txt_user"/>

<Spinner
    android:id="@+id/dd_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:drawable/btn_dropdown"
    android:spinnerMode="dropdown"
    android:layout_below="@+id/txt_2"/>

<TextView
    android:id="@+id/txt_1"
    android:text="@string/txt_medicine_lbl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/dd_2"/>

<Spinner
    android:id="@+id/dd_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:drawable/btn_dropdown"
    android:spinnerMode="dropdown"
    android:layout_below="@+id/txt_1"/>

<ScrollView
    android:id="@+id/scrollBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/dd_1">
<LinearLayout
    android:id="@+id/buttonLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="3dp"
    android:layout_marginRight="3dp"
    android:orientation="vertical">


<Button
    android:id="@+id/add_Button"
    android:text="@string/add_Button_lbl"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    />

<Button
    android:id="@+id/ok_Button"
    android:text="@string/ok_Button_lbl"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/add_Button"/>

<Button
    android:id="@+id/exit_Button"
    android:text="@string/exit_Button_lbl"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/ok_Button"/>
</LinearLayout>
</ScrollView>

Was it helpful?

Solution

Can anybody help?

Use android.R.layout.simple_spinner_item in the ArrayAdapter constructor, not android.R.layout.simple_spinner_dropdown_item.

Your programmatically added Spinner is using a holographic widget theme, like Theme.Holo. Your "static spinner" -- however you got it -- is using a legacy theme, like Theme. If you want them to look the same, they need to use the same theme. Normally, this happens automatically.

If you got @style/AppTheme from Eclipse or Android Studio relatively recently (within the past several months), and did not change anything in the generated styles.xml files, then this should give you a theme based on Theme.Holo when running on an API Level 11+ device or emulator.

You need to Get rid of android:background and android:spinnerMode from your two static elements.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top