0

I've been struggling like crazy to add a button below a ListView of a ListFragment, I've followed most suggestions of the questions How to show a button at the end of an Android ListView, Android Layout with ListView and Buttons and Displaying the button at the end of the ListView with Linear Layout. I just havent tried yet the ListView#addFooterView() yet as I would prefer not to add the button programmatically.

I have the impression that the reason why theirs solutions work for them and not for me is due to the fact that my layout refers to a fragment layout under an activity that has a tab action bar.

As you can see in the picture, the shadow of the linear layout in which my button is part of can be seen overridden by the navigation bar. And the amount overridden resembles the height of the tabs

enter image description here

My fragment layout is the one below:

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

         android:id="@+id/service_notif_list_fragment" 
         >


    <TextView
        android:id="@+id/serviceName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge" />

     <ListView android:id="@id/android:list"
               android:layout_width="match_parent"
               android:layout_height="0dip"
               android:layout_weight="2.0"
               android:drawSelectorOnTop="false"/>

     <TextView android:id="@id/android:empty"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"

               android:text="No data"/>

     <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    style="@android:style/ButtonBar">


      <Button
      android:id="@+id/delServiceButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:layout_gravity="end|left"
      android:text="Delete Service" />
       </LinearLayout>

</LinearLayout>

And my fragment code does not do anything exceptional, except using Loaders to load the list data from the db

public class ServiceSpecifNotListFragment extends ListFragment implements
        LoaderCallbacks<Cursor> {

    Button deleteServiceButton = null;
    String serviceUri;
    String servName;
    TextView serviceName = null;

    private NotificationCursorAdapter adapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

            String[] uiBindFrom = { ...};
            int[] uiBindTo = { ... }; 

            getLoaderManager().initLoader(Constants.SERVICE_SPECIFIC_LIST_LOADER, null, this);
            adapter = new NotificationCursorAdapter(
                    getActivity().getApplicationContext(), R.layout.service_specifc_notif_row,
                    null, uiBindFrom, uiBindTo,
                    CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
            setListAdapter(adapter);

    }

      @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.service_notif_list_fragment, container, false);
            serviceName = (TextView) rootView.findViewById(R.id.serviceName);
            serviceName.setText(servName); // the name is set in the onCreateLoader


           deleteServiceButton = (Button) rootView.findViewById(R.id.delServiceButton);
           deleteServiceButton.setOnClickListener(new View.OnClickListener() {
                 public void onClick(View v) {
                              // TODO
                 }
             });

            return rootView;
        }

    @Override
    public Loader<Cursor> onCreateLoader(int arg0, Bundle bundleArg) {
        String[] projection = { ...};

        Bundle arg = this.getArguments();
            CursorLoader cursorLoader = new CursorLoader(getActivity(),
                    NotificationContentProvider.CONTENT_URI, projection, selection, selectionArgs, NotificationData.SERVER_TIME + " DESC");
            return cursorLoader;
        }
        else{
            Log.d(TAG, "failed to retrieve service name");
            return null;
            //TODO: find a destroy self
        }


    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
        adapter.swapCursor(cursor);

    }

    @Override
    public void onLoaderReset(Loader<Cursor> arg0) {
        adapter.swapCursor(null);

    }

    @Override
    public void onDestroyView(){
        super.onDestroyView();
        Log.d(TAG, "on destroy view");
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        Log.d(TAG, "on destroy");
    }

}

If needed, I can post the code of the main activity, and on...

Community
  • 1
  • 1
Thomas
  • 2,751
  • 5
  • 31
  • 52

2 Answers2

0

Try a RelativeLayout, where you can set the ListView to fill the space between the TextView and LinearLayout containing the button(s):

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

   <TextView
        android:id="@+id/serviceName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_alignParentTop="true" />

    <LinearLayout
        android:id="@+id/layoutButton"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        style="@android:style/ButtonBar">

        <Button
          android:id="@+id/delServiceButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="end|left"
          android:text="Delete Service" />
    </LinearLayout>

    <ListView android:id="@id/android:list"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               android:layout_above="@id/layoutButton"
               android:layout_below="@id/serviceName"
               android:drawSelectorOnTop="false"/>

</RelativeLayout>
CSmith
  • 13,318
  • 3
  • 39
  • 42
  • Sorry for the delay to comment back. I get the same behavior... The grey zone behind the navigation bar leads me to think that it believes that the relative or linear layout under the tab view thinks that there is still more screen space and pushes the button under the navigation bar – Thomas Dec 16 '13 at 15:51
0

Finally what worked here was to set different layout weights for the different elements in the linear layout, but having a weightSum on the top layout as shown below.

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

         android:id="@+id/service_notif_list_fragment" 
         android:weightSum="1.0">


    <TextView
        android:id="@+id/serviceName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge" />

     <ListView android:id="@id/android:list"
               android:layout_width="match_parent"
               android:layout_height="0dip"
               android:layout_weight="0.8"
               android:drawSelectorOnTop="false"/>

     <TextView android:id="@id/android:empty"
               android:layout_width="match_parent"
               android:layout_height="wrap_content"

               android:text="No data"/>

     <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@android:color/transparent"
    style="@android:style/ButtonBar">


      <Button
      android:id="@+id/delServiceButton"
      android:layout_weight="0.05"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:layout_gravity="end|left"
      android:text="Delete Service" />
       </LinearLayout>

</LinearLayout>
Thomas
  • 2,751
  • 5
  • 31
  • 52