How to Add Color Events in Android

Procedure
  • Start Eclipse IDE.
  • Create a new project.
  • Create a MainActivity.java file.
  • Create an XML file in which there are two TableRows having two buttons each.
The following is an example:
  1. <TableRow android:layout_width="fill_parent"  
  2.     android:layout_height="wrap_content">  
  3.       
  4.     <Button android:layout_width="wrap_content"  
  5.         android:layout_height="wrap_content"  
  6.         android:id="@+id/b1"  
  7.         android:text="Background"/>  
  8.           
  9.     <Button  android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:id="@+id/b2"  
  12.         android:text="Yellow"/>  
  13.           
  14. </TableRow>   
In the onClick function add logic for each button using setBackgroundColor.
 
The code is as follows.
 
MainActivity.java
  1. package com.example.colorsevent;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.graphics.Color;  
  5. import android.view.Menu;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.TableLayout;  
  10. import android.widget.Toast;  
  11.   
  12. public class MainActivity extends Activity implements OnClickListener{  
  13.    Button b1,b2,b3,b4;  
  14.    TableLayout tt;  
  15.   
  16.    @Override  
  17.    protected void onCreate(Bundle savedInstanceState) {  
  18.       super.onCreate(savedInstanceState);  
  19.       setContentView(R.layout.activity_main);  
  20.       b1=(Button)findViewById(R.id.b1);  
  21.       b2=(Button)findViewById(R.id.b2);  
  22.       b3=(Button)findViewById(R.id.b3);  
  23.       b4=(Button)findViewById(R.id.b4);  
  24.       tt=(TableLayout)findViewById(R.id.tt);  
  25.       b1.setOnClickListener(this);  
  26.       b2.setOnClickListener(this);  
  27.       b3.setOnClickListener(this);  
  28.       b4.setOnClickListener(this);  
  29.    }  
  30.   
  31.    public void onClick(View v) {  
  32.       // TODO Auto-generated method stub  
  33.       switch (v.getId()) {  
  34.       case R.id.b1:  
  35.          //tt.setBackgroundColor(Color.RED);  
  36.          tt.setBackgroundDrawable(getResources().getDrawable(R.drawable.ts));  
  37.       break;  
  38.       case R.id.b2:  
  39.          tt.setBackgroundColor(Color.YELLOW);  
  40.       break;  
  41.       case R.id.b3:  
  42.          tt.setBackgroundColor(Color.BLUE);  
  43.          Toast.makeText(getApplicationContext(), "Hello Sangeet, Good Morning"100).show();  
  44.       break;  
  45.       case R.id.b4:  
  46.          tt.setBackgroundColor(Color.GREEN);  
  47.          Toast.makeText(getApplicationContext(), "Hello Abhijeet, Good Morning"100).show();  
  48.       break;  
  49.       default:  
  50.       break;  
  51.       }  
  52.    }  
activity_main.xml
  1. <TableLayout  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:id="@+id/tt"      
  6.     android:layout_height="match_parent">  
  7.       
  8.     <View android:layout_width="fill_parent"  
  9.          android:layout_height="10px"  
  10.          android:background="#ff0000"/>  
  11.            
  12.     <TableRow android:layout_width="fill_parent"  
  13.        android:layout_height="wrap_content">  
  14.           
  15.         <Button android:layout_width="wrap_content"  
  16.            android:layout_height="wrap_content"  
  17.            android:id="@+id/b1"  
  18.            android:text="Background"/>  
  19.              
  20.         <Button  android:layout_width="wrap_content"  
  21.            android:layout_height="wrap_content"  
  22.            android:id="@+id/b2"  
  23.            android:text="Yellow"/>  
  24.              
  25.     </TableRow>  
  26.     <View android:layout_width="fill_parent"  
  27.          android:layout_height="10px"  
  28.          android:background="#ff0000"/>  
  29.            
  30.     <TableRow android:layout_width="fill_parent"  
  31.        android:layout_height="wrap_content">  
  32.           
  33.         <Button android:layout_width="wrap_content"  
  34.            android:layout_height="wrap_content"  
  35.            android:id="@+id/b3"  
  36.            android:text="Blue"/>  
  37.              
  38.         <Button android:layout_width="wrap_content"  
  39.            android:layout_height="wrap_content"  
  40.            android:id="@+id/b4"  
  41.            android:text="Green"/>  
  42.              
  43.     </TableRow>  
  44.       
  45.     <View android:layout_width="fill_parent"  
  46.          android:layout_height="10px"  
  47.          android:background="#ff0000"/>  
  48.            
  49. </TableLayout> 
Output
 
By clicking on the Blue button:
 
 
By clicking on the Green button:
 
 
By clicking on the Background button:
 
 
By clicking on the Yellow button:
 
 


Similar Articles