My Aim to Provide you quality contents, Tips & Tricks, Software, Microsoft Office, Graphic Editing (Adobe PhotoShop, After Affects, Illustrator, inDesign) Corel Draw, Corel Video Studio, Cyberlink PowerDirector, Power ActionCinema, Tutorials about Blogging and VU Assignments, Quizes & GDB Solutions and Much More... at regular Basis
Add Custom Spinner in Android Application Using Android Studio in a minute
September 23, 2019
Custom Spinner in Android
Create Custom Spinner in Android Studio
As we know that spinner is very important widget in android and plays very important role we can complete many tasks with the help of spinners.
But Somestime we need to show some hints below the Spinner Items or Show Some Kind of Images (below, top or left right etc) or checkbox or radio box for this purpose we need to create custom spinners. Custom Spinners implementation method is totally different than regular spinner like an example Picture is given below.
To create and implement Custom Spinner as showing in above picture just follow the steps.
First of all create two Java Classes by right Click on App Folder and then Select New and then Java Class and give them names as SpinnerItems.java and CustomAdapter.java.
Now you also need to create an xml activities by Right Click on layout folder from left and Select New then Choose Activity and give then name as custom_spinner.xml.
Note! Here you already have MainActivty.java and main_activity.xml files so you need to create them again.
After Creating above mentioned xml activity and java classed now just copy and paste the following codes in their relevant activity or classes and modify this code according your needs.
Click on Tabs to get your codes and copy these codes in to activities as told as above.
package com.infopalacess.customspinnerexample;
public class SpinnerItem {
private String mMainTxt;
private String mSmallTxt;
public SpinnerItem(String mainText, String smallText) {
mMainTxt = mainText;
mSmallTxt = smallText;
}
public String getMainText() {
return mMainTxt;
}
public int getSmallText() {
return smallText;
}
}
package com.infopalacess.customspinnerexample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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 {
private ArrayList customList;
private CustomAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initList();
Spinner spinner = findViewById(R.id.spinner);
mAdapter = new CustomAdapter(this, customList);
spinnerCountries.setAdapter(mAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView parent, View view, int position, long id) {
//getting Spinner Item Selected Postion
Spinner clickedItem = (SpinnerItems) parent.getItemAtPosition(position);
//converting position of item into String
String clickeditemname = clickedItem.getMainText();
//here i am going to show a toast but you can add your own code according to your needs
Toast.makeText(MainActivity.this, clickeditemname + " selected", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView parent) {
}
});
}
private void initList() {
customList = new ArrayList<>();
customList.add(new SpinnerItems("HTML", "It is Structural Layout"));
customList.add(new SpinnerItems("CSS", "It is Presentation Layout"));
customList.add(new SpinnerItems("JavaScript", "It is a behavior Layout"));
customList.add(new CountryItem("Waqas Ahmed", "I am Software Developer"));
}
}
That's it you have done this task now you have a custom Spinner. If you still have any problem or confusion then fill the contact form below and let me know. Thanks
0 Comments