Android - LinearLayout not scrolling inside ScrollVIew - java

I have the problem with LinearLayout not being scrollable inside the ScrollView, instead it just appears to go beyond the screen frame (look closely at the bottom of the screenshot linked below).
I have the following structure in my XML layout:
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>
<!-- More TextInputLayouts -->
</LinearLayout>
</ScrollView>
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src = "#drawable/ic_done"
android:tint = "#color/white"/>
</android.support.design.widget.CoordinatorLayout>
No solutions found were able to fix the problem: I tried fillViewPort="true" and it didn't do it for me, I also tried commenting CoordinatorLayout - same result, layout_heights seem to be set properly as well.
Moreover, I tried to adjust LinearLayout's height progrmatically, what didn't help just as well.
I'm stuck with this problem for a while now and would really appreciate any help in this regard :)
Screenshot

It works for me with the XML below. I didn't change much, just added an orientation to the LinearLayout and some margin and awful background colors so you can see which view is moving and which is fixed while you scroll.
When you drag up and down, the teal box (the LinearLayout) moves up and down as expected within the red box (the ScrollView). If your LinearLayout is taller than the screen, it will go off the bottom, that's the point of the ScrollView.
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:background="#ff0000"
android:scrollbars="vertical">
<LinearLayout
android:orientation="vertical"
android:layout_margin="16dp"
android:background="#008080"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
</android.support.design.widget.CoordinatorLayout>

you put linear layout orientation in xml code
like..
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
or
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>

Try 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:background="#color/whiteColor"
android:orientation="vertical"
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=".MainActivity">
<!-- vertical ScrollView to make all the items or views scrollable -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none">
<!-- LinearLayout Inside ScrollView -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- create a Linear Layout with horizontal orientation and weightSum property -->
<LinearLayout
android:id="#+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:weightSum="2">
<EditText
android:id="#+id/lastName"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:layout_weight="1.4"
android:background="#color/editTextBack"
android:hint="Last Name"
android:imeOptions="actionNext"
android:paddingLeft="10dp"
android:singleLine="true"
android:textColor="#color/blackColor" />
</LinearLayout>
<!-- create a Linear Layout with horizontal orientation and weightSum property -->
<LinearLayout
android:id="#+id/thirdLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:weightSum="2">
<!-- place one TextView and one EditText inside layout using weight property -->
<TextView
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginRight="10dp"
android:layout_weight="0.6"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="Address"
android:textColor="#color/blackColor" />
</LinearLayout>
</ScrollView>
</LinearLayout>

Related

My scrollview in a linear layout is pushing up other widgets above it

So my button and edit text widgets keep getting pushed upwards when I dynamically add text views to my scroll view. So the scrollview's height is also being bumped up. How do I just make sure the scroll view is fixed, I've looked at similar questions and tried implementing it but not working.
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:windowSoftInputMode="stateVisible|adjustPan"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="#+id/name"
android:hint="Enter name"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"/>
<EditText
android:id="#+id/phone_num"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:hint="Enter Phone Number"/>
<Button
android:id="#+id/submit"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:text="#string/submit_contact"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:isScrollContainer="false"
android:fillViewport="true">
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
Try to set all heights to 0dp and give the wheightSum to the parent view. To have a fixed height of the button surround it with a LinearLayout.
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:windowSoftInputMode="stateVisible|adjustPan"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2.75"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="#+id/name"
android:hint="Enter name"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"/>
<EditText
android:id="#+id/phone_num"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:hint="Enter Phone Number"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:orientation="vertical">
<Button
android:id="#+id/submit"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:text="test"/>
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:isScrollContainer="false"
android:fillViewport="true">
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
Try it out, If it dont work you may post what you have tried alredy and the code how you ad the textViews dynamicly. Good luck

Scroll View does not work on OS 4.1.2

