I have used Coordinator Layout as a parent layout for anchoring one image and Linear layout as a child. So may question is how can i make screen Scrollable? Linear Layout is divided into two child with weights. Please find the xml code below:
I have tried this with Nested Scroll view and normal scroll view, but screen division get disturbed which is done with linear layout weight sum property.
<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:background="#drawable/bg_welcome">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10"
tools:context="com.rblbank.mobank.chequebook.fragments.NewChequeBookFragment">
<TextView
android:id="#+id/tvNewChequeBook"
style="?attr/title_1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:gravity="center"
android:padding="#dimen/activity_horizontal_margin"
android:text="#string/new_cheque_book_title_note"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6"
android:background="#color/white_color"
android:gravity="center_vertical"
android:orientation="vertical"
android:padding="#dimen/padding_12">
<TextView
android:id="#+id/tvNewChequeBookInvestmentAccountTitle"
style="?attr/title_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:text="#string/investment_account"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Small"
android:textColor="#color/black_color" />
<TextView
android:id="#+id/tvNewChequeBookInvestmentAccount"
style="?attr/newChequeBookInvestmentAccount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:textColor="#color/arrow_grey" />
<View
android:id="#+id/vwAccountDetailsHorizontalDivider"
android:layout_width="match_parent"
android:layout_height="#dimen/divider_width"
android:layout_marginBottom="#dimen/minimum_margin"
android:layout_marginTop="#dimen/minimum_margin"
android:background="#color/home_functionality_divider" />
<TextView
android:id="#+id/tvNewChequeBookAddressNote"
style="?attr/newChequeBookAddressNoteStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_horizontal_margin" />
<TextView
android:id="#+id/tvNewChequeBookAddress"
style="?attr/newChequeBookAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_horizontal_margin" />
<Button
android:id="#+id/btnewChequeBookDeliverCheque"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:text="#string/deliver_cheque_book_to_above_address" />
<TextView
android:id="#+id/tvNewChequeBookAddressChanged"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:text="#string/postal_address_has_changed" />
</LinearLayout>
</LinearLayout>
<ImageView
android:id="#+id/ivNewChequeBookPostBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_action_submit"
app:layout_anchor="#id/tvNewChequeBook"
app:layout_anchorGravity="center_horizontal|bottom"
app:layout_behavior="com.rblbank.mobank.ScrollAwareFABBehavior" />
</android.support.design.widget.CoordinatorLayout>
Make your layout like this.
<CoordinatorLayout>
<AppBarLayout>
//Toolbar and other contents will come here
</AppBarLayout>
<Recycler Or Nested Scroll View>
<LinearLayout
app:layout_behavior="#string/appbar_scrolling_view_behavior">
//This is used when you have a collapsing toolbar and you need to add a scrolling behaviour for your views.
<LinearLayout/>
<Recycler Or Nested Scroll View/>
Also if you are not using collapsing toolbar or other animations you can add the views inside AppBarLayout itself.
use scroll view like this
<CoordinatorLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout>
</LinearLayout>
</ScrollView>
<CoordinatorLayout>
Related
Is it possible to overlay 2 textviews in one linear layout? I want them in the same position but they have to be in one linear layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp">
<Textview
android:id="#+id/tv1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Textview
android:id="#+id/tv2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
You don't need Linear layout there if you want it to be overlapping .. you can use RelativeLayout or FrameLayout
<RelativeLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asdf"/>
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sfgsfg"/>
sadly they can't in LinearLayout, but you can use FrameLayout - without an easy option for placing child Views next to each other (as in LinearLayout) - or RealtiveLayout - which have some params for placing one View next to/above another when needed
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp">
<Textview
android:id="#+id/tv1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<Textview
android:id="#+id/tv2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
I have a scrollview, but I want a linear layout with 3 buttons that stay on top when scrolling. So it should function kind of like navigation bar that sticks to the top.
Current code:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<androidx.gridlayout.widget.GridLayout
android:id="#+id/gridRoot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:columnCount="2"
app:rowCount="32">
</androidx.gridlayout.widget.GridLayout>
</ScrollView>
Place the ScrollView inside a vertical LinearLayout, and have those three buttons added above the ScrollView.
<LinearLayout android:orientation="vertical">
<YourButtonsHere/>
<ScrollView/>
</LinearLayout>
There are several ways to achieve this, but the easiest one is to have a vertical LinearLayout as the parent. You then put the buttons in a horizontal linearlayout, so that they are on the top and act as a 'bar'.
Then you only need to add the scrollview.
That way the user can scroll the scrollview and the 3 buttons stay on top of the scroll view.
<!-- vertical linear layout as parent -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Place buttons in a horizontal linearlayout, so that you have a bar on the
top-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- buttons width=0 + same layout_weight, that way the width of the
linear layout is equally distributed between the 3 buttons-->
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height=""
android:layout_weight="1"/>
<Button
android:layout_width="0dp"
android:layout_height=""
android:layout_weight="1"/>
</LinearLayout>
<!-- your scroll view here-->
<ScrollView
android:layout_width=""
android:layout_height="">
</ScrollView>
</LinearLayout>
XML:
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="0dp"
android:gravity="center"
android:layout_weight="1"
android:orientation="horizontal">
<com.sharedcode.widgets.CustomButton
style="#style/FlatButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn1"
android:layout_margin="#dimen/spacing_6" />
<com.sharedcode.widgets.CustomButton
style="#style/FlatButton"
android:layout_width="wrap_content"
android:text="btn2"
android:layout_height="wrap_content"
android:layout_margin="#dimen/spacing_6" />
<com.sharedcode.widgets.CustomButton
style="#style/FlatButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="btn3"
android:layout_margin="#dimen/spacing_6" />
</androidx.appcompat.widget.LinearLayoutCompat>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="9"
tools:context=".MainActivity">
//in your case this is GridView
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/gridRoot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:columnCount="2"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:rowCount="32"
app:spanCount="2"
tools:listitem="#layout/row_exam" />
</ScrollView>
</androidx.appcompat.widget.LinearLayoutCompat>
I want to create a list of image buttons and i added 3 of them but only two are visible and i want to make them scrollable. I tried using ScrollView but it still doesnt work. I also tried replacing RelativeLayout in the first line with ScrollView but the app started to crash.
Here's the xml file:
<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"
android:background="#drawable/back"
tools:context=".home">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/download"
android:layout_gravity="center_horizontal"
/>
</RelativeLayout>
</ScrollView>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="230dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/geo" />
</RelativeLayout>
</ScrollView>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4300dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/images"
android:contentDescription="TODO" />
</RelativeLayout>
</ScrollView>
Make the width and height of first scroll view as wrap content .If it doesn't work then
Make only a single scroll view.Then make a Linear layout as its child and in linear layout having vertical orientation add your 3 image buttons.
I set the ScrollView in my LinearLayout. I want a TextView to scroll. I tried to use ScrollView in xml like this. But, it's not working.
<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"
android:padding="8dp"
tools:context=".SubpagesLayout.ReportResult">
<!-- first scrollView -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/edit_corner_blue"
android:gravity="center"
android:text="#string/personalInformation"
android:textColor="#android:color/white" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/birthDate" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" TestData" />
</LinearLayout>
<!-- second scrollView -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textOphthalmoscopy"
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="standard instrth various settings that allow focusing and adjustment of the light source to accommodate the viewer and to evaluate various features of the fundus." />
</ScrollView>
</LinearLayout>
</ScrollView>
</LinearLayout>
I tried another way like this, but it's still not working too.
android:maxLines = "AN_INTEGER"
android:scrollbars = "vertical"
I used
myTextView.setMovementMethod(new ScrollingMovementMethod());
So how to let TextView to scroll? If it is the second ScrollView.
You might consider using a NestedScrollView along with a ListView for showing the scrollable TextView.
The ListView will have a fixed size so that it'll not take the total height of the items inside.
So the layout may look like this.
<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"
android:padding="8dp"
tools:context=".SubpagesLayout.ReportResult">
<!-- first scrollView -->
<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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/edit_corner_blue"
android:gravity="center"
android:text="#string/personalInformation"
android:textColor="#android:color/white" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/birthDate" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" TestData" />
</LinearLayout>
<ListView
android:layout_width="match_parent"
<!--set a fixed height-->
android:layout_height="80dp"
.. Other attributes
</ListView>
</LinearLayout>
</NestedScrollView>
</LinearLayout>
And the list item of the ListView may contain the TextView you want to scroll. Set the height of the TextView to wrap_content.
<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"
android:padding="8dp"
tools:context=".SubpagesLayout.ReportResult">
<TextView
android:id="#+id/textOphthalmoscopy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="standard instrth various settings that allow focusing and adjustment of the light source to accommodate the viewer and to evaluate various features of the fundus." />
</LinearLayout>
Now your ListView will have a single item and this will scroll with the default behaviour of list.
The whole layout will be scrolled via NestedScrollView.
Hope that helps!
Having a scrollView inside another scrollView is not a good prictice but ant a you check below links it may fix you problem.
Solution 1
Solution 2
The XML file for my activity has ScrollView as rootview . it has two relative layout as child . this is giving me error .
I want to keep two relative layout as child view in ScrollView of this XML.
Here is my XML file:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#drawable/bg_about">
<TextView
android:id="#+id/textAboutUs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:gravity="clip_vertical"
android:text="#string/about_us"
android:textColor="#ffffff" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/relativeLayout"
android:layout_weight="0.96"
android:background="#E8E9EA" />
</RelativeLayout>
</ScrollView>
I want it to be scroll-able either the whole layout or just the second layout.
Wrap one of your layouts in a ScrollView (or HorizontalScrollView, if you want it that way) or if you want the ScrollView at the root, place it at the start of your code. Either way, you have to ensure that there is only ONE child element for the ScrollView (this child element can have many child elements) - below is an example of the whole thing being scrollable:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#drawable/bg_about">
<TextView
android:id="#+id/textAboutUs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:gravity="clip_vertical"
android:text="#string/about_us"
android:textColor="#ffffff" />
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
</ScrollView>