user782104
user782104

Reputation: 13545

How to create a grid like ui in android?

I would like to implement the following ui

enter image description here

The screen will divide into 8 parts which is equal size, each of them contain a imageview

I would perfer not to use grid view as it is not a gallery . Thanks for helping

I tried the following code , but left and right column are not equals

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/share_bg">

    <LinearLayout
        android:id="@+id/s_leftColumn"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/s_rightColumn"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/title" />

        <ImageView
            android:id="@+id/s_sixMates"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/leaderboard_a" />

        <ImageView
            android:id="@+id/s_player"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/leaderboard_b" />

        <ImageView
            android:id="@+id/s_party"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/leaderboard_c" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/s_rightColumn"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/s_food"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/leaderboard_d" />

        <ImageView
            android:id="@+id/s_dress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/leaderboard_e" />

        <ImageView
            android:id="@+id/s_attendant"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/leaderboard_f" />

        <ImageView
            android:id="@+id/s_ball"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/leaderboard_g" />
    </LinearLayout>

</RelativeLayout>

!!!!!!!!!!!!!!Update:!!!!!!!!!!!!!!!!!!!!!!

After I implement gridview , the images size is too large and out of screen , how to fix that? thanks

<?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:background="@drawable/share_bg"
    android:columnCount="2"
    android:orientation="vertical"
    android:rowCount="4" >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/title" />

    <ImageView
        android:id="@+id/s_sixMates"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/leaderboard_a" />

    <ImageView
        android:id="@+id/s_player"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/leaderboard_b" />

    <ImageView
        android:id="@+id/s_party"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/leaderboard_c" />

    <ImageView
        android:id="@+id/s_food"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/leaderboard_d" />

    <ImageView
        android:id="@+id/s_dress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/leaderboard_e" />

    <ImageView
        android:id="@+id/s_attendant"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/leaderboard_f" />

    <ImageView
        android:id="@+id/s_ball"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/leaderboard_g" />

</GridLayout>

Updated: with using Ankit solution . Unfortunately the left right is not equal , please refer to the screenshot enter image description here

Upvotes: 0

Views: 104

Answers (1)

Ankit
Ankit

Reputation: 483

Use this code for your reference and replaced with your drawable. Its working fine for me :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:weightSum="2" >

    <LinearLayout
        android:id="@+id/s_leftColumn"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/s_sixMates"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/s_player"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/s_party"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/s_rightColumn"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/s_food"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/s_dress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/s_attendant"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/s_ball"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

</LinearLayout>

Upvotes: 1

Related Questions