I have added scroll view in a layout, scroll view has attributes of match parent for height and width also fillViewPort is true for scroll view, its working in other devices but not working in android OS 4.1.2.
Layout :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:background="#android:color/white"
android:layout_height="match_parent">
<!-- <RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!– <ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.5"
android:scaleType="fitXY"
android:src="#drawable/page"/>–>
</RelativeLayout>-->
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="8">
<TextView
android:id="#+id/main_welcome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/dimen_20dp"
android:text="#string/welcome"
android:textColor="#color/colorPrimary"
android:textStyle="bold" />
<GridView
android:id="#+id/gridviewMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="#dimen/dimen_40dp"
android:layout_marginLeft="#dimen/dimen_40dp"
android:layout_marginRight="#dimen/dimen_40dp"
android:columnWidth="80dp"
android:horizontalSpacing="#dimen/dimen_10dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"></GridView>
</LinearLayout>
</ScrollView>
<TextView
android:id="#+id/textView20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_centerInParent="false"
android:text="For Any Queries Related To Mobile App Click On Service Request"
android:textAlignment="center" />
</RelativeLayout>
What can be the reason for this? All other scroll views are working in same device just not working on this screen.
Please help.Thank you..
Try Using nestedScrollView instead
<nestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="8">
<TextView
android:id="#+id/main_welcome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/dimen_20dp"
android:text="#string/welcome"
android:textColor="#color/colorPrimary"
android:textStyle="bold" />
<GridView
android:id="#+id/gridviewMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="#dimen/dimen_40dp"
android:layout_marginLeft="#dimen/dimen_40dp"
android:layout_marginRight="#dimen/dimen_40dp"
android:columnWidth="80dp"
android:horizontalSpacing="#dimen/dimen_10dp"
android:numColumns="2"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"></GridView>
</LinearLayout>
</nestedScrollView>

Fitting a Layout inside a Cardview

I have a cardview with radius 10dp
I simply don't want to show the edges of layout.
I'm trying to do this >
I tried card_view:cardPreventCornerOverlap="false" , didn't work............................................................
But I got this;
XML Code: (I fixed, here is the code)
<android.support.v7.widget.CardView
android:padding="6dp"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view_inner"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardBackgroundColor="#color/green"
card_view:cardCornerRadius="8dp"
card_view:cardElevation="3dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.makeramen.roundedimageview.RoundedImageView
card_view:riv_corner_radius="8dp"
android:src="#drawable/picture"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:background="#color/orange"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="text"
android:textAlignment="center"
android:textColor="#color/white" />
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>

Java Android ScrollView buttons always at the top

Hi I have a ScrollView and inside I have a LinearLayout and inside I have a buttons. I don't know how I can have this buttons always at the top when I scroll a view. Now when I scroll view a buttons disappear.
My code :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:weightSum="1">
<LinearLayout
android:id="#+id/LinearLayout02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|bottom"
android:layout_marginTop="0dp">
<Button
android:id="#+id/cpic"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:text="Zdjęcie" />
<Button
android:id="#+id/up"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_weight="1"
android:text="Wyślij" />
</LinearLayout>
<ImageView
android:id="#+id/Imageprev"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_marginTop="58dp" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
what I understand from your question i think you can achieve it by putting your button outside your scroll view inside XML , this would give you a fix button.
hope this helps.

Floating Action Button and CardView

