how to add button(s) in grid layout in xamarin android

Shay Wilner 1,746 Reputation points
2023-06-25T19:07:27.58+00:00

Hello

I have a gridview that i fill it using a baseadapter and i use a gridlayout to display.

table

I would like to add button for each row but not to the header

here the class adapter

class listbirthdateadapter : BaseAdapter<Classdatabirth>
    {
        private Context context;
        private List<Classdatabirth> listbirthdate;
  public listbirthdateadapter(Context thecontext, List<Classdatabirth> Listresult) :base()
        {
            this.context = thecontext;
            this.listbirthdate = Listresult;
        }
        public override Classdatabirth this[int position]
        {
            get { return listbirthdate[position]; }
        }
        public override int Count
        {
            get { return listbirthdate.Count; }
        }
        public override long GetItemId(int position)
        {
            return position;
        }
        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var item = listbirthdate[position];
            View view = convertView; // re-use an existing view, if one is available
            if (view == null) // otherwise create a new one
            {
               // view = activity.LayoutInflater.Inflate(Resource.Layout.griditem, null);
              
               
                LayoutInflater inflater = (LayoutInflater)context.GetSystemService(Context.LayoutInflaterService);
                view = inflater.Inflate(Resource.Layout.tablebirthdate, null);
              
            }
            else
            {
                view = convertView;
            }
             view.FindViewById<TextView>(Resource.Id.tablenamebirth).Text = item.namebirth;
             view.FindViewById<TextView>(Resource.Id.tablesurname).Text = item.surname;
             view.FindViewById<TextView>(Resource.Id.tabledatebirth).Text = item.datebirth;
             view.FindViewById<TextView>(Resource.Id.tableremain).Text = item.remain;
           
            if (position == 0)
            {
                view.SetBackgroundResource(Resource.Drawable.itemstat);
              
            }
             else
            {
                view.SetBackgroundResource(Resource.Drawable.itemmark);
             }
                
            return view;
        }
    }

and the xml

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation= "horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     android:background="#ff71216c"
   android:columnCount="4"
   >
     
    <TextView
        android:id="@+id/tableremain"       
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:fontFamily="calibri"
        android:textColor="#ffffffff"
        android:layout_columnWeight= "1"
        
         />
   
     <TextView
        android:id="@+id/tabledatebirth"       
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
         android:fontFamily="calibri"
        android:textColor="#ffffffff"
         android:layout_columnWeight= "1"
        
         />
    <TextView
        android:id="@+id/tablesurname"       
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:fontFamily="calibri"
        android:textColor="#ffffffff"
        android:layout_columnWeight= "1"
         
          />
    <TextView
        android:id="@+id/tablenamebirth"       
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:fontFamily="calibri"
        android:textColor="#ffffffff"
        
        android:layout_columnWeight= "1"
         /> 
</GridLayout> 

Thanks

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-06-26T02:29:03.7666667+00:00

    Hello,

    You can display Title and GridView items separately. For example, You can add a Girdlayout( display the Title) above your gridview like following layout. And add button to your tablebirthdate layout.

    Because listview have a reuse layout issue, So I recommend you add a Girdlayout( display the Title) above your gridview.

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout
        android:orientation="vertical"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:orientation="horizontal"
            android:id="@+id/gridLayout1"
            android:columnCount="5"
            android:background="#ff71216c"
          >
            
         <TextView
            android:text="Day remain"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="22sp"
            android:fontFamily="calibri"
            android:textColor="#ffffffff"
            android:layout_columnWeight= "1"
            
             />
       
         <TextView
            android:text="Birthdate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="22sp"
            android:fontFamily="calibri"
            android:textColor="#ffffffff"
            android:layout_columnWeight= "1"
            
             />
        <TextView
            android:text="Surname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="22sp"
            android:fontFamily="calibri"
            android:textColor="#ffffffff"
            android:layout_columnWeight= "1"
             
              />
        <TextView    
            android:text="Name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="22sp"
            android:fontFamily="calibri"
            android:textColor="#ffffffff"       
            android:layout_columnWeight= "1"
             /> 
            <Button
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:text="hide"
                 android:layout_columnWeight= "1"
                 android:visibility="invisible"/>
        </GridLayout>
    
        <GridView    
            android:id="@+id/gridview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"      
            />
    
    </LinearLayout>
    
    

    Then you can move the SetBackgroundResource from adapter to the OnCreate method.

               GridLayout gridLayout1 = FindViewById<GridLayout>(Resource.Id.gridLayout1);
                gridLayout1.SetBackgroundResource(Resource.Drawable.itemstat);
    

    Next, you can add button in your tablebirthdate.xml.

    <?xml version="1.0" encoding="utf-8"?>
    <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation= "horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ff71216c"
        android:columnCount="5"
       >
         
        <TextView
            android:id="@+id/tableremain"
            android:text="test1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="22sp"
            android:fontFamily="calibri"
            android:textColor="#ffffffff"
            android:layout_columnWeight= "1"
            
             />
       
         <TextView
             android:text="test1"
            android:id="@+id/tabledatebirth"       
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="22sp"
             android:fontFamily="calibri"
            android:textColor="#ffffffff"
             android:layout_columnWeight= "1"
            
             />
        <TextView
             android:text="test1"
            android:id="@+id/tablesurname"       
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="22sp"
            android:fontFamily="calibri"
            android:textColor="#ffffffff"
            android:layout_columnWeight= "1"
             
              />
        <TextView
             android:text="test1"
            android:id="@+id/tablenamebirth"       
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="22sp"
            android:fontFamily="calibri"
            android:textColor="#ffffffff"       
            android:layout_columnWeight= "1"
             />
    <Button
            android:id="@+id/button1"
            android:text="add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             android:layout_columnWeight= "1"/>
    </GridLayout>
    

    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.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.