Tutorial Programming

Tutorial Spinner Android


Hallo sahabat , yang belum tau spinner , spinner itu adalah event select pada android, untuk melakukan tindakan yang di inginkan, nah kalau secara detailnya cukup itu yang saya tau hehehe, oke kita langsung ke TKP.

1. Buatlah project android kalian
2. lalu copy coding MainActiviy.java tersebut


package com.example.tutorspinner;

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;
    private Button btnSubmit;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        spinner1 = (Spinner) findViewById(R.id.spinner1);
        List<String> list = new ArrayList<String>();
        list.add("Eko");
        list.add("Jaya");
        list.add("Fazri");
        list.add("Ariandi");
        list.add("Wong Palembang");
         
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>
                     (this, android.R.layout.simple_spinner_item,list);
                      
        dataAdapter.setDropDownViewResource
                     (android.R.layout.simple_spinner_dropdown_item);
                      
        spinner1.setAdapter(dataAdapter);
         
        // Spinner item selection Listener  
        addListenerOnSpinnerItemSelection();
         
        // Button click Listener 
        addListenerOnButton();
         
 
    }
 
    // Add spinner data
     
    public void addListenerOnSpinnerItemSelection(){
         
                spinner1.setOnItemSelectedListener(new CallBackSpinnerOnItemSelectedListener());
    }
     
    //get the selected dropdown list value
     
    public void addListenerOnButton() {
 
        spinner1 = (Spinner) findViewById(R.id.spinner1);
         
        btnSubmit = (Button) findViewById(R.id.btnSubmit);
 
        btnSubmit.setOnClickListener(new OnClickListener() {
 
            @Override
            public void onClick(View v) {
 
                Toast.makeText(MainActivity.this,
                        "On Button Click : " + 
                        "\n" + String.valueOf(spinner1.getSelectedItem()) ,
                        Toast.LENGTH_LONG).show();
            }
 
        });
 
    }
 
}


3. lalu buatlah CallBackSpinnerOnItemSelectedListener.java pada class kalian masing-masing, dan copy coding berikut


package com.example.tutorspinner;



import android.view.View;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemSelectedListener;

import android.widget.Toast;
 


public class CallBackSpinnerOnItemSelectedListener implements OnItemSelectedListener {
 

    
 public void onItemSelected(AdapterView<?> parent, View view, int pos,
 long id) {
 
        
      
 Toast.makeText(parent.getContext(), 
                
 "On Item Select : \n" + parent.getItemAtPosition(pos).toString(),
                
 Toast.LENGTH_LONG).show();
    

 } 
 
    

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

 }
 


}

4. Copy coding activity_main.xml

<?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" />
     
    <Button
        android:id="@+id/btnSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click here"
        android:paddingTop="20px"
         />
 
</LinearLayout>

5. Selesai spinner kalian telah selesai



Sekian tutorial saya, tetap semangat yah sahabat. Goodjob

No comments:

Post a Comment

Tutorial Programming Designed by Templateism | Ary EJFA Copyright © 2015

Theme images by richcano. Powered by Blogger.