Change TableRow background color if on row click Android example

Change table row background color if user click on row Android example code.
MainActivity.java

public class MainActivity extends Activity {
Boolean bColorYellow = true;
TextView hTextView;
TableRow hTableRow;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        hTextView = (TextView)findViewById(R.id.idTextView);
        hTableRow = (TableRow)findViewById(R.id.idTableRow1);
 
 
 
 
    } // end onCreate
 
 
    public void myTableRowClickHandler(View view) {
		switch (view.getId()) {
		case R.id.idTableRow1:{
 
			if(bColorYellow){
			hTableRow.setBackgroundColor(Color.GREEN);
			bColorYellow = false;
			}
			else{
			hTableRow.setBackgroundColor(Color.YELLOW);
			bColorYellow = true;
			}
		}
		break;
		}
  }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 
 
<TableLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/tableLayout1">
    <TableRow android:id="@+id/idTableRow1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#5655AA"
    android:onClick="@string/myTableRowClick"
    android:focusable="true">
<TextView  
	android:id="@+id/idTextView"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
 
    </TableRow>
</TableLayout>
 
 
</LinearLayout>

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World!</string>
    <string name="app_name">TableRow</string>
    <string name="myTableRowClick">myTableRowClickHandler</string>
</resources>
This entry was posted in Android Examples Code and tagged , , , , , . Bookmark the permalink.


Comments are closed.



21,957 views