Key
Key

Reputation: 65

Code for android spinner

I'm having some problem to change this XML file into android spinner.I'm still new in android development and i hope someone will help me.

<channel id="1">
<title>M</title>
<description>my new TV.  Viewer: 15+ </description>
<icon>image</icon>
<link>link</link>

Upvotes: 0

Views: 2928

Answers (4)

Hari Babu Mandala
Hari Babu Mandala

Reputation: 295

You can add a spinner to your layout with the Spinner object. You should usually do so in your XML layout with a element. For example:

<Spinner
android:id="@+id/planets_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />  

If the required choices for your spinner are pre-determined, you can provide them with a string array defined in a string.xml resource under values folder file as follows,

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
</resources>

Now you can assign that array list to the spinner object by using adapter as follows,

Spinner spinner = (Spinner) findViewById(R.id.spinner);
   // Create an ArrayAdapter using the string array and a default spinner layout
   ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                            R.array.planets_array, android.R.layout.simple_spinner_item);
   // Specify the layout to use when the list of choices appears
   adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
   // Apply the adapter to the spinner
   spinner.setAdapter(adapter);

Upvotes: 1

jitain sharma
jitain sharma

Reputation: 604

First create the string array in .xml file under values folder like:

 <!-- Spinner Adapter Array -->
    <string-array name="Month">
        <item>JAN</item>
        <item>FEB</item>
        <item>MAR</item>
        <item>APR</item>
        <item>MAY</item>
        <item>JUN</item>
        <item>JUL</item>
        <item>AUG</item>
        <item>SEP</item>
        <item>OCT</item>
        <item>NOV</item>
        <item>DEC</item>
    </string-array>
    <!-- END -->

Now create a spinner view in your layout file Like:

<Spinner
                android:id="@+id/months"
                android:layout_width="300sp"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="35dp"
                android:gravity="center"
                android:paddingLeft="10dp"
                android:prompt="@string/template_prompt" />

now in your activity class create a adapter Like:

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(activity, R.array.Month, R.layout.spinner_row);

Than after bind this adapter to your spinner object Like:

yourSpinner.setAdapter(adapter);

ohh also you can set the dropdown view resources to your adapter as well like:

adapter.setDropDownViewResource(R.layout.spinner_textview);

before bind it to the spinner. Hope it will helps you.

Upvotes: 0

Shailendra Madda
Shailendra Madda

Reputation: 21531

Open “res/values/strings.xml” file, define the list of items that will display in Spinner (dropdown list).

File : res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">MyAndroidApp</string>
    <string name="country_prompt">Choose a country</string>

    <string-array name="country_arrays">
        <item>Malaysia</item>
        <item>United States</item>
        <item>Indonesia</item>
        <item>France</item>
        <item>Italy</item>
        <item>Singapore</item>
        <item>New Zealand</item>
        <item>India</item>
    </string-array>

</resources>
  1. Spinner (DropDown List)

Open “res/layout/main.xml” file, add two spinner components and a button.

In “spinner1″, the “android:entries” represents the selection items in spinner.
In “spinner2″, the selection items will be defined in code later.

File : res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/country_arrays"
        android:prompt="@string/country_prompt" />

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

    <Button
        android:id="@+id/btnSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit" />

</LinearLayout>

3.File : MyAndroidAppActivity.java

package com.mkyong.android;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class MyAndroidAppActivity extends Activity {

  private Spinner spinner1, spinner2;
  private Button btnSubmit;

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

    addItemsOnSpinner2();
    addListenerOnButton();
    addListenerOnSpinnerItemSelection();
  }

  // add items into spinner dynamically
  public void addItemsOnSpinner2() {

    spinner2 = (Spinner) findViewById(R.id.spinner2);
    List<String> list = new ArrayList<String>();
    list.add("list 1");
    list.add("list 2");
    list.add("list 3");
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_spinner_item, list);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner2.setAdapter(dataAdapter);
  }

  public void addListenerOnSpinnerItemSelection() {
    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
  }

  // get the selected dropdown list value
  public void addListenerOnButton() {

    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner2 = (Spinner) findViewById(R.id.spinner2);
    btnSubmit = (Button) findViewById(R.id.btnSubmit);

    btnSubmit.setOnClickListener(new OnClickListener() {

      @Override
      public void onClick(View v) {

        Toast.makeText(MyAndroidAppActivity.this,
        "OnClickListener : " + 
                "\nSpinner 1 : "+ String.valueOf(spinner1.getSelectedItem()) + 
                "\nSpinner 2 : "+ String.valueOf(spinner2.getSelectedItem()),
            Toast.LENGTH_SHORT).show();
      }

    });
  }
}

File : CustomOnItemSelectedListener.java

package com.mkyong.android;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;

public class CustomOnItemSelectedListener implements OnItemSelectedListener {

  public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
    Toast.makeText(parent.getContext(), 
        "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
        Toast.LENGTH_SHORT).show();
  }

  @Override
  public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub
  }

}

Upvotes: 0

Naveen
Naveen

Reputation: 1958

You need to do like this

In your strings.xml define:

<string-array name="array_name">
<item>Array Item One</item>
<item>Array Item two</item>
<item>Array Item Three</item>
</string-array>

In your layout:

<Spinner 
        android:id="@+id/spinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:drawSelectorOnTop="true"
        android:entries="@array/array_name"
    />

Upvotes: 1

Related Questions