question

dersharky-6168 avatar image
0 Votes"
dersharky-6168 asked RobCaplan edited

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

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>
dotnet-xamarin
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @dersharky-6168 ,I have not heard from you for a couple of days. Please let me know if there is anything that I can help here.

0 Votes 0 ·

1 Answer

JessieZhang-2116 avatar image
0 Votes"
JessieZhang-2116 answered JessieZhang-2116 edited

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://docs.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.



image.png (18.3 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.