Search code examples
androidandroid-layoutaudio-playerandroid-music-player

how to create buttons which can be acciable in multiple activites in android?


I am creating a music player app the below are list of activities:

  • WelcomeActivity
  • SongsListActivity
  • MusicPlayActivity
  • SongsListActivity will display list of songs available, on click of item it will take you to MusicPlayActivity and which will trigger SongService which in turn play song in background.

The below is MusicPlayActivity

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"  android:layout_height="match_parent"
    android:background="@drawable/background">

    <include layout="@layout/musicpanel"></include>

</RelativeLayout>

The below code is for musicpanel which is included in MusicPlayActivity

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"

android:orientation="horizontal">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Play"
    android:id="@+id/playPause"
    android:layout_gravity="bottom"
    android:layout_marginLeft="51dp"
    android:layout_marginStart="51dp"
    android:layout_alignTop="@+id/stop"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Stop"
    android:id="@+id/stop"
    android:layout_gravity="bottom"
    android:layout_marginBottom="28dp"
    android:layout_alignParentBottom="true"
    android:layout_toRightOf="@+id/playPause"
    android:layout_toEndOf="@+id/playPause"
    android:layout_marginLeft="67dp"
    android:layout_marginStart="67dp" />
</RelativeLayout>

If the user click back button from MusicPlayActivity we need to display SongListActivity so here I want to display musicpanel.

My question is what is the best practice to create musicpanel?

Right now I am including it in xml but on each activity I need to set listeners for all components of musicpanel (like play, pause, next, previous) every time

Thanks in advance


Solution

  • You have several options:

    1. Create a reusable widget. Make it use your player layout, setup listeners, etc. Then add it everywhere you need.
    2. Move your player to a notification. It would be accessible from any place in your app. Notifications are quite simple and have to be in the notification area, but you can mix this solution with 1.
    3. Move to fragments or views. Your player could be placed above main layout using FrameLayout or RelativeLayout. This is my preffered solution.
    4. Move your player to a floating window. It's how the floating Facebook message's head is done. Tricky to do, but nice when done properly.

    Usually it's done in 1/2 or 2/3 way as these combinations reduce code duplication and are quite easy to do.