I'm try to create a GridView where the values are as close as possible to each other.
Now the content is spread out all over the screen.
How can I reduce the spacing between the cells of the GridView?
Using the setVerticalSpacing() method didn't work.
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".LevelSpelen" >
<GridView
android:id="#+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignLeft="#+id/textView1">
</GridView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/gridView1"
android:layout_marginLeft="18dp"
android:text="" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginLeft="78dp"
android:layout_toRightOf="#+id/textView1"
android:text="Button" />
</RelativeLayout>
Any suggestions?
The spacing is defined in 2 ways: the spacing between cells, and the spacing within a cell
In your GridView definitions add
android:verticalSpacing="8dp"
android:horizontalSpacing="8dp"
or smaller and add android:numColumns="auto_fit" to make sure that you can fit as many items as possible in each row.
In the GridView Item layout, make sure that you make the cell as small or as big as you want
Related
Selection Activity
Selection Activity
What i which to accomplish by using IMAGES inside a GridView is to allow the user to decide from ALL MY options which ones is he willing to do, the challenge throughout the whole process of creating this activity, has been trying to make it possible to fit everything in one screen with a ScrollView while maintaining the pictures.
In the images when the user has selected something it will be more opague (in the pictures is complete black).
I have been searching all over the place, and this is what has helped me so far:
- ScrollView Layout does not fill the whole screen
- Gridview height gets cut
If any of you know a work around or other viable alternative, please dont hesitate to indulge me. I can also post my activity class, but i feel is redundant.
<ScrollView
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:fillViewport="true">
<RelativeLayout
android:id="#+id/activity_woman_activities"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="tech.destinum.baewatch.WomanActivities">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/titleIndoorActivities"
android:text="Indoor Activities"
android:gravity="center"
android:textSize="24sp"/>
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/gridviewWomanActivities"
android:layout_below="#id/titleIndoorActivities"
android:numColumns="2"
android:horizontalSpacing="8dp"
android:verticalSpacing="8dp"
android:gravity="center"
android:stretchMode="columnWidth"
android:layout_marginTop="10dp"
android:minHeight="20dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/titleOutdoorActivities"
android:text="Outdoor Activities"
android:gravity="center"
android:textSize="24sp"
android:layout_below="#+id/gridviewWomanActivities"
android:layout_alignParentStart="true"
android:layout_marginTop="18dp"/>
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/gridviewWomanActivities2"
android:numColumns="2"
android:horizontalSpacing="8dp"
android:verticalSpacing="8dp"
android:gravity="center"
android:stretchMode="columnWidth"
android:minHeight="20dp"
android:layout_below="#+id/titleOutdoorActivities"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp"/>
<Button
android:text="be spontaneous!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/gridviewWomanActivities2"
android:layout_centerHorizontal="true"
android:id="#+id/button5"
android:layout_marginTop="10dp"/>
</RelativeLayout>
</ScrollView>
just working on a simple layout and I was wondering if there was a way to automatically re-size a gridview?
My vertical rotation view looks like:
But my horizontal rotation view looks like:
The code for my layout at the moment is:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="baegmon.mesmerize.GameActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Timer"
android:id="#+id/textView_Timer"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Score"
android:id="#+id/textView_Score"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Question"
android:layout_weight="1"
android:id="#+id/textView_Question"
android:layout_below="#+id/textView_Timer"
android:layout_centerHorizontal="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/textView_Question"
android:layout_alignParentBottom="true">
<GridView
android:id="#+id/gridView_Game"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:columnWidth="100dp"
android:drawSelectorOnTop="true"
android:gravity="center"
android:numColumns="5"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"
android:clickable="true"
android:layout_centerInParent = "true"
/>
</RelativeLayout>
Is there any way to automatically resize in the Horizontal View so that all of the GridView fits inside of the layout? Any help would be appreciated.
Thank You.
Problem not in GridView size but in GridView cell size.
All cells cannot to fit into screen because of height of every cell is more then needed to fit by height of GridView. Thats why GridView makes your content scrollable.
Just try to change cell size.
Futhermore your cells scretches by width because of android:stretchMode="columnWidth", this is the reason why in second case you have big horizontal spacing between cells.
Try to delete or replace that line.
I am trying to show three different vertical sections in my Android layout on one screen, each taking up one third of the screen, one on the top, one in the middle, and one on the bottom. Each section has a TextView and a ListView, and the ListViews can scroll so that you can see all items, but the overall page does not move. I have tried putting each TextView and ListView in a LinearLayout and setting the height of each LinearLayout to one third the total height of the screen, but the first ListView just shows all the items in it, taking up most of the screen, and the other sections are pushed down. I also tried using layout_weights, but for some reason it did not work. (EDIT: Setting layout_weight="1" ended up working, I'm not sure what I did wrong the first time) How can I make this work, or is there a better way to go about this?
<LinearLayout
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="horizontal" >
<ListView android:layout_width="0dp" android:layout_weight="1"
android:layout_height="match_parent" android:background="#FF0000"/>
<ListView android:layout_width="0dp" android:layout_weight="1"
android:layout_height="match_parent" android:background="#00FF00">
<ListView android:layout_width="0dp" android:layout_weight="1"
android:layout_height="match_parent" android:background="#0000FF">
</LinearLayout>
This will give you three equally wide columns: to make it rows, change the orientation to vertical and swap the values of layout_height and layout_width for each listview. If you need to add the TextViews, you'll have to make each listview in this code either a RelativeLayout LinearLayout or FrameLayout, using same width/height/weight and arranging the ListView and TextView inside to taste. To do this most efficiently, use a Framelayout and use a margin on the listview to offset it from the TextView. You can place the TextView relative to the ListView inside the FrameLayout by using the layout_gravity in the TextView.
Ie (swapping the first "column"):
<FrameLayout android:orientation="vertical" android:layout_width="0dp"
android:layout_weight="1" android:layout_height="match_parent"
android:background="#FF0000">
<TextView android:text="Column!" android:background="#3Eff0000"
android:layout_height="40dp" android:layout_width="match_parent">
<ListView android:layout_marginTop="48dp" android:layout_height="match_parent"
android:layout_width="match_parent" android:background="#8Aff0000"/>
</FrameLayout>
Use this:
<?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="horizontal"
android:weightSum="3">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ff89ff91">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#1"
android:id="#+id/textView"
android:padding="5dp"
android:gravity="center" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_below="#+id/textView" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffff8177">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#2"
android:id="#+id/textView2"
android:padding="5dp"
android:gravity="center" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView2"
android:layout_below="#+id/textView2" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#ffffe85d">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#3"
android:id="#+id/textView3"
android:padding="5dp"
android:gravity="center" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView3"
android:layout_below="#+id/textView3" />
</RelativeLayout>
</LinearLayout>
I have a view in which i'd like a button to be placed below a grid view and fill up the remaining space. The view is a relative layout, and no matter what attributes I give it, if I some how relate the button to the grid view, it disappears. It only appears if I place it in the relative view with it aligned parent bottom, but then it doesn't stretch. The solution I thought would be most likely, though it didn't work either, was to place the button in the relative view, alignParentBottom=True and then layout_below="id/gridview" (all that is psuedo code). This is my code right now:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/pin_fragment_background"
tools:context="com.lab125.viva_app.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/audioPIN_marquee"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="15dp"
android:paddingBottom="30dip"
android:gravity="center"
android:textSize="15sp"
android:textStyle="bold"
/>
<LinearLayout
android:id="#+id/keypad_wrapper"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_below="#id/audioPIN_marquee">
<GridView
android:id="#+id/keypad"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="5dip"
android:verticalSpacing="5dip"
android:numColumns="3"
android:stretchMode="columnWidth" />
<Button
android:id="#+id/start_ver_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textStyle="bold"
android:text="#string/Start_Ver_Button"
android:background="#drawable/button_style" />
</LinearLayout>
<Button
android:id="#+id/audio_playback_btn"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/keypad_wrapper"
android:text="play"
android:textSize="15sp">
</Button>
</RelativeLayout>
Again, with this code, the button does not show at all.
Edit: #Dave's suggestion was correct, and I could see the button after setting the orientation to vertical. However, the only reason I had placed the gridView in a linear layout in the first place was in attempt to just not have the button disappear when having its android:layout_below property to the gridView (by setting its android:layout_below to something else). The more accurate fix for what I wanted to do however, was to not have the gridView's width and height set to match_parent, which was the root cause behind why the button was disappearing. So, i set the gridView's height to wrap content, but that caused the grid to shrink to the point where each grid element's content was no longer visible. I found that the cause of that was => I was programmatically setting the width and height of the gridview to wrap its content, and then the width and height of the components inside by the width and height of the parent view. Realized that was quite cyclical which may be why the gridview was shrinking when I set it to wrap content. I then set the width and height of the grid Elements based on the dimensions of the screen, which fixed the size of the gridView. So everything pretty much works now.
Here is my new layout, if anyone is interested.
<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:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/pin_fragment_background"
tools:context="com.lab125.viva_app.MainActivity$PlaceholderFragment" >
<TextView
android:id="#+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/audioPIN_marquee"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="20dp"
android:paddingTop="15dp"
android:gravity="center"
android:textSize="15sp"
android:textStyle="bold"
/>
<GridView
android:id="#+id/keypad"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/audioPIN_marquee"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="5dip"
android:verticalSpacing="5dip"
android:numColumns="3"
android:stretchMode="columnWidth" />
<Button
android:id="#+id/start_ver_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/keypad"
android:layout_alignParentBottom="true"
android:layout_marginTop="20dp"
android:textSize="15sp"
android:textStyle="bold"
android:text="#string/Start_Ver_Button"
android:background="#drawable/button_style" />
<!--
<Button
android:id="#+id/audio_playback_btn"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/keypad_wrapper"
android:text="play"
android:textSize="15sp">
</Button> -->
</RelativeLayout>
start_ver_btn won't show because the gridview has match_parent for both width and height, judging by your layout_width and heights, I think you want to set the LinearLayout's orientation to vertical.
I tried many times to draw 2 Relative layout aligned horizontally and divided in half screen.
I design the image with paint to explain a bit better what i mean.
Any suggestion?
Another way to accomplish the same task without needing to use a LinearLayout is to put a center-aligned "shim" in the middle of the parent layout, then align other elements to it. If you set the half-width element's width to match_parent, but align both their left and right sides, they'll end up shrinking themselves to fit.
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.EqualWidthExample" >
<!-- An invisible view aligned to the center of the parent. Allows other
views to be arranged on either side -->
<View
android:id="#+id/centerShim"
android:layout_height="match_parent"
android:layout_width="0dp"
android:visibility="invisible"
android:layout_centerHorizontal="true"/>
<!--Set width to match_parent sets maximum width. alignParentLeft aligns
the left edge of this view with the left edge of its parent. toLeftOf
sets the right edge of this view to align with the left edge of the
given view. The result of all three settings is that this view will
always take up exactly half of the width of its parent, however wide
that may be. -->
<Button
android:id="#+id/btLeft"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/centerShim"
android:text="Left Button" />
<!--Same deal, but on the right -->
<Button
android:id="#+id/btRight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/centerShim"
android:layout_below="#+id/tvLeft"
android:text="Right Button" />
</RelativeLayout>
You can put these 2 RelativeLayouts inside a LinearLayout with horizontal orientation, then use the weight for both RelativeLayouts. This will divide the LinearLayout in 2 equal parts.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
</RelativeLayout>
</LinearLayout>
now you can do it easily using PercentRelativeLayout
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<Button
android:id="#+id/button"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Button"
app:layout_widthPercent="50%"/>
<Button
android:id="#+id/button2"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/button"
android:text="Button 2"
app:layout_widthPercent="50%"/>
</android.support.percent.PercentRelativeLayout>
don't forget to add the gradle dependency
dependencies {
compile 'com.android.support:percent:25.3.1'
}
code in github
Update
PercentRelativeLayout is Deprecated since API level 26.0.0
You could use an non-recommended structure with nested wieghts
<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:orientation="vertical"
android:baselineAligned="false"
android:weightSum="5">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:layout_gravity="center">
<TextView
android:id="#+id/TopTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Top Text View"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"/>
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:weightSum="2"
android:layout_weight="4">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<TextView
android:id="#+id/LeftTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Left Text View"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<TextView
android:id="#+id/RightTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Right Text View"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
I see 4 relativelayouts in your drawing...?
If you mean the two in the middle put them into one LinearLayout (horizontal orientation), let all of them have a width of match_parent and give both relative layouts a weight of 1