0

I would like to display sqlite data in ListView. But I don't know how to set up the fixed width in ListView column.

How can I make the column with fixed size?

    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_co## Heading ##ntent" 
    android:layout_gravity="center"

Layout of ListView

Here is my XML. I just want to make the name,phone and location column in fixed layouts.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name:" />



    <TextView

        android:id="@+id/phoneNo"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="0.5"
        android:gravity="center" 
        android:text="Phone no:" />

    <Button
        android:id="@+id/location"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Location:" />
</LinearLayout>

tools:context=".Shopping" >

 <ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:layout_gravity="center"/>

Thanks a lot.

user3336747
  • 23
  • 2
  • 7
  • possible duplicate of [Columns in ListView Android](http://stackoverflow.com/questions/3612895/columns-in-listview-android) // note the [second answer](http://stackoverflow.com/a/11508661/2331953) – flx Feb 25 '14 at 03:47
  • fixed view size? You are working with Android I recomend you to use weight in your layout, you will kill your application if you start using fixed sizes, I can help if you want just post your xml and I will try – GhostDerfel Feb 25 '14 at 03:49

2 Answers2

1

For this layout user3336747 you have two way: 1. Use Different colour for name,phone,location and same for their value. 2. Inflate Below layout in adapter of listview.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ellipsize="end"
        android:gravity="center"
        android:singleLine="true"
        android:text="" />

    <TextView
        android:id="@+id/phoneNo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:singleLine="true"
        android:text="Phone no:" />

    <TextView
        android:id="@+id/location"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ellipsize="end"
        android:singleLine="true"
        android:text="" />
</LinearLayout>

I think it will help.

Yogendra
  • 4,817
  • 1
  • 28
  • 21
1

You shall need to create a custom list this link is closer to you requirement

http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=92

but you can achieve it by simply adding appropriate weight to custom items of listview.

like this , first create a custom layout

<?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:orientation="horizontal"
    android:background="@drawable/classback" >

    <TextView
        android:id="@+id/headingtv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center|left"
        android:layout_weight=".5" 
        android:layout_marginLeft="30dp"
        android:padding="10dp"
        android:textColor="#555555"/>

    <TextView
        android:id="@+id/datetv"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:layout_weight=".5"
         android:layout_margin="3dp"
        android:textColor="#555555" />

</LinearLayout>

then create a class extending baseadapter

class ClassCustomList extends BaseAdapter
{

    @Override
    public int getCount() 
    {
        // TODO Auto-generated method stub
        return onelist.size();

    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub


        LayoutInflater inflater ;

        inflater = (LayoutInflater) Class_List.this.
                getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        ViewHolder holder ; 
        if( arg1 == null )
        {

            holder = new ViewHolder();
              arg1 = inflater.inflate(R.layout.classcustomelement, 
                    null);

            holder.Heading = (TextView)arg1.findViewById(R.id.headingtv);

            holder.Date = (TextView)arg1.findViewById(R.id.datetv);

            arg1.setTag(holder);
        }

        else 
        {  

            holder = (ViewHolder)arg1.getTag();

        }

        holder.Heading.setText(twolist.get(arg0));
        holder.Date.setText(fourlist.get(arg0));



        return arg1;
    }


    class ViewHolder 
    {
        TextView Heading , Date ; 
    }

}

and the add this in yourlistview

class_List.setAdapter(new ClassCustomList());

you can see this also

http://www.codeproject.com/Articles/507651/Customized-Android-ListView-with-Image-and-Text

Avi Kumar
  • 4,403
  • 8
  • 36
  • 67