Ashuthosh Patoa
Ashuthosh Patoa

Reputation: 314

Adding fragments to display a full screen layout

I want to use fragments to display the Now playing screen whenever a user clicks on a song.I am having a main_activity layout that displays the list of songs and a item click listener.Now i want to open another layout which completely occupies the screen showing the current song that is being played.I think i can achieve this using a fragment but can't find a way to implement it.

Here is my main_activity xml file

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"/>``
</RelativeLayout>

Now i want to replace this entire layout with now playing layout

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:theme="@style/AppTheme.NoActionBar">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="320dp"
    android:fitsSystemWindows="true"
    android:id="@+id/image"/>

Upvotes: 0

Views: 164

Answers (1)

Janak Suthar
Janak Suthar

Reputation: 383

your main_activity.xml should be like this :

  <FrameLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_frame"

android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
  >

<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:fitsSystemWindows="true"
 tools:context=".MainActivity"
 tools:showIn="@layout/activity_main">

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"/>``
</RelativeLayout>

</FrameLayout>

on button click you have perform this code :

  Fragment fragment = new NowPlayingFragment();


 ((FragmentActivity) view.getContext()).getFragmentManager().beginTransaction()
                .replace(R.id.main_frame, fragment).addToBackStack(null).commit();

so this will replace a fragment on your current activity

EDIT: Here is my fragment class

public class NowPlayingFragment extends android.support.v4.app.Fragment {

public NowPlayingFragment() {
}
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_playing, container, false);
}

Upvotes: 1

Related Questions