I want my main menu be a GridView of icons - 6 icons to be specific.
When the phone is in the portrait layout i want them to be in 2 columns, when it is in horizontal layout - 3 columns --- have sorted that out.
What I didnt sort out is how to make them occupy merely the whole screen.
Fitting it to the screen width is not a problem - it is fitting it to the screen height that makes me stumble.
I want it to stretch/shrink my cells depending on the screen size. I have tried counting screen height and dividing it by 3 - but there are certain problems and I dont think that that is the most elegant solution.
So basically how do I occupy my screen (say in a portrait layout) with a gridview of two columns (3 rows) solely? Are there any xml attributes that could help me?
This is my xml file:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:gravity="center"
/>
I suggest creating the two layouts in xml first, then during the onCreate method you can do the re-sizing of the rows and columns.
For example
if layout is portrait
set column width = screen width / 2
else if layout is landscape
set column width = screen width / 3
Android Dashboard Pattern the answer to my question is somewhere there. Basically I used nested ListViews
Related
I have images that I want to render in a GridView. These images are all the same size (250x250). However, I want to display them in 100x100 squares in the GridView.
<GridView
android:id="#+id/sectionListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="100dp"
android:stretchMode="none"
>
</GridView >
The above is the grid view. Where I am setting the columnWidth to 100. This actually works but for some reason, the height is always 120, leaving horizontal black bars at the top and bottom of my 250px image. Somehow there are getting 10 pixels on the top and bottom. Changing the vertical and horizontal spacing doesn't help because that affects the spacing between the elements. These black bars on top and bottom are inside the gridView item.
I would look into the ImageView which I assume you would be using in your adapter to show the images. You should set android:layout_width and android:layout_height to 100dp and make sure that android:scaleType is set to centerCrop.
In the CustomAdapter
view.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, rowHigh));
rowHigh is the dimension you must to change
If you are using gridview, then I think you are also using another item_layout for displaying each item, I'm guessing it is an ImageView as you are trying to display images. Hence you can limit the size of the items to 100 x 100 in that item layout(ie, android:layout_width="100dp" android:layout_height="100dp"in the item_layout.) Hope it helps.
I want to display 2 images in a Row in GridView.
I have tried to get the screen size in different ways. For example:
final int h = getResources().getDisplayMetrics().densityDpi;
and set size of the image..
convertView.setLayoutParams(new GridView.LayoutParams(h, h));
But this is not sufficent. If screen is large, then 2 images look very small - like peanut, on a big screen.
Can anyone guide me that for example. Device's screen size is 4 inches in Portrait and I can create 2 images of almost 2 inches each.
here is my gridview xml file
<GridView
android:id="#+id/grid_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:alwaysDrawnWithCache="true"
android:clipChildren="true"
android:gravity="center_horizontal"
android:horizontalSpacing="2dp"
android:verticalSpacing="5dp"
android:numColumns="auto_fit"
android:padding="0dp"
android:scrollingCache="true"
android:smoothScrollbar="true"
android:stretchMode="columnWidth"
>
</GridView>
add these attributes to your grid
android:columnWidth="200dp"
android:stretchMode="columnWidth"
It sounds like you're trying to change the images size based on the screen size, if so you should take a look at the supporting multiple screen sizes tutorial. Android already offers that functionality.
Also if you're only displaying two images horizontally you could use a LinearLayout instead, using a GridView is overkill.
Just set the number of columns of the GridView in XML.
To make equal sized images, make the ImageView like this SquaredImageView
and if you want Layout to be of equal size then follow the same method describe on onMeasure of SquaredImageView
I've tried layout_width attribute to match_parent as well as fill_parent on the root element of layout file, for a home screen widget. But when shown in App Widget, it's still being wrapped around the content.
App widget layout:
<StackView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/xxxxxxxxx"
android:layout_width="match_parent" <!-- always acts like wrap_content -->
android:layout_height="fill_parent"
android:gravity="center"
android:loopViews="true" />
Widget is assigned grid size of 4X2:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="250dp"
android:minHeight="110dp"
. . . . . />
But still after being added to a home screen, the widget's width doesn't take up 4 grids on home screen, it just wraps around the content. How can this be solved ?
Having a similar problem, it appears minHeight and minWidth factor into the max size as well, I bet if you increased the minWidth to (4 * 74) - 2 = 294dp it'd work. For your minHeight, the pixel height per the guide would be (2 * 74) - 2 = 146dp, just so you know.
i'm using gridview in android and i want to set the first column's width but i could not. while i am trying to find the answer to the problem i came face to face with a comment as you can see below. My question is that is description below really true?
All columns in a GridView have the same size.
On Sat, May 23, 2009 at 9:25 PM, solid wrote:
I am having trouble controlling the size of the columns in my
GridView. The first column is only 3-4 chars and the other 4 columns
are 5-8 chars. For some reason, all the columns are the same width,
leaving a huge amount of spacing after the first column and wrapping
the text inside the others. Is there a way, I can set the width of
individual columns in the grid view? In an ideal world, gridview
would work like an html table, where the smaller columns are shrunk to
fit the data and larger columns expand as necessary. Any ideas would
be helpful.
You can directly write in your layout resource file.
android:columnWidth="100dp"
https://developer.android.com/guide/topics/ui/layout/gridview.html
try this may this helps you
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
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"
/>
im trying to get images displayed in a GridView and get the columns automatically set, so far I've had to manually set the number of columns which is not what i want to do as this will affect how big the images are on different sized screens. I ahve tried setting it to auto_fit but it only displays 2 columns in the middle of the screen. This is what im trying to achieve: (Each red square represents an image)
then when turned to landscape mode i want the columns to auto fit so that its all even.
Any help would be much appreciated, thank you :)
Experiment with the GridView attributes, specifically android:numColumns="auto_fit" and android:stretchMode. The following works for me:
<GridView
android:id="#+id/myGrid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:columnWidth="60dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
I'm not sure why someone voted down the answer above me (so I voted it up again): I had to solve a similar problem programatically (with a GridView of ImageView children). Some code removed for clarity:
int iDisplayWidth = getResources().getDisplayMetrics().widthPixels ;
iImageWidth = iDisplayWidth
/ iNumberOfColumns ;
gridview.setColumnWidth( iImageWidth );
gridview.setStretchMode( GridView.NO_STRETCH ) ;
/* wrap_content in the xml file is supposed to do this, but it didn't seem to work */
Best regards,
Piesia
A better solution is to measure the width programmatically, so you won't end up with a hard-coded column width:
Android: How does GridView auto_fit find the number of columns?