how to create buttons which can be acciable in multiple activites in android?
Matthew Barrera
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="" 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=""
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
12 Answers
You have several options:
- Create a reusable widget. Make it use your player layout, setup listeners, etc. Then add it everywhere you need.
- 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.
- Move to fragments or views. Your player could be placed above main layout using FrameLayout or RelativeLayout. This is my preffered solution.
- 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.
I think its best for you.Using PageViwer .
1:make 2 fragments. (1)firstActivity (2)SecondActivity
2:Use PageViewer in your xml layout.
<LinearLayout xmlns:android=""
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2">
<LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1.8" android:orientation="vertical"> <android.support.v4.view.ViewPager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v4.view.ViewPager>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight=".2" android:background="@android:color/black" android:weightSum="3"> <ImageView android:id="@+id/btn1" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:src="@android:drawable/ic_media_play" /> <ImageView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:src="@android:drawable/ic_media_pause" /> <ImageView android:id="@+id/windowbtn" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:src="@android:drawable/ic_menu_revert" />
</LinearLayout>Here is viewPager Instance in your Main Activity.
viewPager = (ViewPager) findViewById(R.id.pager); // viewPager.setPageTransformer(true, new VerticalPageTransformer()); mAdapter = new MultiPagerAdapter(getSupportFragmentManager()); viewPager.setAdapter(mAdapter); viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageSelected(int position) { } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { } @Override public void onPageScrollStateChanged(int arg0) { } }); viewPager.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { viewPager.getParent().requestDisallowInterceptTouchEvent(true); return false; } });
}and here is Multipager adapter
public class MultiPagerAdapter extends FragmentPagerAdapter {
public MultiPagerAdapter(FragmentManager fm) { super(fm);
}
@Override
public Fragment getItem(int index) { switch (index) { case 0: // Top Rated fragment activity return new FirstFragment(); case 1: // Games fragment activity return new SecFragment(); } return null;
}
@Override
public int getCount() { // get item count - equal to number of tabs return 2;
}}