简体   繁体   中英

How to change a layout in same activity by clicking a Button?

Change a layout in same Activity when click on play button then layout show pause Button at the same place in Android Studio music player app. How it is possible? I'm a beginner so I don't know how to use method about it? I cannot get an answer.

I understand from your question that when your user taps pause button, you should hide pause button and show play button and vice-versa. This is relatively simple to achieve. Just create a layout with a linear layout with two buttons like this:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_gravity="center_horizontal"
    android:padding="5dp">

    <Button
        android:id="@+id/btnPlay"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="2dp"
        android:layout_weight="1"
        android:text="Play"
        android:padding="5dp"/>

    <Button
        android:id="@+id/btnPause"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginBottom="2dp"
        android:layout_marginTop="2dp"
        android:layout_marginRight="5dp"
        android:layout_weight="1"
        android:text="Pause"/>
</LinearLayout>

Now set on click listner to both of the buttons and handle button visibility within that like this :

Button btnPlay = barcodeDialog.findViewById(R.id.btnPlay);
Button btnPause = barcodeDialog.findViewById(R.id.btnPause);

btnPlay.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        btnPlay.setVisibility(View.GONE);
        btnPause.setVisibility(View.VISIBLE);                    
    }
});


btnPause.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        btnPlay.setVisibility(View.VISIBLE);
        btnPause.setVisibility(View.GONE);                   
    }
});

This way you can handle the visibility of your Play and Pause buttons. Reply if you need more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM