Free Download Software and Android Tutorials

Thursday, August 20, 2015

Android studio tutorial featuring items with spinner

Spinner is one component input in android studio, where through the spinner we can choose the items which we then take these items to be displayed. Usually in a spinner object already there is a choice of what items you want displayed, and for one spinner we can only choose one item only. For more details, we immediately practice just by creating a new project with the name Spinner.
After filling the name of the project with Spinner next you select Blank Activity, then later Spinner project consists of two java files and a layout. Here are the details of the files contained in the project Spinner.
  • activity_main.xml
  • Main_Activity.java
  • ListenerKhusus.java
The source code for each file is as follows:

Source code 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">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Negara"
        android:textAppearance="?android:attr/textAppearanceLarge"/>

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

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Makanan"
        android:textAppearance="?android:attr/textAppearanceMedium"/>

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

    <Button
        android:id="@+id/btnKirim"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Kirim"/>
</LinearLayout>
Source Code Main_Activity.java
package com.dsoccerupdate.spinner;

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 btnKirim;

    @Override
    public void onCreate(Bundle SavedInstanceState){
        super.onCreate(SavedInstanceState);
        setContentView(R.layout.activity_main);

        tambahkanItemSpinner2();
        tambahkanListenerKeButton();
        tambahkanListenerKeSeleksiSpinner();
    }
    public void tambahkanItemSpinner2() {
        spinner2 = (Spinner)findViewById(R.id.spinner2);
        List<String> list = new ArrayList<String>();
        list.add("Sate");
        list.add("Soto");
        list.add("Rendang");
        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 tambahkanListenerKeSeleksiSpinner(){
        spinner1 = (Spinner)findViewById(R.id.spinner1);
        spinner1.setOnItemSelectedListener(new ListenerKhusus());
    }
    public void tambahkanListenerKeButton() {
        spinner1 = (Spinner)findViewById(R.id.spinner1);
        spinner2 = (Spinner)findViewById(R.id.spinner2);
        btnKirim = (Button)findViewById(R.id.btnKirim);
        btnKirim.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,
                        "Data Dari Listener:"+
                "\nSpinner 1:"+
                String.valueOf(spinner1.getSelectedItem())+
                "\nSpinner 2:"+
                String.valueOf(spinner2.getSelectedItem()),
                        Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Source code ListenerKhusus.java
package com.dsoccerupdate.spinner;

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


public class ListenerKhusus implements OnItemSelectedListener{
    public void onItemSelected(AdapterView<?> parent, View view, int pos,long id){
        Toast.makeText(parent.getContext(),
                "Data dari Listener :"+
        parent.getItemAtPosition(pos).toString(),
                Toast.LENGTH_SHORT).show();
    }
    public void onNothingSelected(AdapterView<?> arg0){
        //TODO Auto-generated method stub
    }
}

Do not forget strings.xml files contained in res > values you like to change to be like this
Source code strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">Spinner</string>

    <string name="nama_negara">Pilih Negara Anda !</string>
    <string-array name="array_negara">
        <item>Malaysia</item>
        <item>Amerika Serikat</item>
        <item>Indonesia</item>
        <item>Kanada</item>
        <item>Thailand</item>
        <item>Singapura</item>
        <item>Cayman Island</item>
        <item>India</item>
        <item>Somalia</item>
    </string-array>
</resources>

Android studio tutorial featuring items with spinner

Android studio tutorial featuring items with spinner

Android studio tutorial featuring items with spinner

Similarly android studio tutorial how to display items from Spinner, hopefully from this short article can provide benefits. Look forward to the next article about android studio tutorial.

Android studio tutorial featuring items with spinner Rating: 4.5 Diposkan Oleh: Unknown

0 comments:

Post a Comment