2

I have a gridlayout like this in the main layout xml of my activity:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/parentLayout"
    android:layout_gravity="right">

    <GridView 
        android:id="@+id/gridview"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:background="#AAAAAA"
        android:columnWidth="90dp"
        android:numColumns="3"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="none"
        />

</LinearLayout>

I populate it with 6 icons by inflating the following:

<?xml version="1.0" encoding="utf-8"?>
<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/searchIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:contentDescription="@string/searchIcon"/>

What I want is that the gridlayout is right aligned in the linear layout and that it just wraps its content. I am testing this by giving it a different coloured background. The layout_height="wrap_content" works fine, but the width seems to have no effect. The result is this:

Screenshot of wrap_content not working as I expect

Any ideas?

Thanks in advance, Stephen

ilomambo
  • 8,290
  • 12
  • 57
  • 106
steprobe
  • 1,659
  • 3
  • 17
  • 32

2 Answers2

0

use android:gravity="right" in LinearLayout instead-of LinearLayout ..............

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:id="@+id/parentLayout"

    android:gravity="right"> //<-----------------------
  • android:gravity sets the gravity of the content of the View its used on.
  • android:layout_gravity sets the gravity of the View or Layout in its parent.

for test try

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:orientation="vertical" android:id="@+id/parentLayout"

    android:gravity="right">

    <TextView android:id="@+id/gridview" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:background="#AAAAAA"
        android:text="ewewqe" />

</LinearLayout>

also see this not the answer but see

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:id="@+id/parentLayout"
    android:layout_gravity="right"
    android:gravity="right" android:weightSum="1">

    <GridView
        android:id="@+id/gridview"
        android:layout_height="wrap_content"
        android:layout_width="0dp"
        android:background="#AAAAAA"
        android:columnWidth="90dp"
        android:numColumns="3"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="none"
        android:layout_weight=".5" />

</LinearLayout>
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
0

Insert android:gravity="right" in GridView

<GridView 
  ......
  .....
  android:gravity="right" />
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • I tried various combinations of gravity and none of them worked. The problem seems to be that the width of the content, from the perspective of the LinearLayout is full screen. – steprobe Jun 23 '12 at 12:43