Saturday 14 September 2013

Change Background Color : Android


Change Background Color

Create New Android Application Project.
Add 5 Buttons To Your Relative Layout. Or Drag and Drop 5Buttons to your Graphical Layout.
Set ID To Your Relative Layout So We Can Change Background Color Of Our Layout.
Changing Background Color is Very Simple Task.
I Set My Layout ID as  android:id="@+id/arshadlayout"
My XML Code Now Looks Like Below.

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:id="@+id/arshadlayout"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="46dp"
        android:layout_marginTop="24dp"
        android:text="Red" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="14dp"
        android:text="Green" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button2"
        android:layout_below="@+id/button2"
        android:layout_marginTop="15dp"
        android:text="Yellow" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button3"
        android:layout_below="@+id/button3"
        android:layout_marginTop="14dp"
        android:text="Blue" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button4"
        android:layout_below="@+id/button4"
        android:layout_marginTop="14dp"
        android:text="Gray" />






Now Come To Our MainActivity.Java File.
We Have To Create Instance or Object Of Button And Relative Layout as
       Button red,green,yellow,blue,gray;
       RelativeLayout l;

And In OnCreate Method Add This Code.
l=(RelativeLayout)findViewById(R.id.arshadlayout);
       red = (Button) findViewById(R.id.button1);
              green = (Button) findViewById(R.id.button2);
              yellow = (Button) findViewById(R.id.button3);
              blue = (Button) findViewById(R.id.button4);
              gray = (Button) findViewById(R.id.button5);
Set OnclickListener To All Buttons





package arshad.backgroundcolor;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {
       Button red,green,yellow,blue,gray;
       RelativeLayout l;
       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              l=(RelativeLayout)findViewById(R.id.arshadlayout);
       red = (Button) findViewById(R.id.button1);
              green = (Button) findViewById(R.id.button2);
              yellow = (Button) findViewById(R.id.button3);
              blue = (Button) findViewById(R.id.button4);
              gray = (Button) findViewById(R.id.button5);
             
              red.setOnClickListener(new OnClickListener() {

                     @Override
                     public void onClick(View arg0) {

                      l.setBackgroundColor(Color.RED);

                     }
              });
                          
              green.setOnClickListener(new OnClickListener() {

                     @Override
                     public void onClick(View arg0) {

                      l.setBackgroundColor(Color.GREEN);

                     }
              });   
                    
              yellow.setOnClickListener(new OnClickListener() {

                     @Override
                     public void onClick(View arg0) {

                      l.setBackgroundColor(Color.YELLOW);

                     }
              });   
             
              blue.setOnClickListener(new OnClickListener() {

                     @Override
                     public void onClick(View arg0) {

                      l.setBackgroundColor(Color.BLUE);

                     }
              });   
             
              gray.setOnClickListener(new OnClickListener() {

                     @Override
                     public void onClick(View arg0) {

                      l.setBackgroundColor(Color.GRAY);

                     }
              });   
             
       }

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

}


Output Will Be Like








 After Clicking Red Button


 
 
After Clicking Green Button OutPut Looks Like Below.



 And So On .....

All The Best

No comments:

Post a Comment