How to add Floating Action Button from the support library for CardView so that when you scroll through the ScrollView, the button has gone up along with the card?
My app
before scrolling
after scrolling
The button should go along with the card
I want to fix this
Layout
<?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"
>
<ScrollView
android:id="#+id/nested_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white_vk_background_color">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/spacing_8"
android:orientation="vertical"
android:padding="#dimen/spacing_16">
<android.support.v7.widget.CardView
android:id="#+id/behavior_card_view"
style="#style/RateCardView"
app:cardBackgroundColor="#color/white"
app:cardCornerRadius="#dimen/spacing_2"
app:cardElevation="#dimen/spacing_8">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="#dimen/spacing_16"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="#dimen/spacing_8">
<TextView
android:id="#+id/description_rate_usd_text_view"
style="#style/AppTextView.RateTextView"
android:text="#string/description_currency_text_view_usd"/>
<TextView
android:id="#+id/rate_usd_text_view"
style="#style/AppTextView.RateTextView"
android:layout_centerHorizontal="true"
android:layout_toRightOf="#id/description_rate_usd_text_view"
tools:text="63.99"
/>
<TextView
android:id="#+id/rate_difference_usd_text_view"
style="#style/AppTextView.DifferenceTextView"
android:layout_toRightOf="#id/rate_usd_text_view"
tools:text="+0.44"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingBottom="#dimen/spacing_8">
<TextView
android:id="#+id/description_rate_eur_text_view"
style="#style/AppTextView.RateTextView"
android:text="#string/description_currency_text_view_eur"/>
<TextView
android:id="#+id/rate_eur_text_view"
style="#style/AppTextView.RateTextView"
android:layout_centerHorizontal="true"
android:layout_toRightOf="#id/description_rate_eur_text_view"
tools:text="71.99"
/>
<TextView
android:id="#+id/rate_difference_eur_text_view"
style="#style/AppTextView.DifferenceTextView"
android:layout_toRightOf="#id/rate_eur_text_view"
tools:text="+0.15"/>
</RelativeLayout>
<TextView
android:id="#+id/rate_update_time_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|start"
android:layout_marginBottom="#dimen/spacing_8"
android:layout_marginLeft="#dimen/spacing_16"
android:layout_marginTop="#dimen/spacing_16"
android:gravity="center"
android:textSize="#dimen/density_font_size_text_date"
tools:text="Date: 15.07.2016"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/converter_card_view"
style="#style/RateCardView"
app:cardBackgroundColor="#color/white"
app:cardCornerRadius="#dimen/spacing_2"
app:cardElevation="#dimen/spacing_8">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="#dimen/spacing_16">
<com.rengwuxian.materialedittext.MaterialEditText
android:id="#+id/converter_ruble_edit_text"
style="#style/RateEditText"
android:hint="#string/description_currency_ruble"
app:met_baseColor="#android:color/black"
app:met_bottomTextSize="#dimen/density_font_size_text_converter"
app:met_errorColor="#color/red_light"
app:met_floatingLabel="normal"
app:met_floatingLabelAlwaysShown="true"
app:met_floatingLabelTextSize="#dimen/density_font_size_text_converter"
app:met_helperText=""
app:met_primaryColor="#color/red_light"
app:met_textColorHint="#android:color/transparent"/>
<com.rengwuxian.materialedittext.MaterialEditText
android:id="#+id/converter_dollar_edit_text"
style="#style/RateEditText"
android:hint="#string/description_currency_dollar"
app:met_baseColor="#android:color/black"
app:met_bottomTextSize="#dimen/density_font_size_text_converter"
app:met_errorColor="#color/red_light"
app:met_floatingLabel="normal"
app:met_floatingLabelAlwaysShown="true"
app:met_floatingLabelTextSize="#dimen/density_font_size_text_converter"
app:met_helperText=""
app:met_primaryColor="#color/red_light"
app:met_textColorHint="#android:color/transparent"/>
<com.rengwuxian.materialedittext.MaterialEditText
android:id="#+id/converter_euro_edit_text"
style="#style/RateEditText"
android:hint="#string/description_currency_euro"
app:met_baseColor="#android:color/black"
app:met_bottomTextSize="#dimen/density_font_size_text_converter"
app:met_errorColor="#color/red_light"
app:met_floatingLabel="normal"
app:met_floatingLabelAlwaysShown="true"
app:met_floatingLabelTextSize="#dimen/density_font_size_text_converter"
app:met_helperText=""
app:met_primaryColor="#color/red_light"
app:met_textColorHint="#android:color/transparent"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
<android.support.design.widget.FloatingActionButton
android:id="#+id/update_floating_action_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/density_spacing_size_floating_action_button"
android:layout_marginRight="#dimen/float_action_button_spacing_8"
android:src="#drawable/ic_cached_white_24dp"
app:fabSize="mini"
app:layout_anchor="#id/behavior_card_view"
app:pressedTranslationZ="6dp"
app:layout_anchorGravity="end|right|center"
/>
</android.support.design.widget.CoordinatorLayout>
Instead of anchoring to behavior_card_view, try anchoring it to something inside behavior_card_view. Ideally, anchor to the bottom of the USD textview. This should prevent the FAB from sliding down.
I would actually create a invisible layout which I can use to anchor but it doesn't affect the UI otherwise.
I can't replicate the issue you are having but the following works as you want:
<RelativeLayout
android:id="#+id/rl1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dp"
android:paddingRight="10dp">
<TextView
android:id="#+id/tv1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:paddingLeft="12dp"
android:text="Setting"
android:focusable="true"
android:focusableInTouchMode="true" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:src="#drawable/ic_add"
app:fabSize="mini"
app:pressedTranslationZ="6dp"
app:layout_anchorGravity="end|right|center"
app:layout_anchor="#id/tv1" />
... a lot of elements here ...
</RelativeLayout>
This RelativeLayout is the only element inside my ScrollView element.

Categories

Resources