How to set the row width of listview in xamarin android

Shay Wilner 1,746 Reputation points
2023-05-05T15:33:05.63+00:00

Hi

i have a class to implement a listview

 class Classmenuholydaystat : BaseAdapter<string>


{

   private Context context;\\

 private List<string> items;

 public Classmenuholydaystat(Context context, List<string> result)

    {

        this.context = context;

        this.items = result;


       

    }

    public override string this[int position]

    {

        get { return items[position]; }

    }

    public override int Count

    {

        get { return items.Count; }

    }

    public override long GetItemId(int position)

    {

        return position;

    }

    public override View GetView(int position, View convertView, ViewGroup parent)

    {

        var item = items[position];

        View view = convertView; // re-use an existing view, if one is available

        if (view == null) // otherwise create a new one

        {

            LayoutInflater inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);

            view = inflater.Inflate(Resource.Layout.viewmenu, null);

        }


        

            view.SetBackgroundResource(Resource.Drawable.itemmenustat);

        view.FindViewById<TextView>(Resource.Id.viewtext).Text = items[position];

        return view;

    }

the viewmenu xml

<?xml version="1.0" encoding="utf-8" ?> 
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:paddingStart="6dp"
    android:elegantTextHeight="true"
    android:textColor="#FFFFFFFF"
    android:textStyle="bold"
     android:fontFamily="calibri"
    android:textSize="21sp"
 
    android:id="@+id/viewtext" >
    
</TextView>

 <ListView
          android:layout_width="wrap_content" 
          android:id="@+id/dgvmenuholydays"
          android:layout_below="@+id/buttonhome"
          android:layout_marginTop="2dp"
        android:layout_alignParentStart="true"
          android:layout_height="wrap_content"
        android:fastScrollEnabled="true"
        />

T

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,326 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 72,251 Reputation points Microsoft Vendor
    2023-05-08T07:02:56.9966667+00:00

    Hello,

    How to set the row width of listview in xamarin android

    You can do this by setting a fixed value for Listview's layout_width. Such as android:layout_width="300dp"

    If you set android:layout_width="wrap_content" . ListView will be as wide as the widest of its children. ListView must therefore measure its items and to get the items it has to call getView() on the Adapter. This may happen several times depending on the number of layout passes, the behavior of the parent layout, etc. It will run lots of times getView() on the Adapter. You should implement getView() to be as efficient as possible.

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful