This question already has answers here:
RecyclerView inside ScrollView is not working
(26 answers)
Closed 6 years ago.
I have a recyclerView inside my LinearLayout.
<ScrollView 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:id="#+id/book_detail_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.BookDetailActivity">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
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:orientation="vertical">
<ImageView
android:layout_width="250dp"
android:layout_height="270dp"
android:id="#+id/book_image"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:layout_gravity="center"
android:src="#drawable/book_default"/>
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:background="#EEEEEE"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="#+id/book_authors"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:fontFamily="sans-serif-condensed" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyleSmall"
android:max="5"
android:id="#+id/book_rating"
android:stepSize="0.5"
android:paddingTop="10dp"
android:isIndicator="true"
android:progressTint="#FFA500"
android:secondaryProgressTint="#D1D6D8"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="10dp"
android:paddingLeft="5dp"
android:id="#+id/book_rating_text"
android:fontFamily="sans-serif-condensed" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:id="#+id/book_reviews_text"
android:textColor="#color/redColor"
android:fontFamily="sans-serif-condensed" />
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:background="#EEEEEE"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:fadingEdge="vertical"
android:fadingEdgeLength="20dp"
android:paddingTop="#dimen/activity_vertical_margin"
android:id="#+id/book_description"
android:onClick="toggleText"
android:ellipsize="end"
android:maxLines="3" />
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:id="#+id/book_description_below_line"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:background="#EEEEEE"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/dark_translucent_black"
android:textAlignment="center"
android:textSize="15dp"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:text="People near you who are willing to lend this book"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/lender_recycler_view"
android:layout_width="match_parent"
android:layout_height="400dp"
android:scrollbars="vertical" />
</LinearLayout>
</ScrollView>
The problem is that when I inflate this recyclerView, the complete activity is not scrolling (Only the part of recycler view does). Moreover I have specify height of recyclerView otherwise I am not able to see it.
So my two problems are:
How to give this RecyclerView hight dynamically (layout_height: wrap_content isn't working)
How to scroll the entire activity (and not just the recyclerView part) when content is more.
P.S I am new to Android, though I have implemented recyclerView earlier but in those cases there was only one element (one recyclerView inside a parent LinearLayout)
ExpandableHeightListView.java
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.ListView;
import android.content.Context;
public class ExpandableHeightListView extends ListView
{
boolean expanded = false;
public ExpandableHeightListView(Context context)
{
super(context);
}
public ExpandableHeightListView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public ExpandableHeightListView(Context context, AttributeSet attrs,
int defStyle)
{
super(context, attrs, defStyle);
}
public boolean isExpanded()
{
return expanded;
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
if (isExpanded())
{
// 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();
}
else
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
public void setExpanded(boolean expanded)
{
this.expanded = expanded;
}
}
Add this customized java file and use it in xml.
and in your MainActivity do like this:
ExpandableHeightListView listview= (ExpandableHeightListView) findViewById(R.id.comment_listview);
and set adapter and set one property
listview.setAdapter("Your Adapter");
listview.setExpanded(true);
Related
I have the following GridView:
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="120dp"
app:layout_constraintTop_toTopOf="parent"
android:numColumns="2"
android:gravity="center" />
and the following Item:
<?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:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:gravity="center"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
>
<TextView
android:id="#+id/planlayout_teams_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
tools:text="Tambora 0 - 0 Balagath"
android:textStyle="bold"
android:paddingTop="4dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:gravity="center"/>
<TextView
android:id="#+id/planlayout_status_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
tools:text="noch unentschieden"
android:gravity="center"/>
</LinearLayout>
<TextView
android:id="#+id/planlayout_menu_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text=".\n.\n."
android:textSize="12sp" />
</LinearLayout>
And everything is working fine except for the last item(s). Whether there is one or two items left, the whole row is not displayed. I googled a lot but nothing could help me.
Edit: I have the same "problem" as here: Custom GridView's Last row items not showing fully but those links don't help me.
I think the problem is about your layout. Your adapter load your all items but your layout not let you see last item because of your top margin.
You can edit your grid layout like this. Can you try this way.
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="120dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:numColumns="2"
android:gravity="center" />
just try this -->
<GridView
android:id="#+id/mGridView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:columnWidth="172dp"
android:gravity="center"
android:horizontalSpacing="5dp"
android:numColumns="auto_fit"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:stretchMode="columnWidth"
android:verticalSpacing="5dp"/>
Or try with custom gridview
your gridview code on layout something like this :--
<com.yourpackagename.MyGridView
android:id="#+id/mGridView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="2"
android:adjustViewBounds="true"
android:columnWidth="90dp"
android:verticalSpacing="-2dp"
android:horizontalSpacing="-10dp"
android:stretchMode="columnWidth"
android:scrollbars="vertical"
android:fastScrollEnabled="true"
/>
MyGridview activity:-
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.MATCH_PARENT) {
heightSpec = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
}
else {
heightSpec = heightMeasureSpec;
}
super.onMeasure(widthMeasureSpec, heightSpec);
}
}
In your mainactivity (activity where you want to declare the gridview)
private MyGridView mGridView;
//also add your adapter of gridview
.
.
oncreate(){
mGridView = getView().findViewById(R.id.mGridView);
//then attach your adapter
adapter = new GridViewAdapter(getContext()...whatever parameters you have in your constructor);
mGridView.setAdapter(adapter);
try this custom gridview...still it doesnt worked let me know
I pull data into my listview from servers. The listview is inside an activity. The activity has buttons above it.
When the data is received and when I scroll data, only listview is scrolled. The buttons remain fixed at the top.
I want the listview to take the required vertical height it needs so that I would need to scroll the activity instead of the listview.
My XML code :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:text="text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/view_c_name"
android:textSize="18sp"
android:textStyle="bold"/>
<TextView
android:text="text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/view_c_name"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/view_c_motto" />
<TextView
android:text="text5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/view_c_motto"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/view_c_details" />
<TextView
android:text="text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/view_c_address"
android:layout_below="#+id/view_c_details"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:text="text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/view_c_act_text"
android:layout_below="#+id/view_c_address"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="71dp" />
<Button
android:text="Button 1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:id="#+id/view_button"
android:layout_below="#+id/view_c_address"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="button 2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/view_button"
android:layout_toRightOf="#+id/view_button"
android:layout_toEndOf="#+id/view_button"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:id="#+id/view_button2" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/view_c_act_text"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/my_listview"
android:scrollbars="none"
/>
</RelativeLayout>
The perfect code should be;
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--Inside the below layout your buttons will come. Modify it accordingly-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!--Your buttons will come here-->
</LinearLayout>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
As shown above, Nested Scroll View should be used instead of Scroll View because
there is a need of scrolling view (in this case List View) inside another scrolling view. The system is unable to decide which view to scroll and this is where Nested Scroll View comes in.
Wrap your existing layout in a ScrollView to scroll the entire thing, not just the ListView
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- your existing layout here, change your top container's height from match_parent to wrap_content -->
</ScrollView>
Try this one
<ScrollView
android:id="#+id/scrollViewMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/linearLayoutMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:text="Button"
android:id="#+id/buttonMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent">
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
"your existing layout here, change your top container's height from match_parent or wrap_content "
the best case secanrio is to use scroll View inside of an relative layout and all the existing code inside of the scrollView
I had similar problem then I created a class
public class ListViewExpanded extends ListView {
public ListViewExpanded(Context context, AttributeSet attrs) {
super(context, attrs);
setDividerHeight(0);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST));
}
}
then I used in xml as try it out it will help for sure
<android.support.v4.widget.NestedScrollView 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"
>
<TextView
android:id="#+id/text_goal_scored"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<example.ListViewExpanded
android:id="#+id/list_home"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
</example.ListViewExpanded>
</android.support.v4.widget.NestedScrollView>
Go through this Example. Make ScroolView as Parent. It Work Form me.
<ScrollView android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<include
android:id="#+id/maintool"
layout="#layout/toolbar"></include>
<RelativeLayout
android:id="#+id/re1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp">
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:textSize="18sp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/text" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/re2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Email template"
android:textSize="18sp" />
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/text2"
android:paddingTop="10dp" />
</RelativeLayout>
</LinearLayout>
</ScroolView>
this is how you can do your task.this is Scroll disabled listview so you can scroll your activity easily.
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ListView;
/**
* Created by sudesh Regmi on 4/20/2017.
*/
public class ScrollDisabledListView extends ListView {
private int mPosition;
public ScrollDisabledListView(Context context) {
super(context);
}
public ScrollDisabledListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ScrollDisabledListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
public boolean dispatchTouchEvent(MotionEvent ev) {
final int actionMasked = ev.getActionMasked() & MotionEvent.ACTION_MASK;
if (actionMasked == MotionEvent.ACTION_DOWN) {
// Record the position the list the touch landed on
mPosition = pointToPosition((int) ev.getX(), (int) ev.getY());
return super.dispatchTouchEvent(ev);
}
if (actionMasked == MotionEvent.ACTION_MOVE) {
// Ignore move events
return true;
}
if (actionMasked == MotionEvent.ACTION_UP) {
// Check if we are still within the same view
if (pointToPosition((int) ev.getX(), (int) ev.getY()) == mPosition) {
super.dispatchTouchEvent(ev);
} else {
// Clear pressed state, cancel the action
setPressed(false);
invalidate();
return true;
}
}
return super.dispatchTouchEvent(ev);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}
setListViewHeightBasedOnChildren(your_listview);
add this line in your activity
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
add scrollview at parent in your xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/view_c_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="text1"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="#+id/view_c_motto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/view_c_name"
android:text="text4" />
<TextView
android:id="#+id/view_c_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/view_c_motto"
android:text="text5" />
<TextView
android:id="#+id/view_c_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/view_c_details"
android:text="text2" />
<TextView
android:id="#+id/view_c_act_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/view_c_address"
android:layout_marginTop="71dp"
android:text="text3" />
<Button
android:id="#+id/view_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/view_c_address"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:text="Button 1" />
<Button
android:id="#+id/view_button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/view_button"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_toEndOf="#+id/view_button"
android:layout_toRightOf="#+id/view_button"
android:text="button 2" />
<ListView
android:id="#+id/my_listview"
android:layout_width="match_parent"
android:layout_height="700dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/view_c_act_text"
android:scrollbars="none" />
</RelativeLayout>
</ScrollView>
Im having problem where the android scrollview starts hiding a pair of textviews (Top ~5-6 lines doesnt show). I use padding, margin, fillviewport, wrap in linearlayout, original scrollview etc. many variations but it didnt work. I check this problem both api 16 and 23.
Im working many hours but doesnt find solution.
dialog_base.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/base"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatImageView
android:id="#+id/wallpaper"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="#+id/panelAlt"
android:clickable="false"
android:focusable="false"
android:visibility="gone"/>
<include
layout="#layout/toolbar"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"/>
<android.support.v7.widget.AppCompatTextView
android:id="#+id/topInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="8dp"
android:layout_below="#id/toolbar"/>
<RelativeLayout
android:id="#+id/innerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/topInfo">
<!--Inner view-->
</RelativeLayout>
<LinearLayout
android:id="#+id/lyBottom"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|right"
android:layout_below="#+id/innerView"
android:animateLayoutChanges="true">
<android.support.v7.widget.AppCompatButton
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?selectableItemBackground"/>
<android.support.v7.widget.AppCompatButton
android:id="#+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<android.support.v7.widget.AppCompatButton
android:id="#+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</RelativeLayout>
dialog_checkbox.xml (this view inflate programmatically and adding in id: innerView layout from dialog_base.xml)
<?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="wrap_content">
<include
android:id="#+id/scroll"
layout="#layout/scroll_and_text"/>
<android.support.v7.widget.AppCompatCheckBox
android:id="#+id/checkbox"
android:layout_below="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"/>
</RelativeLayout>
scroll_and_text.xml
<?xml version="1.0" encoding="utf-8"?>
<app.CustomScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:fadeScrollbars="true"
android:scrollbarStyle="outsideInset"
android:scrollbarFadeDuration="3000"
android:fillViewport="true">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical">
<android.support.v7.widget.AppCompatImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:layout_marginRight="16dp"/>
<android.support.v7.widget.AppCompatTextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</app.CustomScrollView>
CustomScrollView.java
public class CustomScrollView extends ScrollView {
private final int maxHeight = Utils.dptoPx(250);
public CustomScrollView(Context context) {
super(context);
}
public CustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
try {
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (maxHeight != -1 && heightSize > maxHeight) {
heightSize = maxHeight;
}
heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.AT_MOST);
getLayoutParams().height = heightSize;
} catch (Exception e) {
Log.print(e);
} finally {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
}
Thanks in advance!
I am using a GridView in a Relative layout. I want the GridView to show
up as tall as needed to show all the rows. However, I am seeing only
one row when set (layout_width=MATCH_PARENT and)
layout_height=WRAP_CONTENT. If I set layout_height to a size that
equals mulitple rows, then I do see those rows.
<?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"
android:gravity="center"
>
<RelativeLayout
android:id="#+id/layout_month"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="#+id/textv_month_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:gravity="center_horizontal|center_horizontal"
android:textColor="#ffffff"
android:textSize="7dp" />
<ImageView
android:id="#+id/top_bar"
android:layout_width="fill_parent"
android:layout_height="15dp"
android:layout_below="#+id/textv_month_name"
android:background="#drawable/calendar_top_small"
android:clickable="false" />
<GridView
android:id="#+id/gridv_inner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/top_bar"
android:layout_gravity="center"
android:clickable="false"
android:gravity="center"
android:numColumns="7"
android:stretchMode="columnWidth"
>
</GridView>
<Button
android:id="#+id/monthBtn"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent" />
</RelativeLayout>
</RelativeLayout>
Any Help will be appreciable
This is a very useful answer.
The GridView didn’t like the wrap_content of its height when it was put in a vertically tight space; it only showed a single row of items (scrolling all content in that single row).
The short version of my fix includes sub classing the GridView and overriding the onMeasure method. The essence of the fix focuses on setting the measured height spec to a ridiculously large number.
MyGridview.Java
package com.arun.calendar;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.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 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 {
// Any other height should be respected as is.
heightSpec = heightMeasureSpec;
}
super.onMeasure(widthMeasureSpec, heightSpec);
}
}
And the new XML file
<?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"
android:gravity="center" >
<RelativeLayout
android:id="#+id/layout_month"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/textv_month_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:gravity="center_horizontal|center_horizontal"
android:textColor="#ffffff"
android:textSize="7dp" />
<ImageView
android:id="#+id/top_bar"
android:layout_width="fill_parent"
android:layout_height="15dp"
android:layout_below="#+id/textv_month_name"
android:background="#drawable/calendar_top_small"
android:clickable="false" />
<com.arun.calendar.MyGridView
android:id="#+id/gridv_inner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/top_bar"
android:layout_gravity="center"
android:clickable="false"
android:gravity="center"
android:numColumns="7"
android:stretchMode="columnWidth" />
</RelativeLayout>
<Button
android:id="#+id/monthBtn"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent" />
</RelativeLayout>
And in java file
MyGridView viewHolder.gridView = (MyGridView) v.findViewById(R.id.gridv_inner);
I have the following layout file:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/second_grey">
<RelativeLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:id="#+id/categoryName"
android:text="#string/sport"
android:textSize="24sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="12dp"
android:padding="2dp"
android:id="#+id/button"
android:text="#string/see_more"
android:textSize="12sp"
android:background="#drawable/blue"
android:textColor="#android:color/white" />
</RelativeLayout>
<LinearLayout
android:orientation="horizontal"
android:baselineAligned="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="0.99">
<RelativeLayout
android:layout_height="wrap_content"
android:background="#drawable/card"
android:layout_width="0dp"
android:layout_weight="0.33">
<com.favega.groups.SquareLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imgContainer1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:id="#+id/img1"
android:src="#drawable/img_football" />
</com.favega.groups.SquareLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/football"
android:padding="8dp"
android:id="#+id/tv1"
android:layout_below="#+id/imgContainer1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<RelativeLayout
android:layout_height="wrap_content"
android:background="#drawable/card"
android:layout_width="0dp"
android:layout_weight="0.33">
<com.favega.groups.SquareLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imgContainer2">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:id="#+id/img2"
android:src="#drawable/img_football" />
</com.favega.groups.SquareLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/basketball"
android:padding="8dp"
android:id="#+id/tv2"
android:layout_below="#+id/imgContainer2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<RelativeLayout
android:layout_height="wrap_content"
android:background="#drawable/card"
android:layout_width="0dp"
android:layout_weight="0.33">
<com.favega.groups.SquareLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imgContainer3">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:id="#+id/img3"
android:src="#drawable/img_football" />
</com.favega.groups.SquareLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tennis"
android:padding="8dp"
android:id="#+id/tv3"
android:layout_below="#+id/imgContainer3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</LinearLayout>
And I put it in a ListView in this layout:
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/categories"></ListView>
then, with a simple Adapter, I just set the layout of each element to the layout file.
It looks like this:
But if I just do instead of the ListView, it looks like this
EDIT: Notice the change of Sport for Fútbol, that's just my mistake, it has nothing to do with the actual layout.
EDIT2:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
Category category = mList.get(position);
if (convertView == null)
convertView = ((Activity) mContext).getLayoutInflater().inflate(R.layout.category, parent, false);
TextView categoryName = (TextView) convertView.findViewById(R.id.categoryName);
categoryName.setText(category.name);
categoryName.setTypeface(Typeface.createFromAsset(mContext.getAssets(), "fonts/Roboto-Italic.ttf"));
return convertView;
}
EDIT3:
package com.favega.groups;
import android.annotation.TargetApi;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
public class SquareLayout extends LinearLayout {
public SquareLayout(Context context) {
super(context);
}
#TargetApi(11)
public SquareLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public SquareLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int size = width > height ? height : width;
setMeasuredDimension(size, size);
}
}
int size = width > height ? height : width;
setMeasuredDimension(size, size);
Clearly you are setting Measured Dimension to the smallest of width and height!
Since you gave width and height as match_parent, we can assume that one of the value got as zero from parent. As your intention is to make a square image, I prefer you relate this SquareLayout to the child(image) itself rather that the parent. By using wrap_content.
Set each tag of view in converView.setTag(viewName); before returning covertView.
Try May help.Thanks!!