Home » Android » android – How can I hide/show an element when a button is pressed?

android – How can I hide/show an element when a button is pressed?

Posted by: admin May 14, 2020 Leave a comment


I’m trying to learn how to develop Android using the Eclipse IDE. What I’m trying to do right now is make a hidden TableLayout visible when a button is pressed. However, I have no idea as to what I need to put in the button’s OnClick property.

Also, are there any tutorials online that could help me learn how to develop Android apps in Eclipse?


How to&Answers:

well just take the reference of the TableLayout by using findViewById(int) in the onClickListener(). once you have the object of TableLayout, call setVisibility(View.VISIBLE)


TableLayout tl = (TableLayout)findeViewById(R.id.yourtablelayout);


Something like that within your onClick() method should do the trick.



TableLayout table;
Button button;
table = (TableLayout) findViewById (R.id.tablelayout1);
button = (Button) findViewById(R.id.button1);

button.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {

        // View list = (View)findViewById(R.id.myviewId); 


Hope this works.


Try this in MainActivity class :

  TextView textview;

/* onClick method of show button */

  public void show(View view){


/* onClick method of hide button */

public void hide(View view){

and try this in onCreate method :

    textview = (TextView) findViewById(R.id.textview);