How to make Spinner (Dropdown List) In android App

The output of the Android Spinner Example







What is Spinner in android?.

In Android, Spinner is a view that allows a user to select one value from the list of values. The spinner in android will behave the same as a dropdown list in other programming languages.

Create Android Spinner in XML Layout File

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
android:layout_width="match_parent" android:layout_height="match_parent">
    <
TextView
        
android:id="@+id/txtVw"
        
android:layout_width="wrap_content"
        
android:layout_height="wrap_content"
        
android:layout_marginLeft="50dp"
        
android:layout_marginTop="150dp"
        
android:text="Select User:"
        
android:textStyle="bold"
        
android:textSize="15dp" />
    <
Spinner
        
android:id="@+id/spinner1"
        
android:layout_width="wrap_content"
        
android:layout_height="wrap_content"
        
android:layout_alignBottom="@+id/txtVw"
        
android:layout_toRightOf="@+id/txtVw" />
</
RelativeLayout>
Once we are done with the creation of layout with required controls, we need to load the XML layout resource from our activity onCreate() callback method, for that open main activity file MainActivity.java from \java\your project path  path and write the code like as shown below.

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 implements AdapterView.OnItemSelectedListener {
String[] 
users = { "Suresh Dasari""Trishika Dasari""Rohini Alavala""Praveen Kumar""Madhav Sai" };
    
@Override
    
protected void onCreate(Bundle savedInstanceState) {
        
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
        Spinner spin = (Spinner) findViewById(R.id.
spinner1);
        ArrayAdapter<String> adapter = 
new ArrayAdapter<String>(this, android.R.layout.simple_spinner_itemusers);
        adapter.setDropDownViewResource(android.R.layout.
simple_spinner_dropdown_item);
        spin.setAdapter(adapter);
        spin.setOnItemSelectedListener(
this);
    }
    
@Override
    
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
        Toast.makeText(getApplicationContext(), 
"Selected User: "+users[position] ,Toast.LENGTH_SHORT).show();
    }
    
@Override
    
public void onNothingSelected(AdapterView<?> arg0) {
        
// TODO - Custom Code
    
}
}
If you observe the above code we are calling our layout using the setContentView method in the form of R.layout.layout_file_name in our activity file. Here our XML file name is activity_main.xml so we used file name activity_main and binding the list of values to Spinner control using ArrayAdapter.




Comments

Popular posts from this blog

How to Integrate or Work with Open Street Map (OSM) in an Android App (Kotlin)

ListView in android with api and volley Network Library in android Studio.

how to implement OpenStreetMap in android