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!
Related
I have ViewPager and grid view in my activity.i want when I scroll anywhere on screen both ViewPager and grid view should scroll at a same time.here is my ss :image.now the only few images are displayed in grid view I have 63 images but shows only 9 in grid..2)ss main
xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
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="fill_parent"
android:background="#fff">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginBottom="8dp"/>
<LinearLayout
android:id="#+id/SliderDots"
android:layout_below="#+id/viewPager"
android:orientation="horizontal"
android:gravity="center_vertical|center_horizontal"
android:layout_width="match_parent"
android:layout_height="15dp"/>
<GridView
android:id="#+id/mGridView"
android:layout_width="match_parent"
android:layout_height="554dp"
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"
/>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
Use this code to resolve your problem
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginBottom="8dp"/>
<LinearLayout
android:id="#+id/SliderDots"
android:layout_below="#+id/viewPager"
android:orientation="horizontal"
android:gravity="center_vertical|center_horizontal"
android:layout_width="match_parent"
android:layout_height="15dp"/>
<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" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
You must be use CUSTOM viewpager to achieve vertical scrolling of viewpager because default viewpager doesn't support vertical scrolling with other components.
Check my below code.
public class CustomViewPager extends ViewPager {
private View mCurrentView;
public CustomViewPager(Context context) {
super(context);
}
public CustomViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (mCurrentView == null) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
return;
}
int height = 0;
mCurrentView.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
int h = mCurrentView.getMeasuredHeight();
if (h > height) height = h;
heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void measureCurrentView(View currentView) {
mCurrentView = currentView;
requestLayout();
}
public int measureFragment(View view) {
if (view == null)
return 0;
view.measure(0, 0);
return view.getMeasuredHeight();
}
}
Apply this Class instead of default ViewPager into your fragment. THAT'S IT!!
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>
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);
I am adding a fragment with:getSupportFragmentManager().beginTransaction().replace(R.id.fragment_host, TutorialFragment_.builder().build(), "tutorial").commit();
the FrameLayout is inside a RelativeLayout using:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:padding="0dp"
android:visibility="visible">
<ImageView
android:id="#+id/phone_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginRight="100dp"
android:layout_marginTop="-100dp"
android:adjustViewBounds="true"
android:src="#drawable/phone_tutorial"
android:visibility="invisible" />
<ImageView
android:id="#+id/tuto_image_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"
android:layout_marginTop="60dp"
android:visibility="invisible"
android:src="#drawable/a_capacity" />
<ImageView
android:id="#+id/tuto_image_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="60dp"
android:src="#drawable/a_loan_simulation"
android:visibility="invisible" />
<ImageView
android:id="#+id/tuto_image_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"
android:layout_marginTop="100dp"
android:src="#drawable/a_checklist"
android:visibility="invisible" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="false"
android:src="#drawable/add_btn_circle_center"
android:visibility="gone" />
</RelativeLayout>
<FrameLayout
android:id="#+id/fragment_host"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/mButtons"
android:layout_marginBottom="20dp" />
<LinearLayout
android:id="#+id/mButtons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:layout_margin="15dp"
android:orientation="vertical"
android:padding="15dp"
android:visibility="visible">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.bnpf.androidnative.core.view.FontButton
android:id="#+id/next_tutorial"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="right"
android:text="#string/Generic_Button_Next"
android:textColor="#color/processStepCategoryTextColor" />
<com.bnpf.androidnative.core.view.FontButton
android:id="#+id/skip_tutorial"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="left"
android:text="#string/Generic_Button_Cancel"
android:textColor="#color/processStepCategoryTextColor" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
The TutorialFragment object uses this layout :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/tutorial_viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
No matter what I try, this FrameLayout is always aligned on top of the screen, and I need it to be at the bottom of the screen.
Try by change your layout like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:padding="0dp"
android:visibility="visible" >
.....
<FrameLayout
android:id="#+id/fragment_host"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/mButtons"
android:layout_below="#+id/relLayout"
android:layout_marginBottom="20dp" />
Now view pager will come below 'relLayout' and above 'mButtons'
maybe your RelativeLayout is not full screen,so the FrameLayout will never aligned the bottom of the screen
You can try this,
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_content"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:layout_alignParentBottom="true"
android:id="#+id/tutorial_viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
you can use like this also:
public class MyViewPager extends ViewPager {
public MyViewPager(Context context) {
super(context);
}
public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// find the first child view
View view = getChildAt(0);
if (view != null) {
// measure the first child view with the specified measure spec
view.measure(widthMeasureSpec, heightMeasureSpec);
}
setMeasuredDimension(getMeasuredWidth(), measureHeight(heightMeasureSpec, view));
}
private int measureHeight(int measureSpec, View view) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
// set the height from the base view if available
if (view != null) {
result = view.getMeasuredHeight();
}
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}
}
in xml:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/mButtons"
android:layout_below="#+id/relLayout"
android:background="#0000ff" >
<FrameLayout
android:id="#+id/fragment_host"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="20dp" />
</RelativeLayout>
I'm using ExpandableHeightGridView but I don't manage to calculate the correct height of the gridview, I use custom rows made by an external layout, but it cuts several rows at the bottom,
here's the code:
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) {
if (isExpanded()) {
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;
}
}
Custom Row
<?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:background="#null"
android:orientation="vertical" >
<TextView
android:id="#+id/textViewBioAnno"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center_horizontal"
android:text="TextView"
android:textColor="#color/oro"
android:textSize="20sp" />
<TextView
android:id="#+id/textViewBioTesto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="TextView"
android:textSize="15sp" />
</LinearLayout>
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:background="#color/sfondo"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<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" >
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:layout_marginTop="10dp"
android:scaleType="fitCenter"/>
<ImageButton
android:id="#+id/imageView1"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="12dp"
android:background="#null"
android:scaleType="fitXY" />
</RelativeLayout>
<TextView
android:id="#+id/textViewBio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/bio"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/oro" />
<******.ExpandableHeightGridView
android:id="#+id/mebersListGridView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="15dp"
android:horizontalSpacing="10dp"
android:isScrollContainer="false"
android:numColumns="1"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:src="#drawable/fondo_pagina" />
<ImageView
android:id="#+id/ImageView02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
android:layout_marginRight="70dp"/>
<ImageView
android:id="#+id/ImageView01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginRight="100dp"" />
</LinearLayout>
</ScrollView>
</LinearLayout>
My activity:
...
g = (ExpandableHeightGridView) findViewById(R.id.mebersListGridView);
arrayAdapter = new StandardArrayAdapter(this,
R.layout.activity_bio, arrayList);
g.setExpanded(true);
g.setAdapter(arrayAdapter);
...