Reputation: 51
I have created two tables, one on the left side and one in the center of the screen. When you start the application the table on the left is set to visible and the table in the center is set to invisible.
Now I want to click a button in the left table (Buton_left) and the entire table in the center would become visible.
I have this method so far but it doesn't seem to be working. Any help or suggestion would be appreciated. Let me know if you need any more information.
package com.example.musicapp;
import android.os.Bundle;
public class Tbl_Show_Hide extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TableLayout table_left = (TableLayout)findViewById(R.id.table_left);
TableLayout table_center = (TableLayout)findViewById(R.id.table_center);
Button Buton_left = (Button)findViewById(R.id.Buton_left);
table_left.setOnClickListener(this);
table_center.setOnClickListener(this);
Buton_left.setOnClickListener(this);
}
@Override
public void onClick(View v) {
boolean visible = true;
int targetId = v.getId();
if(targetId == R.id.Buton_left)
{
if(visible)
{
if(table_center.getVisibility() == View.INVISIBLE)
{
table_center.setVisibility(View.VISIBLE);
}
}
}
}
Upvotes: 0
Views: 2064
Reputation: 14271
The View#getVisibility may return one of VISIBLE, INVISIBLE, or GONE. And you create table_center in onCreate method, that is a local variable, invisible to onClick.
So change it to:
public class Tbl_Show_Hide extends Activity implements OnClickListener {
final TableLayout table_left = (TableLayout)findViewById(R.id.table_left);
final TableLayout table_center = (TableLayout)findViewById(R.id.table_center);
final Button Buton_left = (Button)findViewById(R.id.Buton_left);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
table_left.setOnClickListener(this);
table_center.setOnClickListener(this);
Buton_left.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int targetId = v.getId();
if(targetId == R.id.Buton_left)
{
if(table_center.getVisibility() != View.VISIBLE)
{
table_center.setVisibility(View.VISIBLE);
}
}
}
If the problem is still there, pls post your manifest.
Upvotes: 2