-1

i am creating custom listview containning textview only..
( i will not like Setting height of the root LinearLayout of custom_textview.xml mean fixing the height)

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"

        android:layout_marginTop="5dp"
        android:divider="@drawable/divider"
        android:dividerHeight="2dip"
        android:listSelector="@drawable/my_list_selector" >
    </ListView>

    <!-- <ListView -->
    <!-- android:id="@+id/lv_diseases" -->
    <!-- android:layout_width="match_parent" -->
    <!-- android:layout_height="match_parent" -->
    <!-- android:layout_alignParentLeft="true" -->
    <!-- android:layout_marginTop="5dp" -->
    <!-- android:layout_below="@+id/imageView1" > -->


    <!-- </ListView> -->

    <TextView
        android:id="@+id/emptyText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:layout_marginTop="232dp"
        android:gravity="center"
        android:text="Empty List"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:visibility="gone" />

</RelativeLayout>

this is getview method in listview_Adpter @

Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder view;
        LayoutInflater inflator = activity.getLayoutInflater();

        if (convertView == null) {
            view = new ViewHolder();

            convertView = inflator.inflate(R.layout.custom_textview, null);

            view.txtViewTitle = (TextView) convertView
                    .findViewById(R.id.list_content);

            view.txtViewTitle.setTextColor(Color.WHITE);

            view.txtViewTitle.setTypeface(Typeface.createFromAsset(parent
                    .getContext().getAssets(), "fonts/DroidSerif.ttf"));

            convertView.setTag(view);

        }

        else {
            view = (ViewHolder) convertView.getTag();
        }
        System.gc();
        try {
            view.txtViewTitle.setText(listCountry.get(position));

        } catch (Exception e) {
            System.out.println("this is error " + e.getMessage());
        }

        return convertView;
    }

and this is custom_textview.xml inside my textview is

 <?xml version="1.0" encoding="utf-8"?>
    <!-- Definig a container for you List Item-->
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="center_vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <!-- Defining where should text be placed. You set you text color here-->
    <TextView
    android:id="@+id/list_content"
    android:textColor="#000000"
    android:gravity="left"
    android:text="sample"
         android:paddingTop="10dp"
    android:layout_margin="4dip"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="7dp"
    />

    </LinearLayout>

and this is how i implement

 listview_Adpter la = new listview_Adpter(getActivity(), listname);
            lv.setAdapter(la);

every thing works fine but list view display data in very big height how to overcome it? i want to show it in middle

Charles
  • 50,943
  • 13
  • 104
  • 142
Android
  • 8,995
  • 9
  • 67
  • 108

4 Answers4

2

android:layout_marginTop="232dp" ?? Do you really need that ... i think this is what is causing the height problem.

Viral Patel
  • 32,418
  • 18
  • 82
  • 110
1

You can set the of the custom_textview android:layout_height from "fill_parent" to "match_parent"

baronS
  • 1,074
  • 11
  • 11
1

You can simply remove the LinearLayout in custom_textview.xml
It's not needed at all.

You might need to add xmlns:android="http://schemas.android.com/apk/res/android" to your TextView, as well as changing its gravity to android:gravity="left | center_vertical"

[EDIT]

Your final row item layout (custom_textview.xml):

<?xml version="1.0" encoding="utf-8"?>
<!-- Defining a List Item-->
<!-- Defining where should text be placed. You set you text color here-->
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_content"
    android:textColor="#f000"
    android:gravity="left|center_vertical"
    android:text="sample"
    android:paddingTop="10dp"
    android:layout_margin="4dip"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="7dp"
/>

[EDIT 2]

Some more ideas for customizing your row:

You can even go further specific and give the item a decoration (a 9 patch to add a border or a background color which could also be different for even and odd items).
You can also add custom margins and/or paddings to the row.
And you could even add an embedded (compound) drawable to it.
The text can have an inset or raised aspect (play with the text color and its shadow).

All this with a single TextView!!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • See my edited answer. By the way, you don't need to set the text, since you're going to change it in code. Also note that I fixed the **deprecated** (since API Level 8) "fill_parent" with its equivalent "match_parent" – Phantômaxx Mar 13 '14 at 12:47
0

Use the below layout as custom_textview.xml...

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

    <TextView
        android:id="@+id/list_content"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:padding="7dp"
        android:text="sample"
        android:textColor="#000000" />

</LinearLayout>

Your ListView item's height is looking bigger because, your are applying 17dp padding and 4dp margin at the top and 7dp padding and 4dp margin at the bottom of the Textview...so, its taking total 32dp extra space excluding your TextView. Minimize them as the above layout...I think, your problem will be solved.

Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41