Pages

Sunday, 16 April 2017

Spinner & Toast Usage in Android

FileName: MainActivity.java

package com.example.abbu.spinner_select;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

        Spinner s = (Spinner) findViewById(R.id.spinner1);

        s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            {
                Toast.makeText(MainActivity.this, parent.getSelectedItem().toString(),
                        Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }
}

FileName: activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.abbu.spinner_select.MainActivity">

    <TextView
        android:text="Select one Country : "
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        />

    <Spinner
        android:id="@+id/spinner1"
        android:entries="@array/country_names"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp" />
</RelativeLayout>

FileName: strings.xml

<resources>
    <string name="app_name">Spinner_Select</string>

    <string-array name="country_names">
        <item>India</item>
        <item>Pakistan</item>
        <item>China</item>
        <item>Nepal</item>
        <item>Bangladesh</item>
        <item>Bhutan</item>
    </string-array>
</resources>

Output:

By default, India is the selected spinner item and toast named India will appear.
Spinner & Toast Usage in Android
Spinner & Toast Usage in Android 
Now, we'll select any one country:
Spinner & Toast Usage in Android
Spinner & Toast Usage in Android 
Finally, I selected Bhutan and a toast named Bhutan will appear.
Spinner & Toast Usage in Android
Spinner & Toast Usage in Android 

2 comments:

  1. It is very useful post. Thanks for sharing with us.

    Popup builder Plugin

    ReplyDelete
  2. Your blog on reading is so full of great insights. I love your idea of adapting trade books. We have some that have far too much text, that would be fabulous, modified for our seven-year-old reader.
    Top IT Service in India

    ReplyDelete