I am trying to create a grid layout of cards similar to what is used in google play like this
The layout i am using and the layout obtained are these:
For GridView:
<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"
tools:context=".MainActivity"
android:background="#e5e5e5"
>
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="60dp"
android:numColumns="2"
android:layout_marginLeft="2dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</RelativeLayout>
for GridItem:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/bg_card"
>
<ImageView
android:id="#+id/imgIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="#+id/txtTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:textSize="17sp"
android:layout_below="#id/imgIcon"
android:layout_marginTop="35dp"
android:layout_centerInParent="true"/>
</RelativeLayout>
The output that I get is this:
The image size that i am using is 356x500. (I do get a somewhat well sized grid on using 300x300)
The issues that i am facing are:
1.As this image shows, the cards are too big with lots of white spaces around the image and the image does not fit like in the
google play store. How do i make the card be propotional to the
image. The amount of white space changes with change in image size.
The card is "stuck" to the left edge of the screen . How do i keep spacing between the edge of the screen and the card.
Please help me in rectifying this issue.
Thanks
Space are bacause you used
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
Also edit your
<ImageView
android:id="#+id/imgIcon"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
in gridview use
android:stretchMode="none" instead of android:stretchMode="columnWidth"
Put android:scaleType="fitStart" inside your
Use imgIcon.setAdjustViewBounds(true);
You can also do the same in the xml
Related
For some reason, my CardView is only showing the first row of images in my grid view. (or my GridView is only showing the first row - I can't tell).
I am dynamically adding images into the gridView and using gridAdapter.notifyDataSetChanged(). Perhaps this is why the height of the CardView is not adapting? When I add android:layout_height="500dp" to the CardView, then I see all my images.
Is there a way to make the CardView wrap the content so I can see all of my images (other than hardcoding the height)?
Please note, I cannot hardcode the height as the number of rows of photos is dynamic.
Only showing one row of images, even when there are actually 2 rows of images
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="2dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/m_photos" />
<GridView
android:id="#+id/m_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:clickable="true"
android:columnWidth="90dp"
android:drawSelectorOnTop="true"
android:focusable="true"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="none"
android:verticalSpacing="2dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
I faced This problem few days ago.For this case, Wrap_content not working perfectly.So Use layout height in fixed dp and use dimention for that. it works for my case.
<GridView
android:id="#+id/m_grid"
android:layout_width="match_parent"
android:layout_height="#dimen/gvHeight"
android:layout_margin="2dp"
android:clickable="true"
android:columnWidth="90dp"
android:drawSelectorOnTop="true"
android:focusable="true"
android:gravity="center"
android:numColumns="auto_fit"
android:stretchMode="none"
android:verticalSpacing="2dp" />
I am currently trying to have a GridView only show pictures with text underneath with 1dp space in between. However, because of the text I am not sure how to fill up the gridview cells completely, and I know maybe since I put a dimension for the height and width of the image items that it does not show filled but how do I fill it?
GridView:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/categoryGridView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth"
android:layout_marginTop="30dp">
</GridView>
GridCell xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:descendantFocusability="blocksDescendants">
<ImageView
android:id="#+id/categoryImageViewItem"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/ic_launcher"
android:background="#drawable/com_facebook_button_blue" />
<TextView
android:id="#+id/categoryTextViewItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/categoryImageViewItem"
android:layout_centerHorizontal="true"
android:gravity="center"/>
</RelativeLayout>
how do I actually have the picture fill up everything but leave space for the text programmatically or in the xml
I'm trying to show a list of images in a GridView, and I want to crop only the center of the images in this list. So below is the layout of each image item:
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/place_image"
android:src="#drawable/ic_res"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="centerCrop"/>
And here is the layout that contains the grid view:
<?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" >
<LinearLayout android:id="#+id/upload_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#f6f6f6"
android:padding="10dp"
android:visibility="gone">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton android:id="#+id/photo_select"
android:layout_width="50dp"
android:layout_height="40dp"
android:scaleType="fitXY"
android:src="#drawable/ic_photo"/>
<EditText android:id="#+id/photo_caption"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/caption"
android:inputType="textMultiLine"/>
</LinearLayout>
<TextView android:id="#+id/photo_path"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<Button android:id="#+id/photo_upload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/upload_photo"
android:textStyle="bold"
android:textColor="#ffffff"
android:layout_gravity="right"/>
</LinearLayout>
<TextView android:id="#+id/no_photos"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:gravity="center"
android:textStyle="bold"
android:textSize="20dp"
android:text="#string/no_photos"
android:visibility="gone" />
<GridView android:id="#+id/grid_photos"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:columnWidth="120dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center" />
</LinearLayout>
And here is the result I got:
This is weird because the images are all cropped in a strange way like that. Below is one of my original images:
I've never encountered this problem so if any of you are familiar with this, please help me find the reason of this. Thank you!
P.s: I guess this can only be the problem of the xml layout so I don't think it is necessary to include my android source code here. But if it really is, please let me know where the problem may come from and I'll edit this question with my code.
The reason is GridView clears all the layout params set for the tiles and it set its own layout params so better you add your ImageView inside a RelativeLayout. This is the same with all the AdapterView like ListView,Gallery etc.
I have a gridview, but it was always lined up two columns, I wish it was always lined up two columns.
This is grid layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/newsScroller"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F5F5F5" >
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/newsScrollerContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp" >
</GridView>
</ScrollView>
And this is item in grid.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="5dip"
android:layout_gravity="center" >
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="130dip"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="#drawable/icon" >
</ImageView>
<TextView
android:id="#+id/grid_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/grid_item_image"
android:layout_centerHorizontal="true"
android:layout_marginTop="2dp"
android:gravity="center"
android:shadowColor="#ffffff"
android:shadowDx="1"
android:shadowDy="2"
android:shadowRadius="2"
android:textColor="#333333"
android:textStyle="bold"
android:textSize="14sp" >
</TextView>
</RelativeLayout>
My problem, when I use the device nexus runs perfectly, but if I use a smaller screen devices will be just show one coloumn, as shown below:
And if i use the tablet, so will be five coloumn.
My question is that how it's always a two-column grid when i open in device anything or any screen size.
Thanks.
Instead android:numColumns="auto_fit" just use : android:numColumns="2"
In each grid item replace fill_parent with wrap_content.
A more complicated solution will be to calculate the screen width in code and then set the item width by code also.
You need to change the android:numColumns to 2, not auto_fit
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/newsScrollerContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:verticalSpacing="10dp" >
You can set the number of columns by using this property android:numColumns=2 of grid view in your xml or using this method of grid view setNumColumns(2).Dont use auto fit.
I had created a window with RelativeLayout and insterted two controls in it; one is GridView and the other one is WebView.
I will bind images to gridview to use like menu and i will load a html page in webview for ads.
But i have problems with these controls.
For example, if user scrolls gridview vertically it is disappearing behind webview however, in that i was expecting webview was slide with gridview too when user scroll gridview.
So i wonder, can i group these controls together to act simultaneously?
My another problem with orientation, when i turn my device to landscape mode webview is disappearing from screen.
Lastly if i set GridView's layout_height attribute fill_parent, i can't see WebView, so i set it as wrap_content. But this time WebView doesn't align at bottom, it aligns at GridView's after and there is space between screen bottom and webview.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg480480">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
android:verticalSpacing="45dp"
android:horizontalSpacing="10dp"
android:columnWidth="150dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_marginTop="30dp"
android:scrollbars="none"
android:layout_alignParentTop="true" />
<WebView
android:id="#+id/webView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:gravity="bottom"
android:layout_below="#id/gridview" />
</RelativeLayout>
Here are some screen shots.
I guess the main problem is that both the GridView and the Webview specify layout_height="fill_parent". I guess you probably don't want the GridView to fill the whole parent area, so use "wrap_content" instead.
I don't know enough of the desired layout of the page but I would recommend using a LinearLayout to start and get comfortable. It really depends what the grid view and the web view should show and where your main content is.
In the example below I have simply assumed that your web view wants to take up the majority of the screen.
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff0000"
android:numColumns="auto_fit"
android:verticalSpacing="45dp"
android:horizontalSpacing="10dp"
android:columnWidth="150dp"
android:stretchMode="columnWidth"
android:layout_marginTop="30dp"
android:scrollbars="none"
/>
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00"
android:scaleType="centerInside" />
</LinearLayout>
If the assumption is not correct then you would probably specify it the other way round (Webview=>wrap_conent)
Improve the WebView layout code as below:
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/webView1"
android:layout_alignParentTop="true"
android:layout_marginTop="30dp"
android:columnWidth="150dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:scrollbars="none"
android:stretchMode="columnWidth"
android:verticalSpacing="45dp" />
<WebView
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="bottom"
android:scaleType="centerInside" />
FYI, android:layout_alignParentBottom="true" will display WebView at the bottom of the screen. and include android:layout_above="#+id/webView1" inside the GridView.
Give a try to this layout code.
You need to add
android:layout_above="#+id/webView1"
in your gridview
also
android:layout_alignParentBottom="true"
in your webview
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bg480480"
android:orientation="vertical"
>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numColumns="auto_fit"
android:verticalSpacing="45dp"
android:horizontalSpacing="10dp"
android:columnWidth="150dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_above="#+id/webView1"
android:layout_marginTop="30dp"
android:scrollbars="none"
android:layout_alignParentTop="true" />
<WebView
android:id="#+id/webView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#id/gridview" />
</RelativeLayout>