GridView filling parent despite wrap_content - android

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:
Any ideas?
Thanks in advance,
Stephen

Insert android:gravity="right" in GridView
<GridView
......
.....
android:gravity="right" />

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>

Related

Can not inflate button widget after/before GridView

I want to add a button widget just above the GridView,But it seems not happy with my code, i mean its not showing up on the screen, no matter what i am doing,by the way am on 7.1.1, here is my layout,
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/images_grid_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:columnWidth="180dp"
android:gravity="center"
android:horizontalSpacing="8dp"
android:numColumns="3"
android:padding="16dp"
android:stretchMode="columnWidth"
android:verticalSpacing="8dp">
</GridView>
<Button
android:text="Next"
android:id="#+id/next_Button"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>
So this is it, layout is insanely simple, but i can not figure it out, any help would be highly appreciable !
Use this:
<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/next_Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Next" />
<GridView
android:id="#+id/images_grid_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/next_Button"
android:columnWidth="180dp"
android:gravity="center_horizontal"
android:horizontalSpacing="8dp"
android:numColumns="3"
android:padding="16dp"
android:stretchMode="columnWidth"
android:verticalSpacing="8dp">
</GridView>
</RelativeLayout>
Use RelativeLayout to let the Button stick to the parent's bottom, and set the GridView to stay above the button
You are trying to use Linear layout weight attribute.If you are trying to use it grid layout layout height attribute should also be 0dp and you should add layout weight attribute for grid layout too.
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/images_grid_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6"
android:adjustViewBounds="true"
android:columnWidth="180dp"
android:gravity="center"
android:horizontalSpacing="8dp"
android:numColumns="3"
android:padding="16dp"
android:stretchMode="columnWidth"
android:verticalSpacing="8dp">
</GridView>
<Button
android:text="Next"
android:id="#+id/next_Button"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>
Hope it helps u.

Set maximum column height in gridLayout to avoid overlapping

in my app I am encountering this problem:
As you can see i added the option to let the use specify a name through an edit text in an alertDialog. Hypotetically the user could enter an extremely long name, ruining the entire layout as the following rows will overlap.
How can I enter a specified height and width for each row?
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:longClickable="true"
/>
grid_item.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Layout for a single list item -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="16dp">
<TextView
android:id="#+id/text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="test text"
/>
</LinearLayout>
Give some height to your row item parent or textview as below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="16dp">
<TextView
android:id="#+id/text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="test text"
/>
</LinearLayout>
Or setheight of your textview or give it maxlines
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal"
android:minHeight="?android:attr/listPreferredItemHeight"
android:padding="16dp">
<TextView
android:id="#+id/text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="6"
android:minLines="6"
tools:text="test text"
/>
</LinearLayout>

Unwanted Gridview spacing

My grid design is as such. But i want to remove the spaces on both sides of the grid elements. (Black marked)
I want the image to be expanded somewhat like
My XML layouts for the gridview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<GridView
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="100dip"
android:gravity="center"
android:numColumns="2"
android:stretchMode="spacingWidthUniform" />
</LinearLayout>
My grid element layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/grid_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:contentDescription="#string/im" >
</ImageView>
<TextView
android:id="#+id/grid_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLength="23"
android:gravity="center"
android:layout_marginTop="10sp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textSize="12sp" >
</TextView>
</LinearLayout>
Try Changing
<GridView
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:verticalSpacing="1dp"
android:horizontalSpacing="1dp"
android:numColumns="2"
android:background="#abcdef"
android:stretchMode="columnWidth" />
You can replace GridView with GridLayout and use weights to make the images fill the screen. Here's the reference: http://developer.android.com/reference/android/widget/GridLayout.html, (See the section: Excess space distribution)

GridView not centering; aligned right

I can't figure out where this left padding is coming from.
My grid is not just uncentered, it appears to be aligned to the right. What's up with this?
Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp"
android:paddingBottom="8dp">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridView"
android:numColumns="auto_fit"
android:columnWidth="240dp"
android:stretchMode="spacingWidthUniform"
android:verticalSpacing="22dp"
android:horizontalSpacing="16dp"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent = "true">
</GridView>
</RelativeLayout>
I believe it is coming from the line
android:stretchMode="spacingWidthUniform"
Remove that and test it out.
Try this tag
android:gravity="center" in GridView
As
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridView"
android:numColumns="3"// Changed to 3
android:columnWidth="60dp"
android:stretchMode="columnWidth"// Added
android:verticalSpacing="22dp"
android:horizontalSpacing="16dp"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>
</GridView>
</RelativeLayout>

Padding of grid in GridView

I have some problems with my grid layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/frag_books_grid_view_layout"
android:orientation="vertical">
<GridView
android:padding="0dp"
android:cacheColorHint="#color/bg_default"
android:listSelector="#drawable/list_selector_background"
android:id="#+id/frag_books_grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchMode="spacingWidth"
android:scrollbars="none"/>
</LinearLayout>
Single item:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:cacheColorHint="#color/bg_default"
android:scrollbars="none"
android:listSelector="#drawable/list_selector_background"
android:layout_gravity="center_vertical|left"
android:gravity="center_vertical|left"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:id="#+id/frag_book_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="5dp"/>
</LinearLayout>
Problem is that there is no left and right padding of single item. And it looks terrible when item is selected: http://i.imgur.com/rirNP.png
Please, help to fix it.
try it
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/menu_category"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
for information
http://developer.android.com/guide/topics/ui/layout/gridview.html
this one is best for three columns..
<GridView
android:id="#+id/gridview_Gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="65dp"
android:gravity="center"
android:horizontalSpacing="6dp"
android:listSelector="#00000000"
android:numColumns="3"
android:scrollbars="none"
android:stretchMode="columnWidth"
android:verticalSpacing="-1dp" />

Categories

Resources