How to place gridView to the center of screen - android

How can I place my programmatically filled gridview to the center of screen.
It is always on the top.
I can place my gridview to the centerm if it is included to the scrollView.
But in this case I can only see first cells of gridview (other not visible)
The XML is here:
<?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:background="#color/backNormal"
android:orientation="vertical"
>
<include layout="#layout/adview" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/backNormal"
android:orientation="vertical"
android:layout_gravity="center_horizontal|center_vertical"
>
<GridView
android:id="#+id/gridViewTopics"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#color/backNormal"
android:gravity="center"
android:layout_gravity="center_horizontal|center_vertical"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
/>
</LinearLayout>
</LinearLayout>
Any ideas please? Thanks!

Try this way,hope this will help you to solve your problem.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/backNormal"
android:orientation="vertical">
<include layout="#layout/adview" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/backNormal"
android:orientation="vertical"
android:gravity="center">
<GridView
android:id="#+id/gridViewTopics"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#color/backNormal"
android:gravity="center"
android:numColumns="3"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"/>
</LinearLayout>
</LinearLayout>

Related

LinearLayout under GridView does not wrap content

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.

Unwanted Gridview spacing

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)

scrollView not working android

I see the scroll line but it is not working. This is the code:
What could be the reason?
The layout:
<ScrollView xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/titleshopandsell"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:text="פריטים שתוכל למכור"
android:textColor="#880000"
android:textSize="12sp" />
<GridView
android:id="#+id/gridView1"
android:numColumns="auto_fit"
android:gravity="center"
android:verticalSpacing="3dp"
android:horizontalSpacing="2dp"
android:columnWidth="130dp"
android:stretchMode="columnWidth"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</GridView>
</LinearLayout>
</ScrollView>
What is the problem with my code?
GridView is also a special type of ScrollView.. you should change parent scrollView to Relative/Linear Layout.
Currently Both scrollView and GridView obstruct the proper scrolling.

gridview with tab bar in android

I have to develop one android application.
Here i have to develop grid view layout like:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/Layout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/status"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Mercuryminds Shoppingcart Application"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#040404"
android:gravity="center" />
<GridView
android:layout_height="wrap_content"
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:numColumns="auto_fit"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp">
</GridView>
<include
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="#layout/footer" />
</LinearLayout>
Here the layout footer is tab bar.
<?xml version="1.0" encoding="utf-8"?>
<TabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:id="#+id/linearLayout1"
android:layout_height="match_parent"
android:orientation="vertical">
<TabWidget
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/tabs">
</TabWidget>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/tabcontent">
</FrameLayout>
</LinearLayout>
</TabHost>
But i have to run the app means the tab bar is not display on my main view.whats wrong in my layout file.please help me.
add layout_weight="1" to your gridView and layout_height="0dp"
like this
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp" >
</GridView>

Padding of grid in GridView

I have some problems with my grid 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:id="#+id/frag_books_grid_view_layout"
android:orientation="vertical">
<GridView
android:padding="0dp"
android:cacheColorHint="#color/bg_default"
android:listSelector="#drawable/list_selector_background"
android:id="#+id/frag_books_grid_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchMode="spacingWidth"
android:scrollbars="none"/>
</LinearLayout>
Single item:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ListView
android:cacheColorHint="#color/bg_default"
android:scrollbars="none"
android:listSelector="#drawable/list_selector_background"
android:layout_gravity="center_vertical|left"
android:gravity="center_vertical|left"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:id="#+id/frag_book_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:verticalSpacing="5dp"/>
</LinearLayout>
Problem is that there is no left and right padding of single item. And it looks terrible when item is selected: http://i.imgur.com/rirNP.png
Please, help to fix it.
try it
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/menu_category"
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"
/>
for information
http://developer.android.com/guide/topics/ui/layout/gridview.html
this one is best for three columns..
<GridView
android:id="#+id/gridview_Gallery"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="65dp"
android:gravity="center"
android:horizontalSpacing="6dp"
android:listSelector="#00000000"
android:numColumns="3"
android:scrollbars="none"
android:stretchMode="columnWidth"
android:verticalSpacing="-1dp" />

Categories

Resources