Showing posts with label Background Color. Show all posts
Showing posts with label Background Color. Show all posts

Thursday, 27 November 2014

Android : Change Background Color Using Menu Option.

In this tutorial we will learn how add menu option and how to change background color using menu option.

1. Create New Android Application Project. Name it as ColorUsingMenu.
 


2. Open Your activity_main.xml file.
3. Replace your activity_main.xml code with below code.


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/Rl"
    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"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Change background color using Menu  options.." />

</RelativeLayout>


 Your activity_main.xml will look like below..
 


4 . Open your Mainactivity.java file.

5. Replace Your Mainactivity.java code with below code.


package arshad.colorusingmenu;


import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;;

public class MainActivity extends Activity {

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
       }

         public boolean onOptionsItemSelected(MenuItem item){
              RelativeLayout Ri=(RelativeLayout)findViewById(R.id.Rl);
              if(item.getTitle().toString().equals("RED"))
                     Ri.setBackgroundColor(Color.RED);
              if(item.getTitle().toString().equals("GREEN"))
                     Ri.setBackgroundColor(Color.GREEN);
              if(item.getTitle().toString().equals("BLUE"))
                     Ri.setBackgroundColor(Color.BLUE);
              if(item.getTitle().toString().equals("WHITE"))
                     Ri.setBackgroundColor(Color.WHITE);
              if(item.getTitle().toString().equals("YELLOW"))
                     Ri.setBackgroundColor(Color.YELLOW);
             
              return true;
           }
          
          
          
          
           public boolean onCreateOptionsMenu(Menu menu){
              menu.add("RED").setIcon(android.R.drawable.ic_menu_call);
              menu.add("GREEN");
              menu.add("BLUE");
              menu.add("WHITE");
              menu.add("YELLOW");
              return true;
           }
       }


 6. Now Build And Run Your Application.
  
7. Output screens..






Use menu options to change background colors..


 

All The Best.
 


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