1

I am new to Gridlayout in android, but i create a layout which looks fine on preview screens but when i run it on real device all items gets compressed and get aligned to top-left of screen

This layout is perfect when we see preview on android studio or eclipse with all items having equally stretched or width but on real device it gets compressed to left of layout

My xml is as follows,i need help in sortring out this error:-

  <?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="wrap_content"
                    android:paddingBottom="5dp"
                    android:paddingTop="5dp"
                    android:useDefaultMargins="true"
                    android:background="@drawable/backgournd_cancel"
                    android:columnCount="4">

                        <TextView
                            style="@style/style_textView_marquee"
                            android:layout_columnSpan="4"
                            android:text="Vikramaa" />

                        <ImageView
                           style="@style/style_imageView"
                            android:layout_columnSpan="1"
                            android:layout_columnWeight="1"
                             android:src="@drawable/ic_launcher"  />

                        <ImageView
                            style="@style/style_imageView"
                            android:layout_columnSpan="1"
                            android:layout_columnWeight="1"
                            android:src="@drawable/edit_pressed" />

                        <ImageView
                            style="@style/style_imageView"
                            android:layout_columnSpan="1"
                            android:layout_columnWeight="1"
                            android:src="@drawable/heart_normal" />

                        <ImageView
                            style="@style/style_imageView"
                            android:layout_columnSpan="1"
                            android:layout_columnWeight="1"
                            android:src="@drawable/open_pressed" />

                </GridLayout> 
Kartheek
  • 7,104
  • 3
  • 30
  • 44
Reprator
  • 2,859
  • 2
  • 32
  • 55

1 Answers1

0

This should resolve your issue please see this answer from @HenrikS for more details

<GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:columnCount="1"
>
    <TextView
        android:text="2x2 button grid"
        android:textSize="32dip"
        android:layout_gravity="center_horizontal" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" android:orientation="horizontal">
        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 1" />
        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:text="Button 2" />
        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
    >
        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 3" />
        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="start"
            android:text="Button 4" />
        <Space
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>
</GridLayout>
Community
  • 1
  • 1
Husnain Aslam
  • 865
  • 1
  • 11
  • 28
  • then what is the advantage of using grid layout, i can accomplish this with the use of relative and linear layout....but thanks revolution. – Reprator May 25 '15 at 08:29
  • There are limitations when using the GridLayout. See the documentation http://developer.android.com/reference/android/widget/GridLayout.html – Husnain Aslam May 25 '15 at 08:31