Custom Spinner
Here I have implemented custom spinner example using Model class.
Here is my activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="example.com.spinnercustom.MainActivity"> <Spinner
android:id="@+id/sprCountry"
android:layout_width="match_parent"
android:layout_height="wrap_content"/> </RelativeLayout>
Now we need to create custom layout for spinner.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"> <TextView
android:id="@+id/tvCountryName"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
After that we have to create model class for spinner.
public class SpinnerModel implements Serializable { private String strCountryName; public String getStrCountryName() { return strCountryName; } public void setStrCountryName(String strCountryName) { this.strCountryName = strCountryName; } }
and the code of MainActivity is given below.
public class MainActivity extends AppCompatActivity { private Spinner sprCountry; private ArrayList<SpinnerModel> alCountry; private SpinnerModel model; private SpinnerAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sprCountry = (Spinner) findViewById(R.id.sprCountry); alCountry = new ArrayList<>(); model = new SpinnerModel(); model.setStrCountryName("Select Coutry"); alCountry.add(model); // set First coutry name
model = new SpinnerModel(); model.setStrCountryName("INDIA"); alCountry.add(model); model = new SpinnerModel(); model.setStrCountryName("USA"); alCountry.add(model); model = new SpinnerModel(); model.setStrCountryName("CANADA"); alCountry.add(model); adapter = new SpinnerAdapter(MainActivity.this,alCountry); sprCountry.setAdapter(adapter); sprCountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { Log.e("You select ","=====> "+alCountry.get(i).getStrCountryName()); } @Override public void onNothingSelected(AdapterView<?> adapterView) { } }); } }
and finally I adapter of spinner is
public class SpinnerAdapter extends BaseAdapter { private ArrayList<SpinnerModel> alCountry; private Context context; private LayoutInflater inflater; public SpinnerAdapter(MainActivity mainActivity, ArrayList<SpinnerModel> alCountry) { context= mainActivity; this.alCountry = alCountry; } @Override public int getCount() { return alCountry.size(); } @Override public Object getItem(int i) { return i; } @Override public long getItemId(int i) { return i; } class Holder{ private TextView tvCountryName; } @Override public View getView(int i, View convertView, ViewGroup viewGroup) { View myView = null; try { Holder holder; myView = convertView; if (myView == null) { inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); myView = inflater.inflate(R.layout.spinner_layout, null); holder = new Holder(); holder.tvCountryName = (TextView) myView.findViewById(R.id.tvCountryName); myView.setTag(holder); } else { holder = (Holder) myView.getTag(); } holder.tvCountryName.setText(alCountry.get(i).getStrCountryName()); } catch (Exception e) { e.printStackTrace(); } return myView; } // Below code is used to hide first item.
@Overridepublic View getDropDownView(int position, View convertView, ViewGroup parent) { View v = null; if (position == intHideIndex) { TextView tv = new TextView(context); tv.setVisibility(View.GONE); tv.setHeight(0); v = tv; v.setVisibility(View.GONE); } else v = super.getDropDownView(position, null, parent); return v; }}
Awesome!!!
ReplyDeleteWhat about that intHideIndex? where is it defined? What am I missing here?
ReplyDeleteGreat post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
ReplyDeleteautomation anywhere training in chennai
automation anywhere training in bangalore
automation anywhere training in pune
automation anywhere online training
blueprism online training
rpa Training in sholinganallur
rpa Training in annanagar
iot-training-in-chennai
blueprism-training-in-pune
automation-anywhere-training-in-pune
Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
ReplyDeleterpa Training in Chennai
rpa Training in bangalore
rpa Training in pune
blueprism Training in Chennai
blueprism Training in bangalore
blueprism Training in pune
rpa online training
It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
ReplyDeleteData Science with Python training in chenni
Data Science training in chennai
Data science training in velachery
Data science training in tambaram
Data Science training in OMR
Data Science training in anna nagar
Data Science training in chennai
Data science training in Bangalore
I found this informative and interesting blog so i think so its very useful and knowledge able.I would like to thank you for the efforts you have made in writing this article.
ReplyDeletepython training institute in chennai
python training in velachery
python training institute in chennai
This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me..
ReplyDeleteDevOps online Training|DevOps Training in USA
Devops Training in Chennai
Devops Training in Bangalore
This is very good content you share on this blog. it's very informative and provide me future related information.
ReplyDeleteBlueprism training in Chennai
Blueprism training in Bangalore
Blueprism training in Pune
This is such a great post, and was thinking much the same myself. Another great update.
ReplyDeleteangularjs-Training in chennai
angularjs Training in chennai
angularjs-Training in tambaram
angularjs-Training in sholinganallur
angularjs-Training in velachery
I am really happy with your blog because your article is very unique and powerful for new reader.
ReplyDeleteClick here:
selenium training in chennai
selenium training in bangalore
selenium training in Pune
selenium training in pune
Selenium Online Training
https://sqldbawalkin.blogspot.com/2011/09/training-and-placement-to-dba-pune.html
Were a gaggle of volunteers as well as starting off a brand new gumption within a community. Your blog furnished us precious details to be effective on. You've got completed any amazing work!
ReplyDeleteMicrosoft Azure online training
Selenium online training
Java online training
Python online training
uipath online training
Nice blog..! I really loved reading through this article. Thanks for sharing.You done a great job.Automation Anywhere Training in Bangalore
ReplyDeleteWe are well established IT and outsourcing firm working in the market since 2013. We are providing training to the people ,
ReplyDeletelike- Web Design , Graphics Design , SEO, CPA Marketing & YouTube Marketing.Call us Now whatsapp: +(88) 01537587949
Digital marketing training
good post Mobile XPRESS
Free bangla sex video:Delivery Companies in UK
good post Mobile XPRESS
Thank you so much for this post.
ReplyDeleteData Science Online Training
Python Online Training
Salesforce Online Training
thanks for sharing this useful blog
ReplyDeletebest-angular-training in chennai |
angular-Course in Chennai
Some may stag in Interviews!!! OOPS!! More than 50% of students do this in their career. Instead, do Hadoop Training in Chennai at Infycle. Those students can easily clear this Interview session because more than 5 times at INFYCLE practicing mock-interview sessions, Hence students are Getting out of their interview fear.
ReplyDelete