Tech Programming Ideas is one of the best places on the programming for programmers. Learn coding with Tech Programming Ideas tutorials. We are covered android programming, php, yii2 framework, javascript, mysql, vb.net etc.
Spinner in Android application is equivalent of ComboBox
Spinners provide a quick way to select one value from a set. In the default state, a spinner
shows its currently selected value. Touching the spinner displays a dropdown menu with all other
available values, from which the user can select a new one.
You can add a spinner to your layout with the spinner.
add code in onCreate method public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Spinner spnCountry = (Spinner) findViewById(R.id.spnCountry); ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,getResources().getStringArray(R.array.country_list)); myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spnCountry.setAdapter(myAdapter); } } Video
How to set selected item of Spinner programmatically in Android
Get spinner selected items text?
This example enplane value display on button click Java code Button btnSubmit = (Button) findViewById(R.id.btnSubmit); btnSubmit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(TestActivity.this,String.valueOf(spnCountry.getSelectedItem()),Toast.LENGTH_LONG).show(); } }); Example 2
SpinnerspnCountry=(Spinner)findViewById(R.id.spnCountry);String text =spnCountry.getSelectedItem().toString();
Handle spinner click event
Spinner setOnItemSelectedListener
spnCountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Toast.makeText(TestActivity.this,parent.getItemAtPosition(position) + " Selected",Toast.LENGTH_LONG).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { } });
Set Key and Value in spinner
SpinnerCountry.java
package com.example.root.spinner;
/**
* Created by root on 8/6/19.
*/
public class SpinnerCountry {
Create simple application for Add or Subtract two numbers using android studio.
User Interface
Layout File
activity_add.xml Here is the XML code for the design.
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.root.layouts.AddActivity">
month
ofyear
for the specifiedcalendar
.