Reputation: 1680
I am trying to hide a Fragment when my activity starts, my layout is as follows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="#eee"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.TypeFragment"
android:id="@+id/type_fragment"
android:layout_width="184dp"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:text="@string/type"
/>
<fragment android:name="com.ModeFragment"
android:id="@+id/mode_fragment"
android:layout_below="@id/type_fragment"
android:layout_width="184dp"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:text="@string/mode"
/>
<fragment
android:id="@+id/canvas_fragment"
android:name="com.CanvasFragment"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/type_fragment" />
<fragment android:name="com.NumericsInputFragment"
android:id="@+id/numeric_area"
android:layout_below="@id/mode_fragment"
android:layout_width="184dp"
android:layout_height="wrap_content" />
</RelativeLayout>
My MainActivity extends FragmentActivity and has the following onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_canvas);
Log.d("ACTIVITY" , "linside");
// Check which layout we are using
if(findViewById(R.id.fragment_container) != null) {
/* if it is restored from a previous state - simply return */
if (savedInstanceState != null) {
return;
}
/* Create a TypeFragment */
TypeFragment firstFragment = new TypeFragment();
//firstFragment.getView().setBackgroundColor(Color.GRAY);
ModeFragment secondFragment = new ModeFragment();
/* This is the numerics Fragement */
NumericsInputFragment numericsFragment = new NumericsInputFragment();
/* Pass the Intents Extras to the Fragment */
firstFragment.setArguments(getIntent().getExtras());
secondFragment.setArguments(getIntent().getExtras());
/* Add the fragment to the fragment container */
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment, null)
.add(R.id.fragment_container, secondFragment, null)
.add(R.id.fragment_container, numericsFragment, null)
.commit();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.hide(getFragmentManager().findFragmentById(R.id.numeric_area));
transaction.commit();
}
}
Nothing I do seems to make a difference to my initial layout. I.e if I remove all of the .add() statements I still get the same layout. Obviously I am doing something very wrong here, can someone shed some light on what I might be doing incorrectly ?
EDIT
I have replaced the layout with an updated version, replacing the dynamic Fragment with a FrameLayout like so :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:background="#eee"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.TypeFragment"
android:id="@+id/type_fragment"
android:layout_width="184dp"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:text="@string/type"
/>
<fragment android:name="com.ModeFragment"
android:id="@+id/mode_fragment"
android:layout_below="@id/type_fragment"
android:layout_width="184dp"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:text="@string/mode"
/>
<fragment
android:id="@+id/canvas_fragment"
android:name="com.CanvasFragment"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/type_fragment" />
<FrameLayout
android:id="@+id/numeric_area"
android:layout_width="184dp"
android:layout_height="match_parent"
android:layout_below="@id/mode_fragment"
android:background="?android:attr/detailsElementBackground"
/>
</RelativeLayout>
I can then control if this is displayed as described here http://developer.android.com/guide/components/fragments.html
Upvotes: 0
Views: 204
Reputation: 420
You cannot add/replace/hide fragments that you define statically in layout files. You can use Framelayout and replace it with fragment instances using FragmentManager/FragmentTransaction. Check out this link
Upvotes: 2