Twitter Facebook Delicious Digg Stumbleupon Favorites More

Sunday 18 June 2017

spinner using xml array list

STRING XML FILE
<resources>
    <string name="app_name">Blog_11_spinner</string>
    <string-array name="friendsname">
        <item>nilesh</item>
        <item>ravi</item>
        <item>VIRAJ</item>
        <item>LOKENDRA</item>
        <item>sachine</item>
    </string-array>
</resources>

JAVA FILE

package com.example.viraj.blog_11_spinner;

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

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //create the data ,, define apperencelayoutfile yhrough which the adapter pull dtat inside spinner,
        // , define what the user want the used=r click on spiner using onItemSelectedListner
        Spinner spinner = (Spinner) findViewById(R.id.id_name_spinner);
        ArrayAdapter arrayAdapter = ArrayAdapter.createFromResource(this, R.array.friendsname, android.R.layout.simple_spinner_item);
        spinner.setAdapter(arrayAdapter);
        spinner.setOnItemSelectedListener(this);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        TextView myt1 = (TextView) view;
        Toast.makeText(this, "you seleted" + myt1.getText(), Toast.LENGTH_LONG).show();
    }

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

    }
}

XML FILE

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.viraj.blog_11_spinner.MainActivity">

    <Spinner
        android:id="@+id/id_name_spinner"
        android:layout_width="368dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.212"
     />
</android.support.constraint.ConstraintLayout>


Share:

0 comments:

Post a Comment

Search This Blog

Popular Posts

Pages

how to make crores from 1 lakh in stock markets in 1 year

how to make crores from 1 lakh in stock markets in 1 year

Blogger Tutorials

Blogger Templates

Sample Text

Copyright © ANDROID TUTORIAL CODE | Powered by Blogger
Design by SimpleWpThemes | Blogger Theme by NewBloggerThemes.com & Distributed By Protemplateslab