Fragment not showing up and breaks other buttons on Layout

Posted by Devin Crane on Stack Overflow See other posts from Stack Overflow or by Devin Crane
Published on 2014-05-22T16:05:21Z Indexed on 2014/05/27 15:26 UTC
Read the original article Hit count: 427

I'm learning how to use Fragments, and trying to add a fragment tag_button.xml at runtime to a FrameLayout tagFragmentContainer deep within another layout, deepLayout.xml. I get no errors, but the fragment doesn't show up.

When I make its container visible, I can see a small sliver of layout between the other elements already existing, but then it disappears after onCreateView(), and all the remaining buttons are broken, which is even more confusing to me.

tag_button.xml is just a regular layout file with some text and a button.

tagFragmentContainer, within a LinearLayout in the middle of a large layout file, deepLayout.xml:

<LinearLayout
  android:id="@+id/tagButtonsLayout"
  android:layout_marginBottom="10dip"
  android:visibility="gone"
  style="@style/Form">
    <FrameLayout
       android:id="@+id/tagFragmentContainer"
       android:layout_marginBottom="10dip"
       style="@style/Form">
    </FrameLayout>
</LinearLayout>

In deepLayoutActivity, I make visible tagButtonsLayout and start TagButtonActivity:

final LinearLayout anotherlm = (LinearLayout) findViewById(R.id.tagButtonsLayout);
anotherlm.setVisibility(View.VISIBLE);

Intent i = new Intent (this, TagButtonActivity.class);
startActivity(i);

TagButtonActivity is as follows:

public class TagButtonActivity extends FragmentActivity
{

  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.action_expense);

    if (savedInstanceState != null)
         return;

    TagButtonFragment firstFragment = new TagButtonFragment();
    getSupportFragmentManager().beginTransaction().add(R.id.tagFragmentContainer, 
                                firstFragment, "tagOne").commit();
  }
}

TagButtonFragment:

public class TagButtonFragment extends Fragment
{
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle 
                            savedInstanceState)
   {
      // Inflate the layout for this fragment
      return inflater.inflate(R.layout.tag_button, container, false);
   }
}

tag_button.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  style="@style/Form">
  <TextView
    android:id="@+id/tag_button_header"
    style="@style/FieldHeader"
    android:text="example text"/>
    <RelativeLayout
      android:id="@+id/tag_button_block"
      android:layout_width="match_parent"
      android:layout_marginLeft="2dip"
      android:layout_marginTop="3dip"
      android:layout_marginBottom="3dip"
      android:layout_marginRight="2dip"
      android:layout_height="43dip"
      android:clickable="true"
      android:background="@drawable/row_spinner_selector">

      <ImageView
        android:id="@+id/question_arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:layout_marginRight="10dip"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/arrow_right"/>

      <TextView
        android:id="@+id/tag_question_text"
        android:layout_toLeftOf="@id/question_arrow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dip"
        android:layout_centerVertical="true"
        android:textColor="@android:color/black"
        android:singleLine="true"
        android:ellipsize="end"
        android:textSize="15sp"
        android:text="@string/not_selected"/>
    </RelativeLayout>
</LinearLayout>

I would certainly appreciate any help in figuring this out from someone who knows fragments better than me!

Thanks!

© Stack Overflow or respective owner

Related posts about android

Related posts about android-fragments