Scrollable layout in FrameLayout - android

I've a FrameLayout on my android app but I can't make to scrollable a part of this layout...I've tried some solutions to fix this problem, but I can't find a patch!
Pratically, I would like to see scrollable the linearlayout (maybe, linearlayout is not necessary) between Scrollview tags.
This is my xml:
<FrameLayout 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"
tools:context="com.robertot.timereport.com.robertot.timereport.Pages.YearlyStatFragment">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
// Some views to scroll
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/bottom_linear"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_gravity="bottom"
style="#android:style/Widget.Holo.Light.ActionBar.Solid">
<Spinner
android:id="#+id/spnYearY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"/>
<ImageButton
android:id="#+id/btnFilter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_search"
android:background="#null"
android:layout_weight="1"
android:layout_gravity="center_vertical"/>
</LinearLayout>
</FrameLayout>
Thanks!! :)

In a FrameLayout all the content views are one on top of the other, so the normal behaviour is that the Linear Layout hides part of the previous Scrollview. Why don't you use a LinearLayout or a Relative Layout instead of the FrameLayout?

Thanks to #Fernando! I've solved my problem, as is:
<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"
tools:context="com.robertot.timereport.com.robertot.timereport.Pages.YearlyStatFragment">
<ScrollView
android:id="#+id/scrollyear"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
//Some views
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/bottom_linear"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_gravity="bottom"
style="#android:style/Widget.Holo.Light.ActionBar.Solid">
<Spinner
android:id="#+id/spnYearY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dp"/>
<ImageButton
android:id="#+id/btnFilter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_search"
android:background="#null"
android:layout_weight="1"
android:layout_gravity="center_vertical"/>
</LinearLayout>
</LinearLayout>

You can surround the view that you want to be scrollable with a ScrollView, as below:
<ScrollView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff">
// the views here that you want to make scrollable
</ScrollView>

Related

Align a LinearLayout at the bottom of a RelativeLayout

I have these nested layouts and i want to put a LinearLayout anchored to the bottom (internally) of the first RelativeLayout but i don't know how to do it. Can someone help me? I want something like this: https://imgur.com/eCCYS8Q
<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="#fff"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:weightSum="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.65"
android:background="#drawable/background_gradient">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.35">
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
To make layout_weight work properly set android:layout_height="0dp" for both relative layouts
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.65"
android:background="#color/colorPrimary">
<LinearLayout
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_marginTop="10dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_height="match_parent"
android:background="#color/colorAccent">
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.35"
android:background="#color/colorPrimaryDark">
</RelativeLayout>
</LinearLayout>
change 10dp to whatever suits you.
I set the colors just to distinguish the layouts
If you want to align a view to the bottom of a relative layout you need to use
android:layout_alignParentBottom="true"
in the view that is going to be stuck at the bottom of the relative
Align LinearLayout to the bottom using id.
take the id of relative layout and then align LinearLayout to the bottom at the end of relative layout id.
Since you need weighttoo in your UI so This will work for you
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:weightSum="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.65"
android:background="#drawable/background_gradient">
<LinearLayout
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content">
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.35">
</RelativeLayout>
</LinearLayout>

Android ScrollView not centering

I have the following code:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/activity_list_films">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:onClick="back"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
However the button, and the stuff that I add to the inner LinearLayout dynamically, all appear on the top left of the screen. What am I doing wrong?
I have tried putting layout_gravity="center" and gravity="center" in the inner LinearLayout too, but this does not help.
Thanks.
Your LinearLayout width is wrap_content : it doesn't take the whole width of the screen and cannot center content horizontally. So change it to android:layout_width="match_parent" also set layout_gravity to center_horizontal android:layout_gravity="center_horizontal"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/activity_list_films"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="back"
android:text="Back" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Try this
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity = "center"
android:gravity="center"
android:id="#+id/activity_list_films">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:onClick="back"/>
</LinearLayout>
</ScrollView>
Please set width -> match_parent of LinearLayout
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"<!--change this one -->
android:layout_height="wrap_content"
android:id="#+id/activity_list_films">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:onClick="back"/>
</LinearLayout>
Otherwise you can set you LinesrLayout in center. It will be in center of your ScrollView.
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/activity_list_films"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:onClick="back"/>
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:fillViewport="true"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:id="#+id/activity_list_films">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:onClick="back"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
You are missing fillViewPort for ScrollView
You not need to change Height of your Linear Layout inside Scroll View.
If you do that the content inside Scroll View will not scroll.
You just need to give android:layout_gravity="center_horizontal" inside Linear Layout other than keep all the same.
Follow below Layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/activity_list_films"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="back"
android:text="Back" />
</LinearLayout>
</ScrollView>
</LinearLayout>

How to avoid nested LinearLayout or RelativeLayout

