Search code examples
androidandroid-layoutandroid-fragmentsandroid-viewandroid-tablelayout

View overlapping each other


I have an activity in which my views (table and headertable) are overlapping

private ViewGroup createTable(ViewGroup root) {
    // TODO Auto-generated method stub
    TableLayout table = new TableLayout(getActivity());
    table.setStretchAllColumns(true);
    table.setShrinkAllColumns(true);

    TableLayout headertable = new TableLayout(getActivity());
    headertable.setStretchAllColumns(true);
    headertable.setShrinkAllColumns(true);

    /* Adding stuff to headertable which contains...  */
            /* ...table content I DO NOT WANT to scroll*/

    root.addView(headertable);

    for (int i = -2; i <= 100; i++) {

                    if (i > 0) {
            /*Set up empty views*/
                /*...3 empty views will be set*/
                    }

        /* Adding stuff to table which contains...  */
                    /* ...table content I WANT to scroll*/

    }

    ScrollView sv = new ScrollView(getActivity());
    sv.addView(table);
    root.addView(sv);
    return root;
}

I basically broke a table into headertable and table. I want to scroll table but not headertable. However, my table(which should be below headertable), is overlapping it. Hence, as you can see above I added empty views(so it starts off below headertable(which has three rows)), but realized this would not work. As soon as I scrolled down, the empty views slide up and my headertable gets obstructed again.

All my views have been made programmatically. This activity is a fragment activity. The XML file contains

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff" >

</FrameLayout>

Any help would be appreciated. Thanks in advance.


Solution

  • I don't know if it's the best way, but here's how I do it:

    main.xml

    <?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" >
    <FrameLayout
        android:id="@+id/pane"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    </LinearLayout>
    

    header_list.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <TextView
                android:id="@+id/header"
                android:layout_width="fill_parent"
                android:layout_height="30dp"
                android:layout_alignParentTop="true" />
        <ListView
                android:id="@id/android:list"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_below="@id/header" />
        <TextView
                android:id="@android:id/empty"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/header"
                android:gravity="center" />
    </RelativeLayout>
    

    RecipeListFragment.java

    public class RecipeListFragment extends ListFragment {
        private TextView header;
        private TextView empty;
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
            View v = inflater.inflate(R.layout.common_list, container, false);
            header = (TextView) v.findViewById(R.id.header);
            empty = (TextView) v.findViewById(android.R.id.empty);
            header.setText(R.string.header_recipe);
            empty.setText(R.string.empty_recipes);
            return v;
        }
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            // List code here
        }
    }
    

    This code only has a single header line, but you can change that TextView to a TableLayout and then fill it the same way, giving you your header table and putting the list underneath it so that it won't scroll over the top of it.

    Hope this helps!