Friday 12 June 2015

Spinner example using strings file in android

No comments
Hello friends,in this android tutorial we learn how to implement Spinner using strings.xml. In this example i write a very simple code to implement spinner.spinner is a nothing but  a like a drop down menu.
Let me explore example To create spinner using strings.xml

CREATE NEW ANDROID PROJECT:

We need for this example:

1) strings.xml
2) activity_main.xml
3) MainActivity.java

Step 1: Write code into strings.xml

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

    <string name="app_name">SpinnerUsingString</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="countryselect">Select a Country</string>

    <string-array name="country_arrays">
        <item>India</item>
        <item>United States</item>
        <item>United Kingdom</item>
        <item>France</item>
        <item>Italy</item>
        <item>Singapore</item>
        <item>japan</item>
        <item>China</item>
        <item>Mexico</item>
        <item>Russia</item>
        <item>Spain</item>
        <item>Cuba</item>
    </string-array>

</resources>

Step 2: Write code into activity_main.xml

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Androidapplink"
        android:textSize="20sp" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Select Country"
        android:textSize="20sp" />

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


</LinearLayout>

Step 3: Write code into MainnActivity.java

package dev.androidapplink.spinnerusingstring;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity {
// create variable
Spinner spCountries;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize Spinners
spCountries = (Spinner) findViewById(R.id.spCountries);
// Country Item Selected Listener
spCountries.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView<?> adapter, View v,
int position, long id) {
// On selecting a spinner item
String item = adapter.getItemAtPosition(position).toString();

// Showing selected spinner item
Toast.makeText(getApplicationContext(),
"Selected Country is : " + item, Toast.LENGTH_LONG).show();
}

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

}
});
}


}
Step 3: Now Run Your Project:





No comments :

Post a Comment

Follow me Share