mymemotips

☰ Menu
  • Home
  • Articles
  • Android
  • Java 4 Beginner
  • Java 4 Ad
  • C#
  • Ubuntu tips

POPULAR POSTS

  • VMware Workstation 12, 11, 10 and 9 Unlocker to Run Mac OS X Guests on Windows 10, 8.1 and 7
    VMware Workstation 12, 11, 10 and 9 Unlocker to Run Mac OS X Guests on Windows 10, 8.1 and 7
    I find along time with mac OSX Yosemite VMware iso image in this post I will to discus with this poasted.On the off chance that you are a Ha...
  • How to install genymotion in Ubuntu Zesty 17.04
    How to install genymotion in Ubuntu Zesty 17.04
    In this post we will talking about how to install Genymotion emulate phone.Many asked us for installation instructions for installing Genymo...
  • Download Khmer Unicode for Windows and Mac OSX (Apple Computer)
    Download Khmer Unicode for Windows and Mac OSX (Apple Computer)
     Khmer Unicode is used everywhere nowaday even in government, companies, schools, and individule for making official documents or unofficial...
  • Create local chatroom with java using socket
    Create local chatroom with java using socket
    Hello every one in this video I will show you how to create local chat room in java. In this article I used socket by create socket object. ...
  • How to make Ubuntu look like mac OS X
    How to make Ubuntu look like mac OS X
    In this posted we will talking about how to customize Ubuntu 17.10 look like Mac OSX. I'm really tired to boot new OSX to my computer be...
  • Useful Windows 10 Shortcuts You Should Be Using
    Useful Windows 10 Shortcuts You Should Be Using
    The best way to use a Windows laptop or PC is through the various keyboard shortcuts. In this video, we show you 10 useful Windows shortcuts...
  • Best Windows 11 live wallpapers and animated wallpapers
    Best Windows 11 live wallpapers and animated wallpapers
     Customizing your desktop background is one of the most common tasks for personalizing your computer in your daily life. If you're tired...
  • Windows 10 Activator update 2022
    Windows 10 Activator update 2022
     Windows 10 Activator Txt is a text-based free Windows initiation technique. Here we will direct you through the Windows 10 Pro initiation p...
  • How to Add a Google Sitemap to your Blogger Blog
    How to Add a Google Sitemap to your Blogger Blog
    I'm really tired how to generate sitemap for blogger I try to research more about this. I found the solution to use xml sitemap is sort ...
  • Top 7 new features included in Windows 11
    Top 7 new features included in Windows 11
     This month, Microsoft released Windows 11, bringing significant changes to the storied operating system, including the first name change in...

NEW! CONTROVERSIAL Software Exploits $12.3 MILLION LOOPHOLE & Makes Us $519/DAY....

Cloud Tag

  • Access 2
  • Android 3
  • ArticleDotNet 2
  • Articles 15
  • Genymotion 1
  • Java 1
  • Python 1
  • Software 3
  • Ubuntu 3
  • Windows 4
Home Android Articles Create Android spinner (drop down list) example

Create Android spinner (drop down list) example

September 23, 2018

In this posted we show you how to do the following this:

1-Render a Spinner in XML, and load the selection items via XML file also.
2-Render another Spinner in XML, and load the selection items via code dynamically.
3-Attach a listener on Spinner, fire when user select a value in Spinner.
4-Render and attach a listener on a normal button, fire when user click on it, and it will display selected value of Spinner.
1. First we need to create list of items in spinner
 Open folder " res/values/strings.xml" file, and we need to definde the list of items
 
strings.xml file



<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">MyAndroidApp</string>
    <string name="country_prompt">Choose a country</string>

    <string-array name="country_arrays">
        <item>Thai</item>
        <item>United States</item>
        <item>Indonesia</item>
        <item>France</item>
        <item>Italy</item>
        <item>Singapore</item>
        <item>New Zealand</item>
        <item>India</item>
    </string-array>

</resources>
2 . Spinner (DropDown List)

Open folder "res/layout/main_activity.xml" and add spinner and button
1. "spinner1”, the “android:entries” represents the selection items in spinner.
2. “spinner2”, the selection items will be defined in code later.

  Open folder "res/layout/main_activity.xml" add the code below :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:entries="@array/country_arrays"
        android:prompt="@string/country_prompt" />

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btnSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit" />

</LinearLayout>


3. In MainActivity.java

package com.spinner.example.android;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity {

  private Spinner spinner1, spinner2;
  private Button btnSubmit;

  @Override
  public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

 addItemsOnSpinner2();
 addListenerOnButton();
 addListenerOnSpinnerItemSelection();
  }

  // add items into spinner dynamically
  public void addItemsOnSpinner2() {

 spinner2 = (Spinner) findViewById(R.id.spinner2);
 List<String> list = new ArrayList<String>();
 list.add("list 1");
 list.add("list 2");
 list.add("list 3");
 ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
  android.R.layout.simple_spinner_item, list);
 dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 spinner2.setAdapter(dataAdapter);
  }

  public void addListenerOnSpinnerItemSelection() {
 spinner1 = (Spinner) findViewById(R.id.spinner1);
 spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
  }

  // get the selected dropdown list value
  public void addListenerOnButton() {

 spinner1 = (Spinner) findViewById(R.id.spinner1);
 spinner2 = (Spinner) findViewById(R.id.spinner2);
 btnSubmit = (Button) findViewById(R.id.btnSubmit);

 btnSubmit.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {

     Toast.makeText(MyAndroidAppActivity.this,
  "OnClickListener : " + 
                "\nSpinner 1 : "+ String.valueOf(spinner1.getSelectedItem()) + 
                "\nSpinner 2 : "+ String.valueOf(spinner2.getSelectedItem()),
   Toast.LENGTH_SHORT).show();
   }

 });
  }
}


Custom Select item


package com.spinner.example.android;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;

public class CustomOnItemSelectedListener implements OnItemSelectedListener {

  public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
 Toast.makeText(parent.getContext(), 
  "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
  Toast.LENGTH_SHORT).show();
  }

  @Override
  public void onNothingSelected(AdapterView<?> arg0) {
 // TODO Auto-generated method stub
  }

}

4 Run Our App












  • Twitter
  • Facebook
  • Google
  • Tumblr
  • Pinterest

Related Posts

  • Enhanced TikTok Video Downloader with Stop, Pause, and Resume FunctionalityIntroductionIn today's digital landscape, TikTok has emerged as one of the most popular social media platforms, with users constantly ...
  • Create local chatroom with java using socketHello every one in this video I will show you how to create local chat room in java. In this article I used socket by create socket ob ...
  • How to add Header in Listview Android?In this posted I will talking about how to customize Header of ListView in android. I really tired to find more examples today I have ...
  • Javascript for scraping video from Kuaishou  When developing a Chrome extension to automatically extract and download videos from platforms like KuaiShou, one of the biggest ...
  • Google's Android Oreo Was Announced Sometime Back - What Sets It Apart From Its Predecessors?So you more likely than not began seeing Oreo in numerous gadgets as Google extends its rollout. The most famous gadgets to convey Ore ...
Next
« Prev Post
Previous
Next Post »

  • Sitemap
  • Privacy Policy
  • Contact Us
  • About Us
Copyright © 2016 mymemotips