Reputation: 334
I have a CountDownTimer that is activated by a "Set" button and then can be stopped by a "Cancel" button.
When the activity starts, I want only the set button to show. When the countdown is started, I want to hide the "Set" button and show the "Cancel" button.
I want to do this by changing the weight of the button. How do I do this? I have found similar questions that have answers that don't work when I try them. I think those answers are just fragments of the full code, which is what I'm looking for.
Here is the XML:
<LinearLayout
android:id="@+id/buttonsContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" >
<Button
android:id="@+id/setDelay"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:onClick="activateDelay"
android:text="Set Delay" />
<Button
android:id="@+id/cancelDelay"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:onClick="cancelDelay"
android:text="Cancel Delay" />
</LinearLayout>
I know I'm using hard-coded strings, they're just for development purposes.
Upvotes: 0
Views: 896
Reputation: 15334
Why do you want to use the weight property to hide a button? layout_weight
is used to share space proportionately (according to the weights given). I guess you could "hide" a button by setting the layout_weight
to 0 (and the weight on the other to something greater than 0). I've never updated the layout parameters after adding the views to viewgroup, but parent.updateViewLayout(childView, updatedParams)
looks promising.
In any case, use setVisibility(View.GONE)
on the button you want gone; it is hidden and the rest of the Views are laid out as though the button is not present. (You can use View.INVISIBLE
to hide the View, but the rest of the Views are laid out as though the button is there - you just can't see it.)
Here's an untested "fragment" ;P
private Button set;
private Button cancel;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// it's typical to use underscore notation for IDs - R.id.set_delay
set = (Button) findViewById(R.id.setDelay);
cancel = (Button) findViewById(R.id.cancelDelay);
}
public void activateDelay(View button) {
set.setVisibility(View.GONE);
cancel.setVisibility(View.VISIBLE);
}
public void cancelDelay(View button) {
set.setVisibility(View.VISIBLE);
cancel.setVisibility(View.GONE);
}
And in your XML, you'd start with the "Set" button visible (by default), and "Cancel" gone, both with match_parent
for width:
<LinearLayout
android:id="@+id/buttonsContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
<Button
android:id="@+id/setDelay"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:onClick="activateDelay"
android:text="Set Delay" />
<Button
android:id="@+id/cancelDelay"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:visibility="gone"
android:onClick="cancelDelay"
android:text="Cancel Delay" />
</LinearLayout>
Upvotes: 3