0

Hi

I am new in android. I have many Buttons in view in android and i want to show them part by part by clicking another button.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Vaseph
  • 704
  • 1
  • 8
  • 20

5 Answers5

2

Set button visibility to GONE (button will be completely "removed" -- the buttons space will be available for another widgets) or INVISIBLE (button will became "transparent" -- its space will not be available for another widgets):

View b = findViewById(R.id.button);
b.setVisibility(View.GONE);

or in xml:

<Button ... android:visibility="gone"/>

Ref By: LINK

Community
  • 1
  • 1
AndyBoy
  • 564
  • 6
  • 29
  • i did this code. next = (Button)findViewById(R.id.next); next.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(i – Vaseph Feb 13 '14 at 08:18
1

use android:visibility="visible" or android:visibility="gone" or android:visibility="invisible"

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginTop="91dp"
    android:layout_toLeftOf="@+id/textView1"
    android:text="Button"
    android:visibility="visible" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/button1"
    android:layout_alignLeft="@+id/textView1"
    android:text="Button"
    android:visibility="invisible" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button2"
    android:layout_alignBottom="@+id/button2"
    android:layout_toRightOf="@+id/button2"
    android:text="Button"
    android:visibility="invisible" />
Looking Forward
  • 3,579
  • 8
  • 45
  • 65
1

let say you have 2 buttons : in xml write this:Make button 2 invisible like below

 <Button android:id="@+id/btn1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Button 1"/>

<Button android:id="@+id/btn2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="button 2"
    android:layout_below="@+id/btn1"
    android:visibility="invisible"/>

In your oncreate method write this: when you click on button1 , button2 will appear.

     Button btn1 = (Button)findViewById(R.id.btn1);
    final Button btn2 = (Button)findViewById(R.id.btn2);

    btn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            btn2.setVisibility(View.VISIBLE);

        }
    });
Brinda K
  • 745
  • 3
  • 16
  • 34
1
<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginTop="91dp"
    android:layout_toLeftOf="@+id/textView1"
    android:text="Button"
    android:visibility="gone" />


<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/button1"
    android:layout_alignLeft="@+id/textView1"
    android:text="Button"
    android:visibility="gone" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/button2"
    android:layout_alignBottom="@+id/button2"
    android:layout_toRightOf="@+id/button2"
    android:text="Button"
    android:visibility="gone" />
  1. First make the above xml layout having all your buttons and set them to GONE because the buttons will not occupy space in the layout by default so saves you space and time at initialization.
  2. Now in your code on click event just set the visibility of these buttons to VISIBLE.

    Steps for changing the visibility HashMap map=new HashMap(); map.put(R.id.button1,new Integer[]{R.id.button2,R.id.button3,R.id.button4});

public void onClick(View v){

Integer[] buttonsToShow=map.get(R.id.v.getId());

if(buttonsToShow!=null) for(int button:buttonsToShow){

findViewById(button).setVisibility(View.VISIBLE);

} }

cafebabe1991
  • 4,928
  • 2
  • 34
  • 42
0

I wrote following code and solved my problem. maybe it might be useful to anyone next = (Button)findViewById(R.id.next); next.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) 
        {
            if(i<movie_list.length-3)
            {
                movie_list[i].setVisibility(View.GONE);
                movie_list[i+3].setVisibility(View.VISIBLE);
                i++;
            }else{
                Toast.makeText(getApplicationContext(), "That is the end of the buttons ", Toast.LENGTH_SHORT).show();
            }
        }
    });
Vaseph
  • 704
  • 1
  • 8
  • 20