0

I have this ListView in this Fragment, I need to be able to see the full string inside the item in the list, is it possible? It would be also better if the ListView could have different item heights with the different strings

enter image description here

This is the code:
Java:

package com.rs.donkey;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

public class FragmentHomeworkNotDone extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
        final View rootView = inflater.inflate(R.layout.fragment_homework, container,
                false);
        List<String> compiti = HomeworkActivity.compiti;
        List<String> eseguiti = HomeworkActivity.eseguiti;
        List<String> compitiDaFare = new ArrayList<String>();
        for (int i = 0; i < compiti.size(); i++){
            if (eseguiti.get(i).equals("F")){
                compitiDaFare.add(compiti.get(i));
                Log.i("COMPITI FATTI", compiti.get(i));
            }
        }
        ListView compitiList = (ListView) rootView.findViewById(R.id.listaCompiti);
        compitiList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        ArrayAdapter<String> adapterCompiti = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_checked, compitiDaFare);
        compitiList.setAdapter(adapterCompiti);



        return rootView;
    }

}

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">
    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:singleLine="false"
        android:id="@+id/listaCompiti">
    </ListView>

</RelativeLayout>
Alessio Ragno
  • 476
  • 1
  • 6
  • 20
  • It looks like your fragment is just really tiny. The problem is probably in your activity layout – Gabe Sechan Jul 25 '15 at 20:55
  • nope, because if I set the listview layout to simple_list_item_1 it is high enough – Alessio Ragno Jul 25 '15 at 20:56
  • No, that is the problem. A listview will show as much as it can, given its height. If it isn't showing more, then its because it isn't tall enough. Given your layout here, its set to match parent, so it will fill the relative layout, which fills the fragment. So the fragment itself needs its height adjusted. – Gabe Sechan Jul 25 '15 at 21:09

2 Answers2

1

The android.R.layout.simple_list_item_checked sets a height for each list item.

You need to create your own custom list item layout and pass it in to your ArrayAdapter constructor.

layout/simple_list_item_checked.xml:

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeightSmall"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:checkMark="?android:attr/textCheckMark"
    android:paddingStart="?android:attr/listPreferredItemPaddingStart"
    android:paddingEnd="?android:attr/listPreferredItemPaddingEnd" />

fragment:

ArrayAdapter<String> adapterCompiti = new ArrayAdapter<String>(getActivity(), R.layout.simple_list_item_checked, compitiDaFare);
tachyonflux
  • 20,103
  • 7
  • 48
  • 67
-1

Try:

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:singleLine="false"
    android:id="@+id/listaCompiti"
    android:textAppearance="?android:attr/textAppearanceSmall">
</ListView>

Or use these two to your preference:

android:textAppearance="?android:attr/textAppearanceLarge"

android:textAppearance="?android:attr/textAppearanceMedium"

Another option is:

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:singleLine="false"
    android:id="@+id/listaCompiti"
    android:minHeight="@dimen/minHeight">
</ListView>

Then in dimen.xml:

<dimen name="minHeight">48dp</dimen>

pez
  • 3,859
  • 12
  • 40
  • 72