Solution 1 :
With constraintLayout
it’s very easy :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns_android="http://schemas.android.com/apk/res/android"
android_layout_width="match_parent"
android_layout_height="wrap_content"
xmlns_app="http://schemas.android.com/apk/res-auto">
<androidx.appcompat.widget.AppCompatImageView
android_id="@+id/image1"
app_layout_constraintTop_toTopOf="parent"
app_layout_constraintStart_toStartOf="parent"
android_layout_width="0dp"
android_layout_height="0dp"
android_background="@color/red"
app_layout_constraintWidth_percent="0.5"
app_layout_constraintDimensionRatio="1:1"/>
<androidx.appcompat.widget.AppCompatImageView
android_id="@+id/image2"
app_layout_constraintStart_toEndOf="@id/image1"
app_layout_constraintBottom_toTopOf="parentparent"
app_layout_constraintEnd_toEndOf="parent"
android_layout_width="0dp"
android_layout_height="0dp"
android_background="@color/green"
app_layout_constraintWidth_percent="0.5"
app_layout_constraintDimensionRatio="1:1"/>
<androidx.appcompat.widget.AppCompatImageView
android_id="@+id/image3"
app_layout_constraintTop_toBottomOf="@+id/image1"
app_layout_constraintStart_toStartOf="parent"
android_layout_width="0dp"
android_layout_height="0dp"
android_background="@color/yellow"
app_layout_constraintWidth_percent="0.5"
app_layout_constraintDimensionRatio="1:1"/>
<androidx.appcompat.widget.AppCompatImageView
android_id="@+id/image4"
app_layout_constraintTop_toBottomOf="@id/image2"
app_layout_constraintEnd_toEndOf="parent"
android_layout_width="0dp"
android_layout_height="0dp"
android_background="@color/red"
app_layout_constraintWidth_percent="0.5"
app_layout_constraintDimensionRatio="1:1"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Problem :
I am new to Android programming and XML. So I want to have a table-like GridView which will be 100% width(match_parent) so that the children will have equal width ratio. The GridView will have 4 children which should have a 25% of the GridView width each.
Using HTML to illustrate, I will have something like this in my table <td width="25% valign="center">
So here’s my XML code
<GridView
androìd:layout_width="match_parent"
androìd:layout_height="match_parent"
android_rowCount="1"
android_columnCount="4">
<ImageView
androìd:layout_width="40dp"
androìd:layout_height="40dp"
androìd:layout_row="0"
android_layout_column="1"
android_src="@drawable/p1"/>
<ImageView
androìd:layout_width="40dp"
androìd:layout_height="40dp"
androìd:layout_row="0"
android_layout_column="2"
android_src="@drawable/p2"/>
<ImageView
androìd:layout_width="40dp"
androìd:layout_height="40dp"
androìd:layout_row="0"
android_layout_column="3"
android_src="@drawable/p3"/>
<ImageView
androìd:layout_width="40dp"
androìd:layout_height="40dp"
androìd:layout_row="0"
android_layout_column="4"
android_src="@drawable/p4"/>
</GridView>
But this code doesn’t make the it look like a table or grid, it just align the images horizontally. Please I need help.
Comments
Comment posted by KANAYO AUGUSTIN UG
Then what about the images?
Comment posted by Kapta
Now you have four images. just set