0

I am making phonebook, it works, but if screen resolution is smaller than content height, not all items of ListView shows, they left behind: (see last item)

enter image description here enter image description here

But if content wrap to screen height, it shows good:

enter image description here

How can i fix this?

My codes:

CardView Layout

<?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="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    >
    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/phone_cv"
        app:cardElevation="3dp"
        app:contentPadding="10dp"
        app:cardCornerRadius="3dp"
        android:backgroundTint="#ffffff"
        android:layout_marginBottom="10dp"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/phone_cv_person_name"
                android:text="organisation name"
                android:textSize="18sp"
                android:textColor="#000000"
                />
            <View
                android:layout_height="1dp"
                android:layout_width="match_parent"
                android:background="#000000"
                android:layout_marginTop="4dp"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/phone_cv_person_doljnost"
                android:text="Limpus Dda Lorem"
                android:visibility="gone"
                />
            <ListView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/phone_cv_list"
                android:scrollbars="none"
                />
        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>

My (custom) List View Layout:

<?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">
    <TextView
        android:id="@+id/phone_persons_list_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:text="Name"
        android:textSize="14sp"
        android:paddingStart="10dp"
        android:paddingEnd="10dp"
        android:paddingTop="1dp"
        android:paddingBottom="0dp"
        android:textColor="#000"
        />
    <TextView
        android:id="@+id/phone_persons_list_otdel"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="Otdel"
        android:paddingStart="10dp"
        android:paddingEnd="10dp"
        android:paddingTop="1dp"
        android:paddingBottom="0dp"
        android:visibility="gone"
        android:textColor="#CC000000"
        android:textSize="12sp"
        />
    <TextView
        android:id="@+id/phone_persons_list_doljnost"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="Doljnost"
        android:paddingStart="10dp"
        android:paddingEnd="10dp"
        android:paddingTop="1dp"
        android:paddingBottom="0dp"
        android:textColor="#CC000000"
        android:visibility="gone"
        android:textSize="12sp"
        />
    <View
        android:layout_height="0.5dp"
        android:layout_width="match_parent"
        android:background="#11EEEEEE"
        android:layout_marginTop="4dp"
        />
</LinearLayout>

Setting cardview by next code:

    mRecyclerView = (RecyclerView) findViewById(R.id.phones_rv);
    mRecyclerView.setHasFixedSize(true);

    mLayoutManager = new LinearLayoutManager(this);
    mRecyclerView.setLayoutManager(mLayoutManager);

    mAdapter = new RVAdapter(data);
    mRecyclerView.setAdapter(mAdapter);

Inside of RVAdapter i am setting listview adapter:

final PhoneNumbersContactListAdapter mAdapter = new PhoneNumbersContactListAdapter(mContext, mDataset.get(position).getPhGosData());
holder.mListView.setAdapter(mAdapter);

getTotalHeightOfListView(holder.mListView);

And then, with getTotalHeightOfListView function i am dynamically setting listview height:

credits to Cristiano

public static void getTotalHeightofListView(ListView listView) {

    ListAdapter mAdapter = listView.getAdapter();

    int totalHeight = 0;

    for (int i = 0; i < mAdapter.getCount(); i++) {
        View mView = mAdapter.getView(i, null, listView);

        mView.measure(
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),

                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

        totalHeight += mView.getMeasuredHeight();
        Log.w("HEIGHT" + i, String.valueOf(totalHeight));

    }

    ViewGroup.LayoutParams params = listView.getLayoutParams();
    params.height = totalHeight
            + (listView.getDividerHeight() * (mAdapter.getCount() - 1));
    listView.setLayoutParams(params);
    listView.requestLayout();

}
Daler
  • 181
  • 1
  • 6
  • 8

2 Answers2

0

My (custom) List View Layout:

set android:layout_height="wrap_content" of your linear layout or give fix height like 50dp,100dp

prashant17
  • 1,520
  • 3
  • 14
  • 23
  • actually please give me screen shot that you want to create – Mr. Programmer Jul 19 '18 at 08:01
  • please see my 3-rd screenshot. I have card, inside of that listview, all items are visible. But if listview have a lot of items, so height of all items is larger than screen resolution height, they are not visible and you can see them, only if you scroll a listview. I need to show all items, without scrolling in listview – Daler Jul 19 '18 at 08:31
0

for this you have to set list item height according to screen resolution inside of your viewholder class. like this..

  Resources resources = context.getResources();
  RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.width = AppHelper.getScreenWidth() / 2;
            params.height = (int) AppHelper.convertDpToPixel((float) 200.0,resources);
            // params.setMargins(10,10,10,10);
            params.leftMargin = 10;
            params.rightMargin = 10;
            params.topMargin = 10;
            params.bottomMargin = 10;
            yourlistlayout.setLayoutParams(params);

//convert dp to pixel fucntion

public static float convertDpToPixel(float dp, Resources resources) {
        DisplayMetrics metrics = resources.getDisplayMetrics();
        return dp * ((float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
    }
unzila
  • 190
  • 1
  • 12
  • Can you clarify from which library did you take AppHelper? And is there any way to do this without 3rd party libraries? – Daler Jul 19 '18 at 08:28
  • App helper is my custom class in which i write this function you can change it to this params.height = (int) convertDpToPixel((float) 200.0,resources); – unzila Jul 19 '18 at 08:41
  • sure use this public static int getScreenWidth() { return Resources.getSystem().getDisplayMetrics().widthPixels; } – unzila Jul 19 '18 at 08:47
  • ok but its working on my side.. can u please share where you put this code – unzila Jul 19 '18 at 09:16