How to order elements like this? - android

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>

Related

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.

Gridview if single item means it will place in to center

I need this layout i have done every thing but the First Gridview is having only one item i need to align it to center but it can,t it will like
I need this item 1 to center
here is my code
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/gridview_desc"
android:layout_margin="20dp"
android:gravity="center"
android:horizontalSpacing="0dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="0dp"
android:minWidth="180dp"
android:listSelector="#00000000"
>
</GridView>
any one please help.....
Use ListView instead, and create item layout RelativeLayout and put a LinearLayout inside it as centered. And in code you need to calculate real positions of every item that you put and you can set position as a tag, and set onClickListener to handle with click event. You may want to remove listSelector as well.

Set GridView in center horizontal

I have an application in which I am using a GridView to show some image of different number. I want to set the GridView in the center of screen horizontally that is to be run perfect on every phone of different sizes or density pixels.
Thanks in advance.
code
<GridView
android:id="#+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:horizontalSpacing="5dp"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp" >
</GridView>
your 'GridView' must be in some parent layout for example Linearlayout.Set following two properties of that parent layout
android:layout_gravity="center"
android:gravity="center"
Note:You must use both attributes at the same

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

Ninepatch Background shrinks display area of GridView

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.

Categories

Resources