skip to Main Content

I am new to Android development, and I am trying to use the GridLayout to create rows with 3 columns, each column with a imageview. In this case I am using a square image created in photoshop, all red, just for testing.

This is the xml I currently use:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/activity_menu"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="me.andrewlarsen.testapp.MenuActivity">

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:rowCount="2"
        android:columnCount="3"
        android:gravity="center"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">


        <ImageView
            android:layout_gravity="fill"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@mipmap/redblock"
            android:layout_row="0"
            android:layout_column="0"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:scaleType="fitXY"
            android:id="@+id/imageView1" />

        <ImageView
            android:layout_gravity="fill"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@mipmap/redblock"
            android:layout_row="0"
            android:layout_column="1"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:scaleType="fitXY"
            android:id="@+id/imageView2" />

        <ImageView
            android:layout_gravity="fill"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@mipmap/redblock"
            android:layout_row="0"
            android:layout_column="2"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:scaleType="fitXY"
            android:id="@+id/imageView3" />

        <ImageView
            android:layout_gravity="fill"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@mipmap/redblock"
            android:layout_row="1"
            android:layout_column="0"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:id="@+id/imageView4" />

        <ImageView
            android:layout_gravity="fill"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@mipmap/redblock"
            android:layout_row="1"
            android:layout_column="1"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:id="@+id/imageView5" />

        <ImageView
            android:layout_gravity="fill"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@mipmap/redblock"
            android:layout_row="1"
            android:layout_column="2"
            android:layout_columnWeight="1"
            android:layout_rowWeight="1"
            android:id="@+id/imageView6" />

    </GridLayout>
</RelativeLayout>

The xml above gives a result looking like this:

enter image description here

So as you can see I got 2 rows, that is different from eachother. On all 3 columns in the first row I’ve added android:scaleType=”fitXY” however it only scales horizontally, not vertically.

I want it to scale up, in both directions so the result would be a square just larger.

I am trying to make it look like this:

enter image description here

When I am using android:scaleType=”fitXY” I would expect the image to scale up both in X direction (horizontally) and Y direction (vertically) however it does not do so.

Appreciate all the help I can get.

2

Answers


  1. Chosen as BEST ANSWER

    Problem solved, had to use:

    android:adjustViewBounds="true"
    

    in each imageview element


  2. Try Remove weights (android:layout_columnWeight=”1″ and android:layout_rowWeight=”1″ ), and what size your redblock ? if possible send your image.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search