Darshak
Darshak

Reputation: 2316

How to create 2 column layout dynamically in android?

MOCK UP

2-column layout

Overview

All are the ImageView with buttons with Scrolling

Requirement

I want to implement 2-Column fix layout for the image display.

And from the press the Button , it would be detect that image.

If only 1 image came then it display only one image in layout.

Upvotes: 1

Views: 3947

Answers (3)

keerthana murugesan
keerthana murugesan

Reputation: 581

Try to use GridView.Look into this for your reference. For 2 column layout give android:numColumns = "2". Load the ImageView and Button dynamically using Adapter. For this,

1.Create an xml which hold the GridView.

<GridView
    android:id="@+id/gridFriends"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="2"/>

2.To define the contents of the GridView, create another XML layout which will hold the ImageView and the ImageButton.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainContainer"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
        <ImageView
            android:id="@+id/img"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"
            android:src="@null" />

        <Button
            android:id="@+id/btn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center">
        <Button>
   </RelativeLayout>

Create a customAdapter for populating the GridView dynamically.Look this for creating custom adapter.In that CustomAdapter getView() inflate the layout(in step 2)

Upvotes: 2

keerthana murugesan
keerthana murugesan

Reputation: 581

Try something like this,

<scrollview>
    <LinearLayout
        android:orientation = "vertical"
        android:height = "match_parent"
        android:width = "match_parent"
    >
      //for two column image view
        <LinearLayout
            android:id = "@+id/image_view_container1"
            android:orientation = "horiZontal"
            android:height = "match_parent"
            android:width = "0dp"
            android:weight = "1">
            <LinearLayout
                android:id = "@+id/image_view_01"
                android:orientation = "vertical"
                android:height = "match_parent"
                android:width = "0dp"
                android:weight = "1">

                <ImageView>
                <Button>
            </LinearLayout>

            <LinearLayout
                android:id = "@+id/image_view_02"
                android:orientation = "vertical"
                android:height = "match_parent"
                android:width = "0dp"
                android:weight = "1">

                <ImageView>
                <Button>
            </LinearLayout>

        </LinearLayout>
    </LinearLayout>
</ScrollView> 

Upvotes: 0

RajaReddy PolamReddy
RajaReddy PolamReddy

Reputation: 22493

Create One ScrollView with LinearLayout as main layout with orientation as Vertical. Add linear laoyut's how many you want to parent payout with two buttons like below code

LinearLayout llay = new LinearLayout(this); 
llay.setOrientation(LinearLayout.HORIZONTAL); 

LinearLayout.LayoutParams llp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

Upvotes: 0

Related Questions