Android Tutorial , Programming Tutorial, Php Tutorial, Learn Android, HTML Tutorial, Coding , Java Tutorial, GTU Programs, Learning Programming

Friday 14 October 2016

Spinner Example in Android

Spinner is a one type of drop down menu which is allow you to select an item from menu. In this Example, On Item select of Spinner , Background color will be changed with image.

First of all, when you select Red then background color will be changed and set red color and image of red circle will be set in Image View. As the same , Select on Green , background color will be set green and green circle set in Imageview.

Using This Example, We can change background color of the layout on item select in spinner.

So, Let's now see the example :

File Name : MainActivity.java



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.example.spinnerdemo;

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

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity {

 Spinner spinner;
 RelativeLayout r1;
 ImageView i1;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  spinner = (Spinner)findViewById(R.id.spinner1);
  r1 = (RelativeLayout)findViewById(R.id.r1);
  i1 = (ImageView)findViewById(R.id.imageView1);
  List<String> list = new ArrayList<String>();
     list.add("RED");
     list.add("GREEN");
     list.add("BLUE");
      
     ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
     spinner.setAdapter(dataAdapter);
     
  spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

   @Override
   public void onItemSelected(AdapterView<?> arg0, View arg1,
     int arg2, long arg3) {
    // TODO Auto-generated method stub
    
     String item =arg0.getItemAtPosition(arg2).toString(); 
     Toast.makeText(getApplicationContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
    
     if(item == "RED")
     {
      r1.setBackgroundColor(Color.RED);
      i1.setBackgroundResource(R.drawable.red);
     }
     if(item == "GREEN")
     {
      r1.setBackgroundColor(Color.GREEN);
      i1.setBackgroundResource(R.drawable.green);
     }
     if(item == "BLUE")
     {
      r1.setBackgroundColor(Color.BLUE);
      i1.setBackgroundResource(R.drawable.blue);
     }
   }

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

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

}

File Name : activity_main.xml



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<RelativeLayout 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"
    android:id="@+id/r1"
    tools:context=".MainActivity" >

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_below="@+id/spinner1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="92dp"
         />

</RelativeLayout>

Output :

0 comments:

Post a Comment

Like us on Facebook

Site Visitor

Powered by Blogger.