TextView which is inside a RelativeLayout is not visible inside ScrollView - android

I want to display 4 recycler views and textviews inside the relative layout. The recyclerviews are visible but the textview is not visible.I have tried to put the relativelayout inside a linearlayout but no success. I want to clarify that the recyclerviews are horizontal. Thanks in advance please guide.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none"
android:fillViewport="true">
<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:id="#+id/content_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.himesh.icm.MainActivity"
tools:showIn="#layout/app_bar_main"
android:orientation="vertical">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some Random Text"
android:clickable="true"
android:textColor="#color/colorAccent"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:layout_marginTop="200dp"
android:scrollbars="vertical" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:layout_marginTop="50dp"
android:scrollbars="vertical"
android:layout_below="#+id/recycler_view1"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:layout_marginTop="50dp"
android:scrollbars="vertical"
android:layout_below="#+id/recycler_view2"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:layout_marginTop="50dp"
android:scrollbars="vertical"
android:layout_below="#+id/recycler_view3"/>
</RelativeLayout>
</ScrollView>

Remove: tools:showIn="#layout/app_bar_main" and app:layout_behavior="#string/appbar_scrolling_view_behavior" from RelativeLayout
try this layout:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.himesh.icm.MainActivity">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<RelativeLayout
android:id="#+id/content_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Some Random Text"
android:textColor="#color/colorAccent" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="200dp"
android:clipToPadding="false"
android:scrollbars="vertical" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/recycler_view1"
android:layout_marginTop="50dp"
android:clipToPadding="false"
android:scrollbars="vertical" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/recycler_view2"
android:layout_marginTop="50dp"
android:clipToPadding="false"
android:scrollbars="vertical" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/recycler_view3"
android:layout_marginTop="50dp"
android:clipToPadding="false"
android:scrollbars="vertical" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>

Related

How to make SwipeRefreshLayout wrap_content?

How to make SwipeRefreshLayout wrap_content?
Here is my layout
mydialog_fragmet.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
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="wrap_content">
<LinearLayout
android:id="#+id/contentLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/appBackgroundColorLight"
android:orientation="vertical"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:paddingTop="16dp"
android:scrollbarSize="2dp"
android:scrollbarStyle="outsideOverlay"
android:scrollbarThumbVertical="#color/colorAccent"
/>
<include layout="#layout/view_add_place_button"/>
</LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>
In result LinearLayout id/contentLayout becomes match_parent. Here is screenshot :
But when I use the layout without SwipeRefreshLayout content is wrap_content:
mydialog_fragmet.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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="wrap_content"
android:background="#color/appBlue"
android:orientation="vertical"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/placesRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:paddingTop="16dp"
android:scrollbarSize="2dp"
android:scrollbarStyle="outsideOverlay"
android:scrollbarThumbVertical="#color/colorAccent"
app:maxHeight="300dp"
/>
<include layout="#layout/view_add_place_button"/>
</LinearLayout>
Here is right result:
The solution for this specific behavior is to wrap the view in a parent layout, using a specific dp is only recommended if you want it on that size
<LinearLayout
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:background="#color/appBackgroundColorLight"
android:orientation="vertical">
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/contentLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/appBackgroundColorLight"
android:orientation="vertical"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:paddingTop="16dp"
android:scrollbarSize="2dp"
android:scrollbarStyle="outsideOverlay"
android:scrollbarThumbVertical="#color/colorAccent"
/>
<include layout="#layout/view_add_place_button"/>
</LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>
Found solution:
<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="wrap_content"
android:orientation="vertical"
>
<com.example.views.MaxHeightSwipeRefreshLayout
android:id="#+id/swipeRefreshLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:maxHeight="382dp">//size 382 = 300(RecyclerView) + 82 (addButton)
<com.example.views.MaxHeightNestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:maxHeight="382dp">
<LinearLayout
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="wrap_content"
android:background="#color/appBackgroundColorLight"
android:orientation="vertical"
tools:background="#color/appBlue"
>
<com.example.views.MaxHeightRecyclerView
android:id="#+id/placesRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:paddingTop="16dp"
android:scrollbarSize="2dp"
app:maxHeight="300dp"
android:scrollbarStyle="outsideOverlay"
android:scrollbarThumbVertical="#color/colorAccent"
/>
<include layout="#layout/view_add_place_button"/>
</LinearLayout>
</com.example.views.MaxHeightNestedScrollView>
</com.example.views.MaxHeightSwipeRefreshLayout>
</LinearLayout>
Where MaxHeight* view is from https://stackoverflow.com/a/48728490/2425851

where can I add include in my xml file?

