- Create a new android project named Spinner.
- Create a user model class User.java. Open the User.java file and edit the file as given below.
package com.example.spinner;
public class User {
int id;
String name;
public User() {
}
public User(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
- Edit your activity_main.xml file as below to create a spinner.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:padding="16dp"
>
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
- Open your MainActivity.java class and edit as below.
package com.example.spinner;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = findViewById(R.id.spinner);
// create array list of user model
ArrayList<User> userList = new ArrayList<>();
// adding element to array list for data
userList.add(new User(100,"John"));
userList.add(new User(101,"Adams"));
userList.add(new User(102,"Baker"));
userList.add(new User(103,"Evans"));
userList.add(new User(104,"Mason"));
// initialize the adapter class
// it require context and the arraylist
Adapter a = new Adapter(this,userList);
// now set the adapter to spinner to populate data
spinner.setAdapter(a);
// add a select listener to check which item is selected
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// this selected method provide the id from the adapter class
// we are toasting the id of selected item
Toast.makeText(getApplicationContext(), "Item at position "+id+" is selected",
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
// lets run a check the app
}
- create a new class Adapter.java and edit the code as below
package com.example.spinner;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;
import androidx.annotation.NonNull;
import java.util.ArrayList;
public class Adapter extends BaseAdapter {
Context mContext;
ArrayList<User> userList;
public Adapter(Context mContext, ArrayList<User> userList) {
this.mContext = mContext;
this.userList = userList;
}
@Override
public int getCount() {
return userList.size();
}
@Override
public Object getItem(int position) {
return userList.get(position);
}
@Override
public long getItemId(int position) {
return userList.get(position).id;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null){
convertView = LayoutInflater.from(mContext).inflate(R.layout.layout_spinner,null,false);
TextView textView = convertView.findViewById(R.id.text);
textView.setText(userList.get(position).getName());
}
return convertView;
}
}
this adapter class help us to populate the data into the spinner.
- create a layout file name layout_spinner.xml and edit the file as below.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/text"
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="22sp"
/>
</LinearLayout>
- follow each step carefully and do check for errors in the file then run the application.
Comments