Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – How to make width ratio equal in XML GridView

Posted on November 11, 2022 By

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

READ  [FIXED] kotlin - Is it possible to get Android SD card location on SAMSUNG Tablet / Android 11
Powered by Inline Related Posts
Android Tags:android, gridview, xml

Post navigation

Previous Post: [FIXED] java – How to solve debug issue
Next Post: [FIXED] android – How to solve setting up this payment problem – paypal

Related Posts

[FIXED] Android x86 Kernel Compilation stops abruptly Android
[FIXED] android – downloaded a repo instead of cloning it from gitlab and fixed bugs,now want to push Android
[FIXED] android studio – Hello does someone successfully added Bluetooth CPCL on your Flutter project? Android
[FIXED] android – get amount of lat and lon Android
[FIXED] Pass parameters to flutter module from android or iOS Android
[FIXED] java – How can I resolve the issue of app crashes On Button click? Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What is a good substitute for condensed milk?
  • ¿Como el cuerpo te avisa cuando te va a dar un infarto?
  • What is the luxury brand of Jaguar?
  • Who is Big poppa baseball player?
  • What material are foam runners?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme