How can I insert an element in a specific row in a GridLayout?

der_sharky 21 Reputation points
2021-06-18T12:31:11.41+00:00

I am programming an Android app and I need to create a grid with 4 rows. In each row I need to put different elements. The problem is that I can't insert the elements in the desired row. In my example the ImageButton is inserted in the first row although I want to insert it in the second row. Does anyone have a solution how to do this with XML ? Thank you

<?xml version="1.0" encoding="utf-8"?>

<GridLayout
android:id="@+id/GridLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="2"
android:orientation="horizontal"
android:rowCount="4"
tools:context=".GridXMLActivity"
tools:layout_editor_absoluteX="56dp"
tools:layout_editor_absoluteY="0dp">

    <ImageButton  
       android:id="@+id/imageButton1"  
       android:layout_width="160dp"  
       android:layout_height="90dp"  
       android:layout_row="2"  
       android:layout_gravity="left|top"  
       android:src="@drawable/scene1" />  

</GridLayout>

Developer technologies .NET Xamarin
{count} votes

1 answer

Sort by: Most helpful
  1. JessieZhang-MSFT 7,716 Reputation points Microsoft External Staff
    2021-06-20T13:17:52.08+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    In my example the ImageButton is inserted in the first row although I want to insert it in the second row.

    There are things that need to be corrected.

    1. If you want to put an ImageButton in your GridLayout in the second row, the android:layout_row should be 1,because the number of row starts at 0.:
       android:layout_row="1"  
      
      2.If you don't place any controls before placing an ImageButton, the previous row and column positions won't take up space. That's why it looks like it's in the first place.

    3.If you want to the ImageButton to look like in the second row, you can put a view in the first row, for example:

      <?xml version="1.0" encoding="utf-8"?>  
    <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_width="match_parent"  
        android:layout_height="match_parent"  
        android:id="@+id/GridLayout1"  
        android:columnCount="2"  
        android:layout_margin="10dp"  
        android:orientation="horizontal"  
        android:rowCount="4">  
             <View   
                android:visibility="visible"  
                android:layout_height="50dp"  
                android:layout_width="50dp"  
                android:textSize="14dip"   
                android:layout_row="0"  
                android:layout_column="0" />    
            <ImageButton  
              android:id="@+id/imageButton1"  
              android:layout_width="90dp"  
              android:layout_height="90dp"  
              android:layout_row="1"  
              android:layout_column="0"  
              android:src="@drawable/image1" />  
    </GridLayout>  
    

    The result is:

    107317-image.png

    For more details, you can check:https://learn.microsoft.com/en-us/xamarin/android/user-interface/layouts/grid-layout

    Best Regards,

    Jessie Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    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 comments No comments

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.