Ninepatch Background shrinks display area of GridView - android

I've got a basic GridView that I've set up as follows:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MainMenuGridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
Everything works fine until I add a ninepatch background. Once I add the background image like this:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MainMenuGridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:background="#drawable/mainbackground"
/>
It breaks my gridview. Instead of displaying 3-4 columns (depending on screen resolution and orientation) all icons in the gridview display in a single column in the upper left corner. Furthermore, only two at a time are displayed and I have to scroll down to see the rest. I suspect that the gridview is being bound by the dimensions of the ninpatch's center logo, but I'm not sure.
My ninepatch has a small area in the middle with a logo that I want to be displayed without any skewing. The rest of the background is a small gradient that takes up the remainder of the screen. Any ideas on what's causing this?

Sounds like you have padding defined (the lines at the bottom and right of the ninepatch)? Try getting rid of those, so you only have content area defined (the lines at the top and left)

I had a similar problem. Unfortunately I want to use padding in my case. What I found is that the order of method calls on gridLayout seem to make a difference. As long as I called setPadding() after setBackgroundResource(), all is fine.

Related

Android - grid item - drag and drop - want to remove item highlight

I want to display circular images in grid and also a feature to drag and drop the images to reposition the icons. Also I want to add circular images dynamically. So I used grid view and drag and drop framework in android (along with onItemLongClick event listener). Everything works fine. I just dont want the highlight of an item during onItemLongClick. It highlights an item creating a rectangular shadow (This is not the shadow builder as part of the drag and drop framework) behind the circular image. Is it possible to remove that highlight?
Below is the Grid item layout xml.
<?xml version="1.0" encoding="utf-8"?>
<com.steth.CircleImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="45dp"
android:layout_height="45dp"
android:id="#+id/userImg"
/>
Below is the GridView xml.
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_margin="8dp"
android:layout_weight="1" />
Add to your GridView:
android:listSelector="#android:color/transparent"

How to order elements like this?

I'm trying to put inside a LinearLayout Horizontal some buttons, but I have the next result.
When they get to the right of the screen, they are not sorted under, they go on. I want that if the screen is small, they are ordered as follows.
Without cut any button.
Thank you. If you need my XML, tell me.
Try setting the LinearLayout width as match parent
android:layout_width="match_parent"
android:layout_height="wrap_content"
Hope it helps :)
Update:
Use GridView and set num_coulums to auto_fit and keep adding buttons as you desire,
set width & height as per your requirements
<GridView
android:id="#+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="100dp"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" >
</GridView>

GridView Layout android:horizontalSpacing is not working

I am using android:horizontalSpacing attribute in my GridView layout to give horizontal spacing between two columns. But nothing getting change only it give space between the vertical columns.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:stretchMode="columnWidth"
android:numColumns="7" />
</FrameLayout>
This is the above code but i don't understand what is the actual mistake i am doing. You can see in above picture that , there are only black vertical lines but i also want horizontal black lines.
Please help me out , suggest me some solution.

Which stretchMode to use in GridView on android

Following is my xml code:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridviewgallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="#dimen/gridview_column_width"
android:numColumns="auto_fit"
android:verticalSpacing="0dp"
android:horizontalSpacing="0dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:background="#444444"
/>
When I use android:stretchMode="columnWidth" , it creates a gap between the columns(though it fills the screen width)
And if I use android:stretchMode="none" , it gets left aligned and there is a gap left on right side of screen(though now there is no gap between columns)
Problem is I want both : To fill the screen width as well no gap between columns.
How to achieve it?
A combination of android:numColumns="auto_fit" android:stretchMode="columnWidth" and android:columnWidth="60dp" will give you a layout that adjusts for different screen sizes. Much better than fixing the number of columns. See example below...
<GridView
android:id="#+id/tablegrid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffff7965"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:gravity="center"
android:columnWidth="60dp"
/>
It's very simple dude, just remove the line
android:columnWidth="#dimen/gridview_column_width"
you can specify any number of columns, i am given number of column = 4
android:numColumns = "4"
This is working fine for me
I know this question is a little old but here is an answer I found hoping this helps someone else, just for android:stretchMode use spacingWidthUniform as it says in documentation here it will stretch the spacing between columns equally.
please refer to above link for more information.

Is it possible to set borders to grid view in android

Is it possible to set borders to grid view in xml. Like, i want each grid cell to contain borders. If so, kindly let me know how to do it. I have set this way. But i am not finding any specific thing to set border.
<GridView
android:layout_marginTop="95px"
android:layout_marginLeft="32px"
android:id="#+id/gridview"
android:layout_width="340px"
android:layout_height="250px"
android:columnWidth="35px"
android:numColumns="7"
android:scrollbars="none"
android:verticalSpacing="14px"
android:horizontalSpacing="18px"
android:stretchMode="columnWidth"
android:gravity="center_horizontal"
android:background="#color/white">
</GridView>
Thank you
Swathi

Categories

Resources