Reputation: 485
A bit of a general question that I can't seem to find and answer to.
Say I have a start button in a linear layout in my activity. When the user presses start, I want it to be replaced with a different button (not just changing the label) that says stop. On a website I'd just hide one button and show the other (or fade one out and at zero opacity hide it and show the other and fade it in).
How is this usually done in Android? I read a lot of different suggestions like Fragments (for one button?), or relative layouts or whatever, but I just want to replace a view with another view of the same size, and possibly animate it later as well.
Upvotes: 0
Views: 79
Reputation: 3966
I think this can help you.
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button_1 = (Button) findViewById(R.id.button1);
final Button button_2 = (Button) findViewById(R.id.button2);
button_2.setVisibility(View.INVISIBLE);
button_1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
button_1.setVisibility(View.INVISIBLE);
button_2.setVisibility(View.VISIBLE);
}
});
button_2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
button_2.setVisibility(View.INVISIBLE);
button_1.setVisibility(View.VISIBLE);
}
});
}
If you want to create your button 2 dynamically then you can use
RelativeLayout layout = (RelativeLayout) findViewById(R.id.RelativeLayout1);
Button button_2= new Button(getApplicationContext());
button_2.setText("button 2");
button_1.setVisibility(View.INVISIBLE);
layout.addView(button_2, button_1.getLayoutParams());
instead of
final Button button_2 = (Button) findViewById(R.id.button2);
Hope it will help you if you want layout then that also i can post.
Upvotes: 1
Reputation: 328
You can manage this with two components (ex. Button and Button) in one layout. One will have parameter visibility="gone" and one visibility="visible". So you will just change this value in code.
btnFirst.setVisibility(View.VISIBLE);
btnSecond.setVisibility(View.GONE);
Upvotes: 1
Reputation: 97
To hide one view simply call setVisibility(View.GONE) which will make the view as if it was never in your layout. To show the view call setVisiblity(View.VISIBLE).
Upvotes: 1
Reputation: 3028
One thing you can do is overlay the buttons, for instance using a Relativelayout, and making sure the buttons align perfectly (e.g. by setting android:layout_alignLeft
, top, bottom and right). Then, you set the visibility of one of them to VISIBLE
, and the others to INVISIBLE
(view is invisible, but it still takes up space for layout purposes) or GONE
(view is invisible, and it doesn't take any space for layout purposes).
When the user clicks one button, you then change the visibilities. Here you can even apply any animations you want, such as the fade in/out you mention.
However, it seems you just need to change the label of the button, and handle it based on it's state that you can maintain in your activity. E.g. first it's a start button, then a stop. This is much easier to program, and faster.
Upvotes: 1