i am trying to create something like a list inside a cardview in a recyclerview.
It is a detail box which should be filled programatically in the getholderview function of the recycler adapter.
Now i came up with the following code, but somehow i get a feeling this is not very performant.
How can i avoid a lot of nested layouts to realize this and should i even try to avoid it in this case?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
<RelativeLayout ...>
... Other content Title
<ImageView
android:id="#+id/profileImage"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/ic_loyalty_black_24dp"
/>
<TextView
android:id="#+id/cell_textView"
android:layout_toRightOf="#id/profileImage"
android:layout_toEndOf="#id/profileImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:focusable="true"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="#+id/fat_bar_text"
android:layout_toRightOf="#id/profileImage"
android:layout_toEndOf="#id/profileImage"
android:layout_below="#id/cell_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fett:"
android:layout_marginTop="3dp"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/detailBox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_below="#id/fat_bar_text"
android:gravity="bottom">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_gravity="start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Value:"/>
<TextView
android:layout_weight="0.5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="500€"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_gravity="start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Test:"/>
<TextView
android:layout_weight="0.5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="blah"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
The root LinearLayout is unnecessary, use the CardView as the root element.
the second LinearLayout is unnecessary too if you don't have touch event or style on it. replace it with layout_below attr in the child element
you can put the innermost LinearLayout which contain two TextView into an another xml file, and include them with <include> tags, this can make your code more simple and clean. and easy to reuse. like this
mytext.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_gravity="start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:text="Value:"/>
<TextView
android:layout_weight="0.5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="500€"/>
</LinearLayout>
cardview.xml
<include
layout="#layout/mytext"
android:id="#+id/mytext1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<include
layout="#layout/mytext"
android:id="#+id/mytext2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Please, consider to use Constraint Layout. It was created to provide flexibility of building layouts of any complexity. It reduces number of nested layout. One more link

ScrollView with weight not working for me

I have a parent linearlayout with two scrollview inside it and I am using weight attribute inside scroll vies but this is not working for me I mean it's not providing any scrolling. Here is the code, how can I make it work?
LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<ScrollView
android:layout_width="0dp"
android:layout_height="match_parent"
android:scrollbars="horizontal"
android:fillViewport="true"
android:layout_weight="1"
android:layout_marginRight="8dp">
<LinearLayout
android:id="#+id/linearLayoutSizes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="#dimen/product_detail_items_margin_left"
>
</LinearLayout>
</ScrollView>
<ScrollView
android:layout_width="0dp"
android:layout_height="match_parent"
android:scrollbars="horizontal"
android:fillViewport="true"
android:layout_weight="1"
android:layout_marginLeft="8dp">
<LinearLayout
android:id="#+id/linearLayoutColors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioGroup
android:id="#+id/colorsGroup"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RadioGroup>
</LinearLayout>
</ScrollView>
</LinearLayout>
Experimented your code and found the issue.
From your code, it does seems like what you want is a Horizontal Scroll. But you are using normal ScrollView which is by default Vertical Scroll. Consider changing ScrollView to HorizontalScrollView.
i.e. from
<ScrollView
android:layout_width="0dp"
android:layout_height="match_parent"
android:scrollbars="horizontal"
android:fillViewport="true"
android:layout_weight="1"
android:layout_marginRight="8dp">
to
<HorizontalScrollView
android:layout_width="0dp"
android:layout_height="match_parent"
android:scrollbars="horizontal"
android:fillViewport="true"
android:layout_weight="1"
android:layout_marginRight="8dp">
Do change on both your scroll view. Cheers!
Since you are fixing weight for the scrollView it thinks that the data should never exceed the height and hence it doesnt scroll. Just cover the scrollviews with frame layouts like below and try.
LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<FrameLayout android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="horizontal"
android:fillViewport="true"
android:layout_weight="1"
android:layout_marginRight="8dp">
<LinearLayout
android:id="#+id/linearLayoutSizes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginLeft="#dimen/product_detail_items_margin_left"
>
</LinearLayout>
</ScrollView>
<FrameLayout>
<FrameLayout android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="horizontal"
android:fillViewport="true"
android:layout_weight="1"
android:layout_marginLeft="8dp">
<LinearLayout
android:id="#+id/linearLayoutColors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioGroup
android:id="#+id/colorsGroup"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RadioGroup>
</LinearLayout>
</ScrollView>
</FrameLayout>
</LinearLayout>
See if this works.
See if the following code works for you:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<ScrollView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="8dp"
android:layout_weight="1"
android:fillViewport="true"
android:scrollbars="horizontal">
<LinearLayout
android:id="#+id/linearLayoutSizes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"></LinearLayout>
</ScrollView>
<ScrollView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:layout_weight="1"
android:fillViewport="true"
android:scrollbars="horizontal">
<LinearLayout
android:id="#+id/linearLayoutColors"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioGroup
android:id="#+id/colorsGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
</RadioGroup>
</LinearLayout>
</ScrollView>
</LinearLayout>
your XML looks ok to me so why dont you try and add a couple of buttons or images to the layouts and try to scroll them?
also dont forget to add this line of code to the very top of your XML file:
<?xml version="1.0" encoding="utf-8"?>
let me know how you get on.

Design a round button over two colored layouts

I'm trying to design something like that :
Currently i'm having issue putting the button at "the middle" of the two layouts reunion point. Is there a way i do this ?
Thanks.
Here is a way to do it
<?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:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="#FF0000">
<!-- TO ADD CONTENT HERE -->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:background="#FFFF00"
android:weightSum="0.5">
<!-- TO ADD CONTENT HERE -->
</LinearLayout>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#000000"
android:text="TEST"
android:textColor="#FFFFFF"/>
</RelativeLayout>
OUTPUT:
You can change the layout height from android:layout_weight="0.5"
you can use RelativeLayout for your parent and try adding twoLinearLayouts , one below another and set their heights, now place a Button in your RelativeLayout and set android:layout_centerInParent="true" for your Button .
and if you want to use weight for layouts, instead of adding two LinearLayout to your RelativeLayout, add one LinearLayout with weightSum=100 and then add two LinearLayouts to it with custom weights.
for your case , you have to do sth like this :
<RelativeLayout 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="match_parent"
android:orientation="vertical"
android:weightSum="100" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20"
android:background="#f00" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="80"
android:background="#0f0" >
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="20" >
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="test" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>

Categories

Resources