I'm trying to add a toolbar in my xml files.
So I made toolbar.xml and I need to include it in upload.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_upload"
android:layout_width="match_parent"
android:padding="10dp"
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="com.sk.mf.Upload"
android:background="#fff">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageToUpload"
android:layout_gravity="center_horizontal"
android:layout_width="150dp"
android:layout_height="150dp" />
<EditText
android:id="#+id/etUploadName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/bUploadImage"
android:text="Upload Image"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
the problem is, anywhere I place it I had error like:
scrollview can host only one direct child (inside scrollview)
multiple root tags (after scrollview)
...
my question here is, where and how can I put the code below in the xml above?
<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">
<include android:id="#+id/toolbar" layout="#layout/toolbar"></include>
</RelativeLayout>
try this : wrap your layout in LinearLayout and the toolbar tag
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar"></include>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_upload"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:padding="10dp"
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="com.sk.mf.Upload">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageToUpload"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center_horizontal" />
<EditText
android:id="#+id/etUploadName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/bUploadImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Upload Image" />
</LinearLayout>
</ScrollView>
</LinearLayout>

Textview doesnot shows up above RecyclerView in nestedscrollview android

<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.tech.world.MainActivity"
tools:showIn="#layout/app_bar_main_activity"
android:fillViewport="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<TextView
android:id="#+id/textView"
android:textColor="#43a047"
android:typeface="serif"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="#string/app"
android:gravity="center"
android:textStyle="bold"
android:textSize="15sp" />
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/recyclerview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_marginTop="10dp"
android:divider="#color/colorAccent"
android:dividerHeight="1dp" />
</RelativeLayout> </android.support.v4.widget.NestedScrollView>
now the textview above dont show up,i have to scroll a bit down to see the textview,also i am using toolbar which expands and contacts in coordinator layout due to this
app:layout_behavior="#string/appbar_scrolling_view_behavior"
i want to make sure textview is visible on start without scrolling a bit to see it
Set android:focusableInTouchMode="true" to your recycler view's parent layout i.e your relative layout.
Check this post.
Switching to ConstraintLayout was my solution
My Working Code using CoordinatorLayout
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true"
tools:context=".PostsActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/MyMaterialTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/MyMaterialTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingLeft="5dp"
android:paddingRight="5dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<TextView
android:id="#+id/txtUserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="User"
android:textAlignment="center"
android:textSize="25sp"
android:textStyle="bold"
android:layout_marginBottom="#dimen/padding_10"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:orientation="vertical"
android:scrollbars="vertical"
android:layout_below="#+id/txtUserName"/>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

How to make view over recyclerview permanent example not scroll in nestedscrollview

Hi i want to make view not scroll able over recyclerview,for example a textview above recyclerview in nestedscrollview,how can i make textview not scrolling while recycler scroll as usual
my code
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:fillViewport="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:focusableInTouchMode="true"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<TextView
android:id="#+id/textView"
android:textColor="#43a047"
android:typeface="serif"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="#string/app"
android:gravity="center"
android:textStyle="bold"
android:textSize="15sp" />
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_marginTop="5dp"
android:divider="#color/colorAccent"
android:dividerHeight="1dp" />
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
how to make textview not scroll
You have the TextView inside the NestedScrollView. So your TextView scrolls because of that parent view. Something like that would work:
<?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="match_parent"
android:focusableInTouchMode="true" >
<TextView
android:id="#+id/textView"
android:textColor="#43a047"
android:typeface="serif"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="#string/app"
android:gravity="center"
android:textStyle="bold"
android:textSize="15sp" />
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_marginTop="5dp"
android:divider="#color/colorAccent"
android:dividerHeight="1dp" />
</RelativeLayout>
The RecyclerView itself scrollable, so just remove the NestedScrollView
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:focusableInTouchMode="true"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<TextView
android:id="#+id/textView"
android:textColor="#43a047"
android:typeface="serif"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="#string/app"
android:gravity="center"
android:textStyle="bold"
android:textSize="15sp" />
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_marginTop="5dp"
android:divider="#color/colorAccent"
android:dividerHeight="1dp" />

Android HorizontalScrollView nesting HorizontalScrollView

I want to have a HorizontalScrollView containing a HorizontalScrollView.
This is my current Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_main"
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="com.example.erik.playground.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:clipChildren="false"
android:nestedScrollingEnabled="true"
android:layout_marginTop="40dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_height="20dp"
app:cardElevation="9dp"
android:layout_width="500dp"/>
<HorizontalScrollView
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_marginTop="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<android.support.v7.widget.CardView
android:layout_height="20dp"
app:cardElevation="9dp"
android:layout_width="600dp"/>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
Right now the inner HorizontalScrollView does not scroll and only the outer one scrolls.
Is there a possibility to make that work?

Categories

Resources