I want to make a layout that display several items, but I cannot make them fully unfold, so I use a ScrollView. However, I find that I can only scroll the first GridView, the last two item can not be scroll up.
What I need is to display two GridView fully and all items can be scrolled,but I do not know how to deal with the problem.
<RelativeLayout
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"
tools:context="com.ifchan.reader.AllClassActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/all_class_tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorToolbar"
app:title="All Class"
app:titleTextColor="#ffffff"/>
<ScrollView
android:id="#+id/all_class_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/all_class_tool_bar"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/all_class_male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="male"
android:textSize="20dp"/>
<GridView
android:id="#+id/all_class_male_grid_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"/>
<TextView
android:id="#+id/all_class_female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="female"
android:textSize="20dp"/>
<GridView
android:id="#+id/all_class_female_grid_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Use Recycler View or a non scrollable gridview if you want to use scrollview.
public class NonScrollGridView extends GridView{
public NonScrollGridView (Context context) {
super(context);
}
public NonScrollGridView (Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonScrollGridView (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
<NonScrollGridView
android:id="#+id/all_class_female_grid_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"/>
You put GridViews into ScrollView. It will not work as you expected.
Try Using GridLayoutManager with RecyclerView and put it in NestedScrollView
You can check this question for detailed explanations.
If you want to use GridView anyway, you can use this ExpandableHeightGridView
which extends GridView
Related
I'm trying to create this view:
It´s Rerecyclew with a Gridview for each item.
So when you press the plus item I've to add a new item to the Gridview and adjust the RecyclewView item height.
But i can't make it with a wrap_content and i don´t know why.
Activity Layout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_scenes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
</LinearLayout>
RecyclewView Item
<?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="wrap_content"
android:orientation="vertical">
<GridView
android:id="#+id/gv_scene_items"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:gravity="center"
android:columnWidth="50dp" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#eaeaea" />
</LinearLayout>
GridView Item (just for example)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="55dp"
android:layout_height="55dp"
android:background="#color/liloColorBlue">
</LinearLayout>
Can you help me to get the example view?
Thank you.
You can do this very easily.
Change the recyclerView height in xml to:
<android.support.v7.widget.RecyclerView
android:id="#+id/rv_scenes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
This will automatically adjust the layouts.
Hope this helps.
Call the notifyDataSetChanged() or notifyItemChanged(int position) methods of the RecyclerView.Adapter from the MAIN THREAD.
I hope this can help you
I found the solution creating my own gridview like this:
public class ScenesGridView extends GridView {
public ScenesGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ScenesGridView(Context context) {
super(context);
}
public ScenesGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
I have a layout which contains only a LinearLayour with 3 buttons inside.
I would want this 3 buttons to fill the screen vertically so I used "weigth=1" but I should also want the buttons to be square, that is, that their width is equal to their height.
Any way to do it?
Here's the .xml file
<?xml version="1.0" encoding="utf-8"?>
<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="android.jose.se.Prueba"
android:background="#drawable/bg"
android:gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="#+id/bHet"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#drawable/layer1"
android:onClick="cambiarBoton"/>
<Button
android:id="#+id/bGay"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#drawable/layer2"
android:onClick="cambiarBoton"/>
<Button
android:id="#+id/bLesbi"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#drawable/layer3"
android:onClick="cambiarBoton"/>
</LinearLayout>
</RelativeLayout>
I had similar issue and done it using Google's Percent Relative Layout that helps you manage view aspect ratio.
You can use it like this
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
app:layout_aspectRatio="100%"/>
</android.support.percent.PercentFrameLayout>
The best way to do this, is to make a new Class called Square button and this class should extend Button. Then in the onMeasure of this class, simply make your heightSpec as WidthSpec.
public class SquareButton extends Button {
public SquareButton(Context context) {
super(context);
}
public SquareButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SquareButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
protected void onMeasure(int WidthSpec, int HeightSpec){
int newWidthSpec = HeightSpec ;
super.onMeasure(newWidthSpec, HeightSpec);
}}
Now you can use this SquareButton instead of a normal Button in your xml like this. The value of android:width attribute will be ignored.
<com.example.package.SquareButton
android:height = "0dp"
android:weight = "1"
android:width = "0dp"
/>
I want the homepage of my app to have different sections containing a list of items in every section. For this I have used a ScrollView. Inside the scrollview I have put a search area, and a ExpandableHeightGridView (this is a custom class that help the GridView to expand the space inside the ScrollView based on the number of items).
But regardless of this the scrollView doesn't work and search box appears at the top of the screen, meanwhile the GridView scrolls as if it wasn't inside ScrollView.
Below is my Code:
activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/njoftime_background">
<ScrollView
android:id="#+id/sv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:layout_marginTop="?android:attr/actionBarSize"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/sr"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/event_background_block"
android:padding="5dp">
<RelativeLayout
android:id="#+id/search_area"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:background="#drawable/njoftime_item_background">
<ImageButton
android:id="#+id/btn_search"
android:layout_width="65dp"
android:layout_height="35dp"
android:layout_alignParentRight="true"
android:layout_marginLeft="1dp"
android:background="#color/njoftime_main_color"
android:onClick="searchPressed"
android:scaleType="fitCenter"
android:src="#drawable/ic_search_white" />
<EditText
android:id="#+id/search_text"
android:layout_width="fill_parent"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#+id/btn_search"
android:background="#null"
android:ems="20"
android:imeOptions="actionSearch"
android:inputType="text"
android:maxLines="1"
android:paddingLeft="7dp"
android:textColor="#color/njoftime_desc"
android:textCursorDrawable="#null"
android:textSize="17sp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/search_categories_area"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/search_area"
android:paddingBottom="7dp"
android:paddingTop="13dp">
<GridView
android:id="#+id/search_grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:drawSelectorOnTop="false"
android:horizontalSpacing="2dp"
android:numColumns="4" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.example.crs.tempus.ExpandableHeightGridView
android:id="#+id/njoftime_list"
android:layout_width="match_parent"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp"
android:layout_marginTop="5dp"
android:layout_height="wrap_content"
android:background="#color/white"
android:drawSelectorOnTop="true"
android:numColumns="1" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
<RelativeLayout
android:id="#+id/left_drawer"
android:layout_width="230dp"
android:layout_height="fill_parent"
android:layout_gravity="start"
android:layout_marginTop="?android:attr/actionBarSize"
android:background="#color/white"
android:orientation="vertical">
<ExpandableListView
android:id="#+id/list_slidermenu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:choiceMode="singleChoice"
android:dividerHeight="1dp"
android:drawSelectorOnTop="true"
android:groupIndicator="#null"
android:paddingBottom="13dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:paddingTop="13dp"></ExpandableListView>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
ExpandableHeightGridView
public class ExpandableHeightGridView extends GridView
{
boolean expanded = false;
public ExpandableHeightGridView(Context context)
{
super(context);
}
public ExpandableHeightGridView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public ExpandableHeightGridView(Context context, AttributeSet attrs,
int defStyle)
{
super(context, attrs, defStyle);
}
public boolean isExpanded()
{
return expanded;
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
// HACK! TAKE THAT ANDROID!
if (isExpanded())
{
// Calculate entire height by providing a very large height hint.
// View.MEASURED_SIZE_MASK represents the largest height possible.
int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
else
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded)
{
this.expanded = expanded;
}
}
The ExpandableHeightGridView, is used also in another activity and it works.
Any idea?
1) In your ExpandableHeightGridView class use below code inside your onMeasure override method. It works in my case.
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
Or
2) In your code you might need to call setExpanded method of ExpandableHeightGridView class and set it to true.
Hope one of this solution would works for you.
UPDATED---------
try to use a customScollview,create one CustomScrollview like this and use this
public class CustomScrollView extends ScrollView {
public CustomScrollView(Context context) {
super(context);
}
public CustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return false;
}
}
android:fillviewport="true"
add this in your ScrollView ,like this
<CustomScrollView
android:fillviewport="true"
android:id="#+id/sv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white">
currently I am developing a settings menu in a scrollview. The basic structure looks like the following:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="#+id/scrollview_settings"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="#dimen/paddingSmall">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/transparent_white_settings"
android:paddingTop="#dimen/paddingMedium"
android:paddingBottom="#dimen/paddingMedium">
<ImageView
android:id="#+id/logo"
android:layout_width="68dp"
android:layout_height="wrap_content"
android:src="#drawable/newspaper_logo_color"
android:layout_alignParentRight="true"
android:layout_margin="#dimen/paddingBig"
android:adjustViewBounds="true"/>
</RelativeLayout>
<!-- Layout News Uebersicht -->
<include layout="#layout/separator_line_grey"/>
<de.basecom.noznewsapp.views.FontTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:textColor="#color/buttonHoverBackground"
android:textSize="#dimen/sliding_image_kicker_font_size"
android:paddingLeft="#dimen/paddingLarge"
android:paddingTop="#dimen/paddingMedium"
android:paddingBottom="#dimen/paddingMedium"
android:text="#string/news_overview"
android:textScaleX="#dimen/letter_spacing"
android:textStyle="bold"
android:gravity="center_vertical"
android:background="#color/settings_header_color"
android:layout_gravity="center_vertical"/>
<include layout="#layout/separator_line_grey"/>
<!-- News-Overview Issues -->
<LinearLayout
android:id="#+id/issue_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/transparent_white_settings">
<de.basecom.noznewsapp.views.NewsScrollListView
android:id="#+id/listview_issues"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
android:divider="#null"
android:dividerHeight="0dp" />
</LinearLayout>
<!-- Layout Einstellungen -->
<de.basecom.noznewsapp.views.FontTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:textColor="#color/buttonHoverBackground"
android:textSize="#dimen/sliding_image_kicker_font_size"
android:paddingLeft="#dimen/paddingLarge"
android:paddingTop="#dimen/paddingMedium"
android:paddingBottom="#dimen/paddingMedium"
android:text="#string/settings"
android:textStyle="bold"
android:textScaleX="#dimen/letter_spacing"
android:gravity="center_vertical"
android:background="#color/settings_header_color"
android:layout_gravity="center_vertical"/>
<include layout="#layout/separator_line_grey"/>
<!-- Einstellungs- Werte -->
<LinearLayout
android:id="#+id/settings_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#color/transparent_white_settings">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/paddingLarge">
<de.basecom.noznewsapp.views.FontTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/receive_pushmessages"
android:textAllCaps="true"
android:textScaleX="#dimen/letter_spacing"
android:textSize="#dimen/sliding_image_kicker_font_size"
android:layout_centerVertical="true"
android:textColor="#color/buttonBackground"/>
<Switch
android:id="#+id/receive_pushmessages_switch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="#string/empty_string"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
For the listview I used a custom implementation, to give it a fix height and make it able to scroll within the scrollview (note: I have to use the listview within the scrollview, because I have lots of elements, so that using a linearlayout wouldn't be nice).
public class CustomListView extends ListView {
public CustomListView(Context context) {
super(context);
}
public CustomListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomListView(Context context, AttributeSet attrs,int defStyle) {
super(context, attrs, defStyle);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Calculate entire height by providing a very large height hint.
// But do not use the highest 2 bits of this integer; those are
// reserved for the MeasureSpec mode.
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
}
Each item of the listview contains a label and a gridview, which is all shown correctly. But now I want to implement the functionality, that the user can click on the label to show and hide the visibility of the gridview.
But if I click on the label and the gridview is shown, the height of the listview doesn't adapt to the new open element. It always stays at the same height and if I open an element, the elements below are no longer visible. This also even happens, if I call the requestLayout() method of the listview to trigger its onMeasure again.
Anybody got an idea what I am doing wrong?
EDIT: Updated my xml file :)
Finally I was only able to solve this issue by replacing Listview with a LinearLayout and add my items in code to the Layout.
I am having all sorts of issues with row heights in GridView.
I would like to set the size of the items (height/width) in XML and then just have the GridView autofit as many of them as it can with no stretching. If it can't fit the next element it should just add padding around the current number of elements it was able to fit.
Currently I get 2 columns (which to me almost seems fixed size) and the rows get stretched. Could someone please help explain what is happening and how to achieve what I want?
GridView:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/main_grid"
android:numColumns="auto_fit"
android:gravity="center"
android:paddingRight="20dp"
android:paddingLeft="20dp"
android:clipToPadding="true"
android:fitsSystemWindows="true"
android:stretchMode="none"
android:background="#drawable/main_grid_background">
</GridView>
GridItem (I want it 320x320 as I later insert a background image into it which looks odd if its not a perfect square).
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="320dp"
android:layout_height="320dp"
android:padding="10dp" >
<TextView
android:id="#+id/grid_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="#+id/label"
android:layout_marginTop="5dp"
android:textColor="#color/black"
android:textSize="15sp"
android:visibility="invisible"
android:layout_centerInParent="true" >
</TextView>
</RelativeLayout>
Java:
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.main_grid_item, null);
} else {
gridView = (View) convertView;
}
TextView textView = (TextView) gridView
.findViewById(R.id.grid_item_label);
//SET TEXT AND BACKGROUND IMAGE HERE
//gridView.setBackgroundResource(R.drawable.main_grid_item_import);
return gridView;
}
In the CustomAdapter
v.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, rowHigh));
rowHigh is the dimension you must change
So, this is not a perfect answer but this is how I got it to work. Ideas borrowed from this thread.
All of this works assuming I know my image sizes, which are 320x320. Also, in GridView I had to set android:columnWidth="320dp" or it would not work. If someone has a better idea please post it... for now I am moving on.
Grid Item XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp" >
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
<TextView
android:id="#+id/grid_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="#+id/label"
android:textColor="#color/black"
android:layout_alignLeft="#+id/grid_item_image"
android:layout_alignTop="#+id/grid_item_image"
android:layout_alignRight="#+id/grid_item_image"
android:layout_alignBottom="#+id/grid_item_image"
android:gravity="center"
android:textSize="15sp"
android:visibility="invisible">
</TextView>
</RelativeLayout>
GridView XML:
<GridView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/main_grid"
android:numColumns="auto_fit"
android:gravity="center"
android:columnWidth="320dp"
android:paddingRight="20dp"
android:paddingLeft="20dp"
android:clipToPadding="true"
android:fitsSystemWindows="true"
android:stretchMode="columnWidth"
android:background="#drawable/main_grid_background">
</GridView>
public class MyGridView extends GridView {
public MyGridView(Context context) {
super(context);
}
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightSpec;
if (getLayoutParams().height == LayoutParams.WRAP_CONTENT) {
// The great Android "hackatlon", the love, the magic.
// The two leftmost bits in the height measure spec have
// a special meaning, hence we can't use them to describe height.
heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST);
} else {
heightSpec = heightMeasureSpec;
}
super.onMeasure(widthMeasureSpec, heightSpec);
}
}
In XML:
<MyGridView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/main_grid"
android:gravity="center"/>