Not sure it is the good way for it, but you can try with a FrameLayout
to host a Fragment
and make the FrameLayout
with a background transparent (if you don't know what is FrameLayout, in few words it serves to overlap views. I writed an answer on this). The layout of your Parent Activity
(FragmentActivity) may be something like this:
<FrameLayout
android:id="@+id/FragmentContainer"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@android:color/transparent" >
<TextView
android:layout_width="match_parent" android:layout_height="match_parent"
android:text="I'm a text of the content" />
</FrameLayout>
And the Fragment
may be as below:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="match_parent" android:layout_height="250dip"
android:background="@color/blue"
android:layout_alignParentTop="true" >
<TextView
android:id="@+id/ReaderEdit"
android:layout_width="match_parent" android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="I'm a text in a fragment" >
</LinearLayout>
</RelativeLayout>
And then when you initialise your Fragment
in the FrameLayout
, you set hide() and show() methods for display it. For this, use FragmentSupportManager
in your FragmentActivity
and add() or replace() methods like the example below:
// Initialize your fragment:
FragmentTransaction mTransaction = this.getSupportFragmentManager().beginTransaction();
Fragment mFrag = new MyFragment(); // call your Fragment Class
mTransaction.add(R.id.FragmentContainer, mFrag, null).hide(mFrag).commit(); // add to the Container (FrameLayout) and hide it
// don't forget to commit at the end of every FragmentTransaction
// To display your fragment:
FragmentTransaction mTranDisplay = this.getSupportFragmentManager().beginTransaction();
mTranDisplay.show(mFrag).addToBackStack(null).commit(); // pass the Fragment to show + add the BackStack method (when the user press back button, the Fragment disappears without quit the Activity)
mTranDisplay.setCustomAnimations(R.anim.slide_in, 0, 0, R.anim.slide_out); // you can make an animation to the BackStack method, here it's a slide in/out from top to bottom
I think there is a proper way to do this. Maybe you can check this library and see, break, rebuild,... the code.
Hope this helps.