Procedure
The following is an example:
  1. <TableRow android:layout_width="fill_parent"
  2. android:layout_height="wrap_content">
  3. <Button android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:id="@+id/b1"
  6. android:text="Background"/>
  7. <Button android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:id="@+id/b2"
  10. android:text="Yellow"/>
  11. </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. public class MainActivity extends Activity implements OnClickListener{
  12. Button b1,b2,b3,b4;
  13. TableLayout tt;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. b1=(Button)findViewById(R.id.b1);
  19. b2=(Button)findViewById(R.id.b2);
  20. b3=(Button)findViewById(R.id.b3);
  21. b4=(Button)findViewById(R.id.b4);
  22. tt=(TableLayout)findViewById(R.id.tt);
  23. b1.setOnClickListener(this);
  24. b2.setOnClickListener(this);
  25. b3.setOnClickListener(this);
  26. b4.setOnClickListener(this);
  27. }
  28. public void onClick(View v) {
  29. // TODO Auto-generated method stub
  30. switch (v.getId()) {
  31. case R.id.b1:
  32. //tt.setBackgroundColor(Color.RED);
  33. tt.setBackgroundDrawable(getResources().getDrawable(R.drawable.ts));
  34. break;
  35. case R.id.b2:
  36. tt.setBackgroundColor(Color.YELLOW);
  37. break;
  38. case R.id.b3:
  39. tt.setBackgroundColor(Color.BLUE);
  40. Toast.makeText(getApplicationContext(), "Hello Sangeet, Good Morning", 100).show();
  41. break;
  42. case R.id.b4:
  43. tt.setBackgroundColor(Color.GREEN);
  44. Toast.makeText(getApplicationContext(), "Hello Abhijeet, Good Morning", 100).show();
  45. break;
  46. default:
  47. break;
  48. }
  49. }
  50. }
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. <View android:layout_width="fill_parent"
  8. android:layout_height="10px"
  9. android:background="#ff0000"/>
  10. <TableRow android:layout_width="fill_parent"
  11. android:layout_height="wrap_content">
  12. <Button android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:id="@+id/b1"
  15. android:text="Background"/>
  16. <Button android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:id="@+id/b2"
  19. android:text="Yellow"/>
  20. </TableRow>
  21. <View android:layout_width="fill_parent"
  22. android:layout_height="10px"
  23. android:background="#ff0000"/>
  24. <TableRow android:layout_width="fill_parent"
  25. android:layout_height="wrap_content">
  26. <Button android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:id="@+id/b3"
  29. android:text="Blue"/>
  30. <Button android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:id="@+id/b4"
  33. android:text="Green"/>
  34. </TableRow>
  35. <View android:layout_width="fill_parent"
  36. android:layout_height="10px"
  37. android:background="#ff0000"/>
  38. </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: