thekevshow
thekevshow

Reputation: 784

Android ListFragment Layout

I have this List layout where I am trying to have my loaded list content go between two layouts, so that I can have text in one, on top, and another on bottom with buttons.

Here is the XML code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_list_contrainer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/progress_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:visibility="gone" >

        <ProgressBar
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/rel1_layout_list_view"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:background="@color/Gray" >

        <TextView
            android:id="@+id/text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="TEXTTTTHEREEE"
            android:textColor="@color/White"
            android:textSize="18sp" />

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/list_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/layout_list_view" >

        <TextView
            android:id="@+id/empty_id"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center" />

        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:drawSelectorOnTop="false" >
        </ListView>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/selection_layout_list_view"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:layout_alignParentBottom="true"
        android:background="@color/Gray" >

        <Button
            android:id="@+id/button1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:background="@color/Gray"
            android:text="BUTTON1"
            android:textColor="@color/White" />

        <Button
            android:id="@+id/button2"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:background="@color/Gray"
            android:text="BUTTON2"
            android:textColor="@color/White" />

        <Button
            android:id="@+id/button3"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:background="@color/Gray"
            android:text="BUTTON3"
            android:textColor="@color/White" />
    </RelativeLayout>

</RelativeLayout>

I am having trouble where it is just displaying the list content only. It displays as if the other layouts aren't there. I was looking for advice as to see whether I am doing something wrong, or I just am missing something here.

Here is the inflation code as well:

// Set-up inflater
        _inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

TO THE RESPONSES BELOW, I THANK YOU. It was nothing wrong with xml file, I see that your output is accurate to what I had, had even before. I was foolish and not using my custom fragment properly. The fix I have applied is this.

ANSWER:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        ListView listView = (ListView) view.findViewById(android.R.id.list);
        ViewGroup parent = (ViewGroup) listView.getParent();

        // Remove ListView and add CustomView in its place
        int listViewIndex = parent.indexOfChild(listView);
        parent.removeViewAt(listViewIndex);
        RelativeLayout relLayout = (RelativeLayout) inflater.inflate(
                R.layout.main_list_contrainer_layout, container, false);
        parent.addView(relLayout , listViewIndex,
                listView.getLayoutParams());
        return view;
    }

This has solved my problem. If someone sees a more efficient way of doing this, let me know but from what I read afterwards I think this is how using custom fragment layouts with a list view is suppose to be done.

Thank you again guys

Upvotes: 0

Views: 1916

Answers (3)

thekevshow
thekevshow

Reputation: 784

ANSWER:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        ListView listView = (ListView) view.findViewById(android.R.id.list);
        ViewGroup parent = (ViewGroup) listView.getParent();

        // Remove ListView and add CustomView in its place
        int listViewIndex = parent.indexOfChild(listView);
        parent.removeViewAt(listViewIndex);
        RelativeLayout relLayout = (RelativeLayout) inflater.inflate(
                R.layout.main_list_contrainer_layout, container, false);
        parent.addView(relLayout , listViewIndex,
                listView.getLayoutParams());
        return view;
    }

Upvotes: 0

Piyush
Piyush

Reputation: 18933

Simply use this one:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >

 <LinearLayout
    android:id="@+id/progress_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:visibility="gone" >

    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 </LinearLayout>

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="10" >

    <LinearLayout
        android:id="@+id/rel1_layout_list_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#c0c0c0" >

        <TextView
            android:id="@+id/text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TEXTTTTHEREEE"
            android:layout_gravity="center"
            android:textColor="#fff"
            android:textSize="18sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/list_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8"
        android:orientation="vertical" >

        <!-- <TextView
            android:id="@+id/empty_id"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center" /> -->

        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:drawSelectorOnTop="false" >
        </ListView>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/selection_layout_list_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#c0c0c0"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="#c0c0c0"
            android:text="BUTTON1"
            android:textColor="#fff" />

        <Button
            android:id="@+id/button2"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="#c0c0c0"
            android:text="BUTTON2"
            android:textColor="#fff" />

        <Button
            android:id="@+id/button3"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="#c0c0c0"
            android:text="BUTTON3"
            android:textColor="#fff" />
    </LinearLayout>
   </LinearLayout>

</LinearLayout>

Upvotes: 1

vipul mittal
vipul mittal

Reputation: 17401

You need to set above and below to the layout that contains ListView.

Right now listview is coming in front of other views.

Try this layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_list_contrainer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/progress_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:visibility="gone" >

        <ProgressBar
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/rel1_layout_list_view"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:background="@color/Gray" >

        <TextView
            android:id="@+id/text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="TEXTTTTHEREEE"
            android:textColor="@color/White"
            android:textSize="18sp" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/list_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/selection_layout_list_view"
        android:layout_below="@id/rel1_layout_list_view" >

        <TextView
            android:id="@+id/empty_id"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center" />

        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:drawSelectorOnTop="false" >
        </ListView>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/selection_layout_list_view"
        android:layout_width="match_parent"
        android:layout_height="25dp"
        android:layout_alignParentBottom="true"
        android:background="@color/Gray" >

        <Button
            android:id="@+id/button1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:background="@color/Gray"
            android:text="BUTTON1"
            android:textColor="@color/White" />

        <Button
            android:id="@+id/button2"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:background="@color/Gray"
            android:text="BUTTON2"
            android:textColor="@color/White" />

        <Button
            android:id="@+id/button3"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:background="@color/Gray"
            android:text="BUTTON3"
            android:textColor="@color/White" />
    </RelativeLayout>

</RelativeLayout>

OUTPUT:

enter image description here

Upvotes: 1

Related Questions