Reputation: 16813
I am trying to design the activity layout based on different criteria. I have created the layout on the xml and as a default it shows two buttons(ask, buy) on the activity.
However, in some circumstances, I do not want to show these two buttons, only show a single button which none of them below, totally different button and different action. How could I make it happen?
In other words, I would like to make my new button to stay in middle of buy and ask buttons. with the same size. How should I make it happen ? Instead of two buttons, side by side (buy and ask), now there is only one button (delete) in the center.
<Button
android:id="@+id/btnAsk"
android:textSize="16sp"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="ask"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:textStyle="bold"
android:background="@drawable/button_style"
android:textColor="@color/white"/>
<Button
android:id="@+id/btnBuy"
android:layout_width="0dip"
android:layout_weight="1"
android:textSize="16sp"
android:layout_height="wrap_content"
android:text="buy"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:textStyle="bold"
android:background="@drawable/button_style"
android:textColor="@color/white"
/>
Upvotes: 1
Views: 1399
Reputation: 39
Try the below code -
Button buttonname=(Button)findViewById(R.id.butBuy);
buttonname.setVisibility(View.INVISIBLE);
or -
buttonname.setVisibility(View.GONE);
show it again with -
buttonname.setVisibility(View.VISIBLE);
Upvotes: 0
Reputation: 4787
Create a layout(say userActionLayout) and add your buttons to it,hide this layout when you want to hide these buttons using setVisibility(View.Gone)
Add another button outside this layout(userActionLayout)` and define it differently.Set this Button visible in code when you hide the other layout.
For Example:
<LinearLayout
android:id="@+id/userActionLayout"
android:height="wrap_content"
android:width="fill_parent"
android:orientation="vertical">
<Button
android:id="@+id/btnAsk"
android:textSize="16sp"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="ask"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:textStyle="bold"
android:background="@drawable/button_style"
android:textColor="@color/white"/>
<Button
android:id="@+id/btnBuy"
android:layout_width="0dip"
android:layout_weight="1"
android:textSize="16sp"
android:layout_height="wrap_content"
android:text="buy"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:textStyle="bold"
android:background="@drawable/button_style"
android:textColor="@color/white"
/>
<LinearLayout/>
<Button
android:id="@+id/btnOther"
android:height="wrap_content"
android:width="fill_parent"
android:visibility="gone"
/>
Upvotes: 1
Reputation: 669
Use setVisibility(View.GONE)
in Button object when you want to hide that buttons:
Button btnAsk = (Button)findViewById(R.id.btnAsk);
Button btnDel = (Button)findViewById(R.id.btnDel);
Button btnBuy = (Button)findViewById(R.id.btnBuy);
/* if you want to show only Delete button.. */
btnAsk.setVisibility(View.INVISIBLE);
btnDel.setVisibility(View.VISIBLE);
btnBuy.setVisibility(View.INVISIBLE);
/* if you want to show Ask and Buy buttons.. */
btnAsk.setVisibility(View.VISIBLE);
btnDel.setVisibility(View.INVISIBLE);
btnBuy.setVisibility(View.VISIBLE);
View.INVISIBLE
attribute will be make your buttons are not shown, but their position is kept.
But View.GONE
attribute will hide your buttons completely.
Take care of it.
If you want to set three buttons, edit your xml code like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<Button
android:id="@+id/btnAsk"
android:textSize="16sp"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="ask"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:textStyle="bold"
android:background="@drawable/button_style"
android:textColor="@color/white"/>
<Button
android:id="@+id/btnDel"
android:textSize="16sp"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Delete"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:textStyle="bold"
android:background="@drawable/button_style"
android:textColor="@color/white"/>
<Button
android:id="@+id/btnBuy"
android:layout_width="0dip"
android:layout_weight="1"
android:textSize="16sp"
android:layout_height="wrap_content"
android:text="buy"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:textStyle="bold"
android:background="@drawable/button_style"
android:textColor="@color/white"
/>
</LinearLayout>
Upvotes: 1
Reputation: 1903
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:oriantation="horizontal" >
<Button
android:id="@+id/btnAsk"
android:textSize="16sp"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="ask"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:textStyle="bold"
android:background="@drawable/button_style"
android:textColor="@color/white"/>
<Button
android:id="@+id/btnDelete"
android:layout_width="match_parent"
android:gravity="center"
android:textSize="16sp"
android:layout_height="wrap_content"
android:text="delete"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:textStyle="bold"
android:background="@drawable/button_style"
android:textColor="@color/white"
android:visibility="gone"
/>
<Button
android:id="@+id/btnBuy"
android:layout_width="0dip"
android:layout_weight="1"
android:textSize="16sp"
android:layout_height="wrap_content"
android:text="buy"
android:layout_marginLeft="5dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:textStyle="bold"
android:background="@drawable/button_style"
android:textColor="@color/white"
/>
</LinearLayout>
on some condition
delete.setVisibility(View.Visible);
buy.setVisibility(View.Gone);
action.setVisibility(View.Gone);
Upvotes: 1
Reputation: 2623
Use all your button in XML itself.But make it visible / invisible in your java file ie Activity.In onCreate if you don't want to show make it as an INVISIBLE
Button buyBut=(Button)findViewById(R.id.butBuy);
buyBut.setVisibility(View.GONE);
And make it as an VISIBLE where you need to show it by using the following..
buyBut.setVisibility(View.VISIBLE);
Upvotes: 1
Reputation: 10037
To hide button, Use ONE
or INVISIBLE
Button btnName = (Button)findViewById(R.id.btnName);
btnName.setVisibility(View.GONE);
To visible button
btnName.setVisibility(View.VISIBLE);
In xml use android:gravity="center"
or
android:centerInParent="true"
Upvotes: 1