Gridview height gets cut
After (too much) research, I stumbled on the excellent answer of Neil Traft.
Adapting his work for the GridView
has been dead easy.
ExpandableHeightGridView.java:
package com.example;
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;
}
}
Include it in your layout like this:
android:id="@+id/myId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:horizontalSpacing="2dp"
android:isScrollContainer="false"
android:numColumns="4"
android:stretchMode="columnWidth"
android:verticalSpacing="20dp" />
Lastly you just need to ask it to expand:
mAppsGrid = (ExpandableHeightGridView) findViewById(R.id.myId);
mAppsGrid.setExpanded(true);
Android GridView last column getting cut
Remove android:layout_marginStart and android:layout_marginEnd attributes, and they will stop being pushed outside of bounds.
Why my GridView cuts off at The End?
Its happening because you have set the FrameLayout height 520dp in xml
set the frameLayout height match and also use android:layout_above="@+id/navigation"
try this code
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/navigation"
android:orientation="horizontal">
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="?android:attr/windowBackground"
app:menu="@menu/navigation">
dynamic fixed height for grid view in flutter
You can put your GridView
in some Expanded
widget and use
shrinkWrap: true,
in GridView
attribute .
So the GridView
will wrap to the Expanded
height.
Do not forget to change settings about scrolls
.
GridView height is automatically increasing
The height of your GridView is set to a fixed value (285px). The rows must adapt their height to add up to that total value. You may not notice it when the grid is filled with 10 rows but it becomes more apparent when fewer rows are displayed. If you remove Height="285px"
from the GridView attributes, each row should keep its normal height.
Gridview show according to actual height within scroll view
public class MyGridView extends GridView {
public MyGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyGridView(Context context) {
super(context);
}
public MyGridView(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);
}
}
Related Topics
Disable Scrollview Programmatically
Execution Failed App:Processdebugresources Android Studio
Using Intents to Pass Data Between Activities
Android Studio's Project Gradle File Changed
How to Implement Android Pull-To-Refresh
How to Keep Onitemselected from Firing Off on a Newly Instantiated Spinner
Clear the Entire History Stack and Start a New Activity on Android
Android Videoview Orientation Change with Buffered Video
Custom Toast on Android: a Simple Example
How to Hang Up Outgoing Call in Android
How to Display Image from Url on Android
Changing Background Color of Listview Items on Android
Toggle Airplane Mode in Android
How to Make a Phone Call in Android and Come Back to My Activity When the Call Is Done
How to Send a File in Android from a Mobile Device to Server Using Http
How to Develop a Soft Keyboard For Android