Home » Android » android – Last Item on Listview is cut off

android – Last Item on Listview is cut off

Posted by: admin June 15, 2020 Leave a comment

Questions:

So in my Android app I have where the main activity has tabs and in each tab there is a fragment containing a listview. But for some reason the last item in the listview always gets cut off. I have looked around for solutions but I havent found any yet. Any suggestions?

Main Activity layout

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            app:tabGravity="fill"/>


    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"  />
</android.support.design.widget.CoordinatorLayout>

Fragment with ListView

<?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"
    android:orientation="vertical">

    <ProgressBar
        android:id="@+id/progressBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        style="@style/Widget.AppCompat.ProgressBar" />

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

        <ListView
            android:id="@+id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            style="@style/Widget.AppCompat.ProgressBar"></ListView>

    </LinearLayout>
</RelativeLayout>

List Item Layout

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/title"
        android:textSize="20sp"
        android:textStyle="bold"
        android:typeface="normal"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/author"
        android:textSize="15sp"
        android:typeface="normal"
        android:layout_below="@+id/publishdate"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/publishdate"
        android:textSize="15sp"
        android:typeface="normal"
        android:layout_below="@+id/title"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</LinearLayout>

List Adapter Class

package com.czhou.dailyprincetoniannewspaper.adapters;

import android.content.ClipData;
import android.content.Context;
import android.graphics.Paint;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;


import com.czhou.dailyprincetoniannewspaper.NewspaperMetaObject;
import com.czhou.dailyprincetoniannewspaper.R;

import java.util.List;

public class NewsListAdapter extends ArrayAdapter<NewspaperMetaObject> {

    static class ViewHolder {
        TextView author;
        TextView publishdate;
        TextView title;
    }

    private LayoutInflater inflater;
    List<NewspaperMetaObject> newsitems;
    public NewsListAdapter(Context context, List<NewspaperMetaObject> items) {
        super(context, R.layout.newslistitem, items);
        this.newsitems = items;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

             ViewHolder viewHolder;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.newslistitem, parent, false);

            viewHolder = new ViewHolder();
            viewHolder.title = (TextView) convertView.findViewById(R.id.title);
            viewHolder.author = (TextView) convertView.findViewById(R.id.author);
            viewHolder.publishdate = (TextView) convertView.findViewById(R.id.publishdate);
            convertView.setTag(viewHolder);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.title.setText(newsitems.get(position).getArticleTitle());
        viewHolder.title.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
        viewHolder.publishdate.setText(newsitems.get(position).getArticlePublishDate());
        viewHolder.author.setText(newsitems.get(position).getArticleAuthor());
        System.out.println(viewHolder.publishdate.getText());

        return convertView;
    }

}

Main Activity Class

package com.czhou.dailyprincetoniannewspaper;

import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import java.util.ArrayList;
import java.util.List;
/**
 * Created by czhou on 11/21/2015.
 */
public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private ProgressBar mProgressBar;
    private CoordinatorLayout coordinatorLayout;

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

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void setupViewPager(ViewPager viewPager){
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new PUNewsFragment(), "News");
        adapter.addFragment(new PUSportsFragment(), "Sports");

        viewPager.setAdapter(adapter);
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }
}
How to&Answers:

Use RecyclerView instead of ListView. It will work for sure. Scrolling of the toolbar strips the bottom space of your layout. ListView won’t work perfectly with app:layout_behavior="@string/appbar_scrolling_view_behavior". If you want scrolling and view full items ,use RecyclerView or NestedScrollView instead of ListView.

If you remove app:layout_behavior="@string/appbar_scrolling_view_behavior" and

app:layout_scrollFlags="scroll|enterAlways"  

of toolbar you can see ListView with full items.

So, use RecyclerView instead of ListView with app:layout_behavior="@string/appbar_scrolling_view_behavior" and app:layout_scrollFlags="scroll|enterAlways". It will works with scrolling and layout behavior.

Answer:

Put in a line like android:paddingBottom="20dp" into your ListView item in your Fragment XML file. Adjust the 20dp value until it looks the way you want it to.

For some strange reason it just fixes the problem, and it doesn’t have to do with the size of the ListView being changed. Somehow the small padding forces the ListView to layout in a different way.

Answer:

Adding below line in your RecyclerView should solve the issue.

android:layout_marginBottom="?attr/actionBarSize"

Answer:

For anyone still having this issue, removing the action bar worked for me.

Inside your AndroidManifest:

android:theme="@style/AppTheme.NoActionBar">

Also delete the Action Bar from the layout XML for the Activity.

And remove this from your Activity class:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);