08 January 2017

Android: Spinner (Combobox android)

activity_main.xml
Java Android 2017
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">

    <Spinner
        android:id="@+id/spnCategory"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="30dp"
        android:layout_width="150dp"
        android:layout_height="50dp" />

</FrameLayout>
MainActivity.java
Java Android 2017
package com.example.lonely.demolisttenner;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private Spinner spnCategory;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        spnCategory = (Spinner) findViewById(R.id.spnCategory);

        List<String> list = new ArrayList<>();
        list.add("Java");
        list.add("Android");
        list.add("PHP");
        list.add("C#");
        list.add("ASP.NET");

        ArrayAdapter<String> adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item,list);
        adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);

        spnCategory.setAdapter(adapter);
        spnCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(MainActivity.this, spnCategory.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });

    }

}
activity_main.xml
Java Android 2017
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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">

    <Spinner
        android:id="@+id/sp_subject"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="30dp"
        android:layout_width="150dp"
        android:layout_height="50dp" />

    <Spinner
        android:id="@+id/sp_city"
        android:layout_gravity="center_horizontal|center"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:entries="@array/country_arrays"
        android:prompt="@string/country_prompt" />

    <TextView
        android:id="@+id/tv_result"
        android:textColor="#e40000"
        android:textSize="19sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center"
        android:layout_marginBottom="150dp"
        android:text="Result City"
        />

    <Button
        android:text="City"
        android:id="@+id/btn_city"
        android:layout_gravity="center_horizontal|bottom"
        android:layout_marginBottom="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</FrameLayout>
MainActivity.java
Java Android 2017
package com.example.lonely.demolisttenner;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private Spinner spnCategory;
    private Spinner spnCity;
    private TextView tvResult;
    private Button btnCity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvResult =(TextView) findViewById(R.id.tv_result);
        spnCategory = (Spinner) findViewById(R.id.sp_subject);
        spnCity = (Spinner) findViewById(R.id.sp_city);
        btnCity = (Button) findViewById(R.id.btn_city);

        addSpinner1();
        selectSpinner1();
        addSpinner2();
    }
    //Spinner 1 add value
    public void addSpinner1() {
        List<String> list = new ArrayList<>();
        list.add("Java");
        list.add("Android");
        list.add("PHP");
        list.add("C#");
        list.add("ASP.NET");

        ArrayAdapter<String> adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, list);
        adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
        spnCategory.setAdapter(adapter);
    }
    //Spinner 1 display Toast
    public void selectSpinner1() {
        spnCategory.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                Toast toast= Toast.makeText(getApplicationContext(),spnCategory.getSelectedItem().toString(), Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.TOP|Gravity.TOP, 0, 350);
                toast.show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }
        });
    }
    //Spinner 2 Display by onClick
    public void addSpinner2() {
        btnCity.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                tvResult.setText("\nSpinner: "+ spnCity.getSelectedItem().toString());
            }
        });
    }
}
strings.xml
Java Android 2017
<resources>
    <string name="app_name">DemoSpinner</string>

    <string name="country_prompt">Chọn Tỉnh/Thành Phố</string>
    <string-array name="country_arrays">
        <item>Hà Nội</item>
        <item>Huế</item>
        <item>Sài Gòn</item>
        <item>Đà Nẵng</item>
        <item>Cần Thơ</item>
    </string-array>

</resources>

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang