info@thistimebd.com

Thursday 21st of November 06:48:40pm

spinner on item selected android app source code

MainActivity.java

================


package com.sam.web.spinneronitemselected;


import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.AdapterView;

import android.widget.CursorAdapter;

import android.widget.Spinner;

import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    private Spinner spinner;

    private String[] countryNames;

    private String[]population;


    private int[]flags={R.drawable.australia,R.drawable.bangladesh,R.drawable.brazil,R.drawable.hong_kong,

            R.drawable.india,R.drawable.italy,R.drawable.laos,R.drawable.new_zealand,

            R.drawable.pakistan,R.drawable.russia,R.drawable.uk};

    private boolean isFirstSelection=true;




    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        countryNames=getResources().getStringArray(R.array.country_names);

        population=getResources().getStringArray(R.array.populations);


        spinner=(Spinner) findViewById(R.id.spinnerId);

        CustomAdapter customAdapter=new CustomAdapter(this,flags,countryNames,population);

        spinner.setAdapter(customAdapter);

        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override

            public void onItemSelected(AdapterView<?> parent, View view, int i, long id) {

                if(isFirstSelection==true){


                    isFirstSelection=false;

                }else {

                    Toast.makeText(getApplicationContext(),countryNames[i]+" is selected ",Toast.LENGTH_SHORT).show();


                }



            }


            @Override

            public void onNothingSelected(AdapterView<?> parent) {


            }

        });

    }

}

============================================================================


activity_main.xml

-------------------


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

<LinearLayout 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.sam.web.spinneronitemselected.MainActivity">


<Spinner

    android:layout_marginTop="20dp"

    android:id="@+id/spinnerId"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"></Spinner>


</LinearLayout>

================================================================================

string.xml

-----------

<resources>

    <string name="app_name">SpinnerOnItemSelected</string>

    <string-array name="country_names">

        <item>Australia</item>

        <item>Bangladesh</item>

        <item>Brazil</item>

        <item>HongKong</item>

        <item>India</item>

        <item>Italy</item>

        <item>Laos</item>

        <item>NewZialand</item>

        <item>Pakistan</item>

        <item>Russia</item>

        <item>UK</item>

    </string-array>

    <string-array name="populations">

        <item>14 million</item>

        <item>12 million</item>

        <item>10 million</item>

        <item>9 million</item>

        <item>80 million</item>

        <item>122 million</item>

        <item>9 million</item>

        <item>120 million</item>

        <item>20 million</item>

        <item>120 million</item>

        <item>50 million</item>

    </string-array>

</resources>



=============================================================================


CustomAdapter.java

===================


package com.sam.web.spinneronitemselected;


import android.content.Context;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.TextView;


/**

 * Created by maudud on 1/16/18.

 */


public class CustomAdapter extends BaseAdapter {

    private String[] countryNames;

    private String[]population;

    int[] flags;

    Context context;

    private LayoutInflater layoutInflater;


    public CustomAdapter(Context context,int[] flags, String[] countryNames, String[] population) {

        this.countryNames = countryNames;

        this.population = population;

        this.flags = flags;

        this.context = context;

    }



    @Override

    public int getCount() {

        return countryNames.length;

    }


    @Override

    public Object getItem(int position) {

        return null;

    }


    @Override

    public long getItemId(int position) {

        return 0;

    }


    @Override

    public View getView(int i, View view, ViewGroup viewGroup) {

        if (view==null)

        {

          LayoutInflater layoutInflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

          view=layoutInflater.inflate(R.layout.sample_view,null,false);



        }

        ImageView imageView=view.findViewById(R.id.imageViewId);

        imageView.setImageResource(flags[i]);

        TextView country=view.findViewById(R.id.countryNametextViewId);

        country.setText(countryNames[i]);

        TextView populationNumber=view.findViewById(R.id.populatonTextViewId);

        populationNumber.setText(population[i]);

        return view;

    }

}


===============================================================================================

sample_view.xml

------------------


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

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="match_parent"

    android:layout_height="80dp">

    

    <ImageView

        android:id="@+id/imageViewId"

        android:padding="5dp"

        android:src="@drawable/australia"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"


        />

    <LinearLayout

        android:paddingLeft="10dp"

        android:gravity="center"

        android:orientation="vertical"

        android:layout_width="match_parent"

        android:layout_height="match_parent"></LinearLayout>


    <TextView


        android:textColor="@color/colorAccent"

        android:id="@+id/countryNametextViewId"

        android:paddingLeft="10dp"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:textSize="20sp"

        android:textStyle="bold"

        />


    <TextView

        android:text="14 million"

        android:id="@+id/populatonTextViewId"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:textSize="14sp"

        android:textStyle="bold"

        />


</LinearLayout>