The result will show as below screen.
Layout Name : activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<Spinner
android:layout_marginTop="20dp"
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Step 2 : Java class add in following:
1. List String for "studentList" consist of studentA, studentB, studentC.
2. Create a new ArrayAdapter and assigned the "List".
3. Declare Spinner and find id from the activity_main.xml.
4. Set spinner "setOnItemSelectedListener" to get selected value.
Spinner spinner = (Spinner) findViewById(R.id.spinner);
List<String> list = new ArrayList<String>();
list.add("student A");
list.add("student B");
list.add("student C");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setOnItemSelectedListener(this);
spinner.setAdapter(dataAdapter);
Step 3 : Add in implements AdapterView.OnItemSelectedListener to listen the spinner selected value.
implements AdapterView.OnItemSelectedListener
Step 4 : Override "onItemSelected" & "onNothingSelected". onItemSelected trigger when item different value selected.
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(parent.getContext(), "OnItemSelectedListener : "
+ parent.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) { }
Full Coding as screen below:
package com.example.myapplication;
import androidx.appcompat.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;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity
implements AdapterView.OnItemSelectedListener{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(parent.getContext(), "OnItemSelectedListener : "
+ parent.getItemAtPosition(position).toString(),Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Spinner spinner = (Spinner) findViewById(R.id.spinner);
List<String> list = new ArrayList<String>();
list.add("student A");
list.add("student B");
list.add("student C");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setOnItemSelectedListener(this); spinner.setAdapter(dataAdapter); } }
THE END. HAPPY CODING. :)
No comments:
Post a Comment