HOW TO SET BACKGROUND COLOR USING SPINNER
Spinner.xml File
Spinner.xml File
<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/layer"
tools:context=".MainActivity"
android:background="@drawable/k">
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:entries="@array/color"
android:layout_marginTop="100dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner1"
android:layout_centerHorizontal="true"
android:layout_marginTop="158dp"
android:text="SetColor" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:text="SelectBackgroundColor"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layer"
tools:context=".MainActivity"
android:background="@drawable/k">
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:entries="@array/color"
android:layout_marginTop="100dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner1"
android:layout_centerHorizontal="true"
android:layout_marginTop="158dp"
android:text="SetColor" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:text="SelectBackgroundColor"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
String.xml file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">SpinnerColor</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string-array name="color">
<item >RED</item>
<item >GREEN</item>
<item >BLUE</item>
<item >YELLOW</item>
</string-array>
</resources>
<resources>
<string name="app_name">SpinnerColor</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string-array name="color">
<item >RED</item>
<item >GREEN</item>
<item >BLUE</item>
<item >YELLOW</item>
</string-array>
</resources>
Spinner.java File
package com.example.spinnercolor;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Spinner;
public class spin extends Activity {;
Spinner sp;
Button setcolor;
RelativeLayout rl;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp=(Spinner)findViewById(R.id.spinner1);
setcolor=(Button)findViewById(R.id.button1);
rl=(RelativeLayout)findViewById(R.id.layer);
setcolor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(sp.getSelectedItemId()==0)
{
rl.setBackgroundColor(Color.RED);
}
else if(sp.getSelectedItemId()==1)
{
rl.setBackgroundColor(Color.GREEN);
}
else if(sp.getSelectedItemId()==2)
{
rl.setBackgroundColor(Color.BLUE);
}
else
{
rl.setBackgroundColor(Color.YELLOW);
}
}
});
}
}
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Spinner;
public class spin extends Activity {;
Spinner sp;
Button setcolor;
RelativeLayout rl;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp=(Spinner)findViewById(R.id.spinner1);
setcolor=(Button)findViewById(R.id.button1);
rl=(RelativeLayout)findViewById(R.id.layer);
setcolor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(sp.getSelectedItemId()==0)
{
rl.setBackgroundColor(Color.RED);
}
else if(sp.getSelectedItemId()==1)
{
rl.setBackgroundColor(Color.GREEN);
}
else if(sp.getSelectedItemId()==2)
{
rl.setBackgroundColor(Color.BLUE);
}
else
{
rl.setBackgroundColor(Color.YELLOW);
}
}
});
}
}
Spinner OutPut