Search code examples
androidandroid-layoutlistviewandroid-fragmentsandroid-listfragment

Specify where ListFragment goes in activity_main.xml


My activity_main.xml is evenly split vertically 3 ways with LinearLayout. I would like to fill the bottom LinearLayout with a ListFragment and populate it with data.

Currently, the ListFragment ignores the 3 LinearLayout and just places itself on top of them.

LstFragment.java

public class LstFragment extends ListFragment{


public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragmentlayout, container, false);
    // Create an array of string to be data source of the ListFragment
    String[] datasource={"English","French","Khmer","Japanese","Russian","Chinese"};
    // Create ArrayAdapter object to wrap the data source
    ArrayAdapter<String> adapter=new ArrayAdapter<String>(getActivity(),R.layout.rowlayout,R.id.txtitem,datasource);
    // Bind adapter to the ListFragment
    setListAdapter(adapter);
    //  Retain the ListFragment instance across Activity re-creation
    setRetainInstance(true);
    return rootView;
}

// Handle Item click event
public void onListItemClick(ListView l, View view, int position, long id){
    ViewGroup viewg=(ViewGroup)view;
    TextView tv=(TextView)viewg.findViewById(R.id.txtitem);
    Toast.makeText(getActivity(), tv.getText().toString(),Toast.LENGTH_LONG).show();

}

}

MainActivity.java

public class MainActivity extends FragmentActivity {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    LstFragment lstfragment=(LstFragment)getSupportFragmentManager().findFragmentByTag("lstfragment");
    if(lstfragment==null){
        lstfragment=new LstFragment();
        FragmentTransaction transact=getSupportFragmentManager().beginTransaction();
        transact.add(android.R.id.content,lstfragment,"lstfragment");
        transact.commit();

    }
}

}

activity_main.xml

<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:id="@+id/topSection">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Testing"
        android:id="@+id/textView3" />
</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:id="@+id/middleSection">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Yo"
        android:id="@+id/textView2"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:id="@+id/bottomSection">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="WhatUP"
        android:id="@+id/textView"
        android:layout_gravity="center_horizontal" />
</LinearLayout>
</LinearLayout>

There are two more simple .xml layout files called fragmentlayout.xml which contains a simple ListView @id/android:list and a rowlayout.xml which has a single TextView @id/textitem.


Solution

  • Reason

    android.R.id.content gives you the root element of a view, without having to know its actual name/type/ID. That's why your Fragment is appearing at the top of everything.

    You are trying to add your Fragment to wrong container here

    transact.add(android.R.id.content,lstfragment,"lstfragment");
    

    Solution

    Add this

    <FrameLayout
        android:layout_width="fill_parent"
        android:id="@+id/fragment_container"
        android:layout_height="fill_parent">
    </FrameLayout>
    

    after 3rd LinearLayout but before closing the root LinerarLayout

    then change to

    transact.add(R.id.fragment_container,lstfragment,"lstfragment");
    

    Also,

    in all of the LinearLayout use this android:layout_height="wrap_content"