0

I had an android UI from sample which is shown below

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
     <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:weightSum="1" android:layout_height="wrap_content">
        <TextView android:text="data1:" 
            android:id="@+id/data1Label" 
            android:layout_weight="0.5"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content">
        </TextView>
        <TextView android:text="data2:" 
            android:id="@+id/data2Label" 
            android:layout_weight="0.5"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content">
        </TextView>     
    </LinearLayout>
    <ListView android:id="@+id/in"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll"
        android:layout_weight="1"
    />
</LinearLayout>

Then I needed to add a combobox after the top label panel and, I added a Spinner component like below

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
     <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:weightSum="1" android:layout_height="wrap_content">
        <TextView android:text="data1:" 
            android:id="@+id/data1Label" 
            android:layout_weight="0.5"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content">
        </TextView>
        <TextView android:text="data2:" 
            android:id="@+id/data2Label" 
            android:layout_weight="0.5"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content">
        </TextView>     
    </LinearLayout>
    <Spinner
    android:id="@+id/cmd_spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
    <ListView android:id="@+id/in"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll"
        android:layout_weight="1"
    />
</LinearLayout>

And In my Strings.xml resource file I added the data list for the spinner.

<string-array name="cmd_array">
        <item>cmd1</item>
        <item>cmd2</item>
        <item>cmd3</item>
    </string-array>

Then at the end of on create method of my activity class I added below method to set data to the spinner

Spinner spinner = (Spinner) findViewById(R.id.cmd_spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
             R.array.cmd_array, android.R.layout.simple_spinner_item);
 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
  spinner.setAdapter(adapter);

But when I run my app it does not show me the spinner. Any idea about the issue?

UPDATE

I am compiling my application as a android libaray project and the main activity is starting from Unity Game Engine.

Steven
  • 166,672
  • 24
  • 332
  • 435
Thudani Hettimulla
  • 754
  • 1
  • 12
  • 32

2 Answers2

0

in onCreate() method set your spinner data in this way:

    Spinner spinner = (Spinner) findViewById(R.id.cmd_spinner);
    String[] array = getResources().getStringArray(R.array.cmd_array);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_dropdown_item, array);
    spinner.setAdapter(adapter);
Saritha
  • 488
  • 3
  • 4
0

you should try this-----

 Spinner spinner = (Spinner) findViewById(R.id.cmd_spinner);
 String[] stringArray = getResources().getStringArray(R.array.cmd_array);
 ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_dropdown_item, stringArray);
 spinner.setAdapter(arrayAdapter);

and you should modify your xml like below , since this is much better way....

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:text="data1:"
        android:id="@+id/data1Label"
        android:layout_weight="0.5"
        android:layout_width="0dp"
        android:layout_height="wrap_content"/>

    <TextView
        android:text="data2:"
        android:id="@+id/data2Label"
        android:layout_weight="0.5"
        android:layout_width="0dp"
        android:layout_height="wrap_content"/>
</LinearLayout>

<Spinner
    android:id="@+id/cmd_spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<ListView
    android:id="@+id/in"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:stackFromBottom="true"
    android:transcriptMode="alwaysScroll"
    android:layout_weight="1" />

Deven Singh
  • 398
  • 1
  • 3
  • 16