Sunday, 9 October 2016

SpinnerView

It is a dropdown list.
Example Bookmyshow app or just tickets.
Here we are Using ArrayAdapter class in the code below
Similarly AutoCompleteTextView we can use as dropdown list.


activitymain.xml :-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context="com.kiranapp.MainActivity">
<Spinner
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:id="@+id/spinner"
  android:layout_marginTop="50dp"/>
</LinearLayout>

Germany.xml :-

<?xml version="1.0" encoding="utf-8"?>
 <TextView xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:textAppearance="?android:attr/textAppearanceLarge"
   android:id="@+id/textView" />

MainActivity.java :-

package com.kiranapp;

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

public class MainActivity extends AppCompatActivity {
   Spinner spinner;
   String[] countrys={"-select countrys-","INDIA","GERMANY","FRANCE","USA","UK","AUSTRALIA",
            "CANADA","CHINA","SWITERLAND","ITALY"};


@Override
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   spinner=(Spinner)findViewById(R.id.spinner);
   ArrayAdapter<String> aa=new ArrayAdapter<String>(MainActivity.this,
    android.R.layout.simple_list_item_1,countrys);
    spinner.setAdapter(aa);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
 @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    Toast.makeText(MainActivity.this, "COUNTRY ID : "+countrys[position],     
    Toast.LENGTH_SHORT).show();
    if(position==2){
     Intent in=new Intent(MainActivity.this,Germany.class);
     startActivity(in);
     }
    }
@Override
   public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }
}

Germany.java :-

package com.kiranapp;

/**
 * Created by Dell on 10/8/2016.
 */
public class Germany {
}

Output :-

SpinnerView
SpinnerView Fig:1




















SpinnerView
SpinnerView Fig:2

No comments:

Post a Comment