0

I am doing an Android app and i need put 4 elements or pictures in a activity. My idea is the pictures are distributed in two rows and two colums. Applications always limits their display to portrait orientation.

I think a gridview is a good solution no? but my question is, how can i do with this gridview to it adapt to everey display?

This is the layout where i define it:

<!-- ?xml version="1.0" encoding="utf-8"? -->
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:numColumns="2" />

Some new parameters to this layout??

Thanks

user3243651
  • 241
  • 2
  • 4
  • 14

2 Answers2

0

Try the following XML code:

<!-- ?xml version="1.0" encoding="utf-8"? -->
<GridView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/gridview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:verticalSpacing="0dp"
    android:horizontalSpacing="0dp"
    android:stretchMode="columnWidth"
    android:numColumns="2" />
Manitoba
  • 8,522
  • 11
  • 60
  • 122
0

if you have static number image ( like just 4 image ) better to use LinearLayout and set weight for each Image to fill page, but if you don't have static number you need write custom adapter and get height of display then set height if each element to (height / numberRow) in your code.

for first suggest you can use following code:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:layout_weight=".5"
    android:weightSum="1">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher"
         android:layout_weight=".5" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher" 
         android:layout_weight=".5"/>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight=".5" >

        <ImageView
        android:id="@+id/imageView1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher"
         android:layout_weight=".5" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher" 
         android:layout_weight=".5"/>
</LinearLayout>

but second suggest its more complicated. you need do following work:

1 - create custom adapter for gridView ( Developing a custom adapter)

2 - get screen size (How to get screen dimensions)

3 - set height of View in getView to ( height / numberRow )

and set following code to your gridView

 android:numColumns="2"

this create 2 column in each row

Community
  • 1
  • 1
Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63
  • yes in my application the number of elements is static (4) so your first solution is Ok, thansk so much for your code – user3243651 Jun 25 '14 at 08:48