I want to add a button widget just above the GridView,But it seems not happy with my code, i mean its not showing up on the screen, no matter what i am doing,by the way am on 7.1.1, here is my layout,
<?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">
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/images_grid_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:columnWidth="180dp"
android:gravity="center"
android:horizontalSpacing="8dp"
android:numColumns="3"
android:padding="16dp"
android:stretchMode="columnWidth"
android:verticalSpacing="8dp">
</GridView>
<Button
android:text="Next"
android:id="#+id/next_Button"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>
So this is it, layout is insanely simple, but i can not figure it out, any help would be highly appreciable !
Use this:
<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/next_Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Next" />
<GridView
android:id="#+id/images_grid_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/next_Button"
android:columnWidth="180dp"
android:gravity="center_horizontal"
android:horizontalSpacing="8dp"
android:numColumns="3"
android:padding="16dp"
android:stretchMode="columnWidth"
android:verticalSpacing="8dp">
</GridView>
</RelativeLayout>
Use RelativeLayout to let the Button stick to the parent's bottom, and set the GridView to stay above the button
You are trying to use Linear layout weight attribute.If you are trying to use it grid layout layout height attribute should also be 0dp and you should add layout weight attribute for grid layout too.
<?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">
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/images_grid_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6"
android:adjustViewBounds="true"
android:columnWidth="180dp"
android:gravity="center"
android:horizontalSpacing="8dp"
android:numColumns="3"
android:padding="16dp"
android:stretchMode="columnWidth"
android:verticalSpacing="8dp">
</GridView>
<Button
android:text="Next"
android:id="#+id/next_Button"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>
Hope it helps u.
Related
I am working on Xamarin app but I believe this concept also applies for android.
I would like to show my LinearLayout wrapped items in GridView as below. But is does not render as I expected.
Is there anything I am missing here?
Code:
Main.axml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<GridView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/gridViewHomeItems"
android:horizontalSpacing="10px"
android:verticalSpacing="10px"
/>
HomeItem.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="25px"
android:minHeight="25px"
android:padding="25px">
<ImageView
android:src="#android:drawable/ic_menu_gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/homeItemImage" />
<TextView
android:text="Item Title"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/homeItemTitle" />
</LinearLayout>
Remove the px from properties. Use sp or dp instead. Same thing apply to the Grid View layout also.
Like this.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="25dp"
android:minWidth="25dp"
android:orientation="vertical"
android:padding="25dp">
<ImageView
android:id="#+id/homeItemImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#android:drawable/ic_btn_speak_now" />
<TextView
android:id="#+id/homeItemTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Item Title"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
Grid View like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minHeight="25dp"
android:minWidth="25dp"
android:orientation="vertical">
<GridView
android:id="#+id/gridViewHomeItems"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="96dp"
android:horizontalSpacing="10dp"
android:minHeight="25dp"
android:minWidth="25dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp" />
</LinearLayout>
Try adding this atributes. Main.axml#GridView:
android:columnWidth="96dp"
android:numColumns="auto_fit"
HomeItem.axml (#ImageView and #TextView):
android:layout_centerHorizontal="true"
Try the below code in your layout.
<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="match_parent"
android:orientation="vertical"
>
<GridView
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="100dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/gridViewHomeItems"
/>
</LinearLayout>
Change your custom layout as below:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="25dp"
android:minWidth="25dp"
android:orientation="vertical"
android:padding="10dp">
<ImageView
android:id="#+id/homeItemImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#android:drawable/ic_menu_gallery"/>
<TextView
android:id="#+id/homeItemTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Item Title"
android:textAppearance="?android:attr/textAppearanceMedium"/>
</LinearLayout>
Also make sure you use dp instead of px as per android standards.
I wish to achieve a layout like this one that can dynamically display albums and change the number of columns according to orientation (vertical or horizontal). Here is an example of what I am trying to achieve:
As of now my code looks something like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="160dp"
android:layout_height="200dp"
android:layout_gravity="top|left"
android:layout_weight="1"
android:layout_margin="8dp"
app:cardCornerRadius="0dp"
app:elevation="5dp">
<ImageView
android:id="#+id/avatar"
android:layout_width="160dp"
android:layout_height="160dp"
android:layout_gravity="top|center"
android:scaleType="centerCrop"
android:src="#drawable/wp" />
<TextView
android:id="#+id/album_name"
android:layout_width="match_parent"
android:layout_height="25dp"
android:alpha="0.85"
android:background="#color/light"
android:layout_above="#+id/detail"
android:layout_below="#+id/avatar"
android:gravity="bottom|center"
android:layout_alignParentBottom="true"
android:text="Test"
android:textColor="#color/cardview_dark_background" />
<TextView
android:id="#+id/detail"
android:layout_width="match_parent"
android:layout_height="15dp"
android:layout_gravity="bottom|center"
android:alpha="0.85"
android:background="#color/light"
android:gravity="bottom|center"
android:layout_alignParentBottom="true"
android:layout_below="#+id/album_name"
android:text="Test"
android:textColor="#color/dim_foreground_disabled_material_dark" />
</android.support.v7.widget.CardView>
</LinearLayout>
It's a GridView. Inside GridView you can set CardView of your choice. You can set how many Cards you want to show vertical or horizontal.
Sample layout:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
In your Activity now you can set it's adapter
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new YourCustomAlbumAdapter(this));
For complete example you can see official Example http://developer.android.com/guide/topics/ui/layout/gridview.html
My grid design is as such. But i want to remove the spaces on both sides of the grid elements. (Black marked)
I want the image to be expanded somewhat like
My XML layouts for the gridview
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
tools:context=".MainActivity" >
<GridView
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="100dip"
android:gravity="center"
android:numColumns="2"
android:stretchMode="spacingWidthUniform" />
</LinearLayout>
My grid element layout
<?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" >
<ImageView
android:id="#+id/grid_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:contentDescription="#string/im" >
</ImageView>
<TextView
android:id="#+id/grid_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLength="23"
android:gravity="center"
android:layout_marginTop="10sp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textSize="12sp" >
</TextView>
</LinearLayout>
Try Changing
<GridView
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:verticalSpacing="1dp"
android:horizontalSpacing="1dp"
android:numColumns="2"
android:background="#abcdef"
android:stretchMode="columnWidth" />
You can replace GridView with GridLayout and use weights to make the images fill the screen. Here's the reference: http://developer.android.com/reference/android/widget/GridLayout.html, (See the section: Excess space distribution)
I have a gridlayout like this in the main layout xml of my activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="#+id/parentLayout"
android:layout_gravity="right">
<GridView
android:id="#+id/gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#AAAAAA"
android:columnWidth="90dp"
android:numColumns="3"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="none"
/>
</LinearLayout>
I populate it with 6 icons by inflating the following:
<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/searchIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/searchIcon"/>
What I want is that the gridlayout is right aligned in the linear layout and that it just wraps its content. I am testing this by giving it a different coloured background. The layout_height="wrap_content" works fine, but the width seems to have no effect. The result is this:
Any ideas?
Thanks in advance,
Stephen
Insert android:gravity="right" in GridView
<GridView
......
.....
android:gravity="right" />
use android:gravity="right" in LinearLayout instead-of LinearLayout ..............
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="#+id/parentLayout"
android:gravity="right"> //<-----------------------
android:gravity sets the gravity of the content of the View its used
on.
android:layout_gravity sets the gravity of the View or Layout in its
parent.
for test try
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical" android:id="#+id/parentLayout"
android:gravity="right">
<TextView android:id="#+id/gridview" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#AAAAAA"
android:text="ewewqe" />
</LinearLayout>
also see this not the answer but see
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:id="#+id/parentLayout"
android:layout_gravity="right"
android:gravity="right" android:weightSum="1">
<GridView
android:id="#+id/gridview"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:background="#AAAAAA"
android:columnWidth="90dp"
android:numColumns="3"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="none"
android:layout_weight=".5" />
</LinearLayout>
I need somehow to do this :
I tried playing with Relative Layout, I placed top imageview and bottom button but how do I place my gridview in the center? BUT BETWEEN top image and bottom button?
Have the top image and the bottom button use wrap_content for height, and the GridView use 0dip for height along with a weight of 1. This will cause the top and bottom elements to measure first, and the GridView will get all remaining space in the middle.
<?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">
<ImageView android:id="#+id/top_image"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<GridView android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<Button android:id="#+id/bottom_button"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
adamp's answer didn't work for me. This is what I found to work perfectly:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView android:id="#+id/top_image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<Button android:id="#+id/bottom_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
<LinearLayout
android:id="#+id/container_layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/bottom_button"
android:layout_below="#id/top_image"
android:gravity="center" >
<GridView android:id="#+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
</RelativeLayout>
I've used this with other LinearLayouts in place of the GridView, with android:gravity="center" set on the innermost layout as well to make all of its text views and images centered horizontally as well as vertically.
use something like this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/table">
<TableRow
android:weightSum="100">
<ImageView android:id="#+id/image1"
android:src="#drawable/icon"
android:visibility="visible"
android:gravity="center"
android:layout_weight="50" />
</TableRow>
<TableRow
android:weightSum="100">
<GridView
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</GridView>
</TableRow>
<TableRow
android:weightSum="100">
<Button android:id="#+id/btn1"
android:visibility="visible"
android:gravity="center"
android:layout_weight="50" />
</TableRow>
</TableLayout>
</LinearLayout>
You could try 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="vertical">
<ImageView android:id="#+id/top_image"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<GridView android:id="#+id/grid1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<Button android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Add these to your Grid View:
android:layout_above="#id/bottom_button"
android:layout_below="#id/top_image"