How to set spinner in android studio with source code - RSM Developer

how to set spinner App in Android Studio
how to set spinner in android studio

Presenting brand new Article:
In this video you will learn how to set spinner App in Android Studio. Just follow the steps in the 
Article. More article about Android Application Development will uploaded so get in touch with the channel. So you are no more far. You can be developer. 

Step 1Creating a new project

  • Open a new project.
  • We will be working on Empty Activity with language as Java. Leave all other options unchanged.
  • You can change the name of the project at your convenience.
  • There will be two default files named activity_main.xml and MainActivity.java.

Step 2: Open res -> layout ->activity_main.xml (or) main.xml and add following code:

In this step we open an XML file and add the code :-

<?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"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your home division"
android:textSize="30sp"
android:gravity="center"
android:layout_marginTop="50dp"
android:textColor="@android:color/holo_blue_dark"
/>


<!--Spinner widget-->
<Spinner
android:id="@+id/spinner"
android:layout_height="50dp"
android:layout_width="wrap_content"
android:layout_marginTop="5dp"
android:layout_gravity="center"
/>

</LinearLayout>

Step 3: Open Java -> package – > MainActivity.Java and add following code:

In this step we open an Java file and add the code :-

package com.rsmdeveloper.spnnertest;

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;

/**
* Created by RSM Developer on 25-04-2023.
* Follow Facebook : https://www.facebook.com/RsmDeveloper
* Subscribe YouTube : https://www.youtube.com/@RsmDeveloper
* Visit Website : https://rsmdeveloper.blogspot.com/
* Developed Your Creativity With RSM Developer
**/

public class MainActivity extends AppCompatActivity {

//============================================================================================
Spinner spinner;
ArrayAdapter<String> adapter;
//============================================================================================
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


//String Start============================================================================================
spinner = findViewById(R.id.spinner);
String[] info = {"Barishal", "Chattogram", "Dhaka", "Khulna", "Rajshahi", "Rangpur", "Mymensingh", "Sylhet"};
adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, info);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
//String End============================================================================================

//OnItemSelectedListener Start============================================================================================
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long l) {

if(parent.getItemAtPosition(pos).equals("Barishal")){

Toast.makeText(MainActivity.this, "My home division is Barishal",
Toast.LENGTH_LONG).show();


} else if(parent.getItemAtPosition(pos).equals("Chattogram")){
Toast.makeText(MainActivity.this, "My home division is Chattogram",
Toast.LENGTH_LONG).show();


}else if(parent.getItemAtPosition(pos).equals("Dhaka")){
Toast.makeText(MainActivity.this, "My home division is Dhaka",
Toast.LENGTH_LONG).show();


}else if(parent.getItemAtPosition(pos).equals("Khulna")){
Toast.makeText(MainActivity.this, "My home division is Khulna",
Toast.LENGTH_LONG).show();


}else if(parent.getItemAtPosition(pos).equals("Rajshahi")){
Toast.makeText(MainActivity.this, "My home division is Rajshahi",
Toast.LENGTH_LONG).show();


}else if(parent.getItemAtPosition(pos).equals("Rangpur")){
Toast.makeText(MainActivity.this, "My home division is Rangpur",
Toast.LENGTH_LONG).show();


}else if(parent.getItemAtPosition(pos).equals("Mymensingh")){
Toast.makeText(MainActivity.this, "My home division is Mymensingh",
Toast.LENGTH_LONG).show();


}else if(parent.getItemAtPosition(pos).equals("Sylhet")){
Toast.makeText(MainActivity.this, "My home division is Sylhet",
Toast.LENGTH_LONG).show();


}

}
//OnItemSelectedListener End===================================================================================================
@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}
});
}
// onCreate End===================================================================================================
}

Output:

Now run the App and you will see main topics and sub-topics listed.....

How to set spinner in android studio



Spinner Navigation Bar in android Studio With Fragment



spinner in android studio example, spinner in android studio java, spinner in android studio source code, spinner in android studio Java,  spinner in android studio github, spinner design in android, android, spinner example Java, how to set spinner in android studio

Post a Comment

Previous Post Next Post

যোগাযোগ ফর্ম