I'm building and android application using android studio and I'm using scroll view in my layout, but the problem is that the scroll view doesn't start after the action bar and leaves huge white space at the bottom (as you can see in the screenshots below).
and here's my code:
<?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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="false"
android:background="#fefcf8"
android:orientation="vertical"
android:weightSum="1">
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_gravity="center"
android:orientation="vertical"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="5dp"
android:orientation="vertical">
<ImageButton
android:id="#+id/info1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="2dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="#android:color/transparent"
android:scaleType="fitCenter"
android:src="#drawable/info1" />
</LinearLayout>
.
.
.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="5dp"
android:orientation="vertical">
<ImageButton
android:id="#+id/info6"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="2dp"
android:layout_weight="1"
android:adjustViewBounds="true"
android:background="#android:color/transparent"
android:scaleType="fitCenter"
android:src="#drawable/info6" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:background="#3a3c3a"
app:menu="#menu/navigation"
app:itemTextColor="#ffffff"/>
</LinearLayout>
As you can see my xml above, i didn't any padding or margin. Yet there's a white space at the bottom and the scroll view starts at the very top of the activity not after the action bar. How to solve this?
Try by putting this inside your ScrollView or FrameLayout containing ScrollView
<ScrollView
...
app:layout_behavior="#string/appbar_scrolling_view_behavior"
...>
Related
I have tried the below code but it doesn't scroll when I rotate my application it will cover most of screen but I need to scroll there it doesn't working there. Is it right way to use scroll view in constraint layout. I have seen this type of example. There it was in working condition I have tried the below code but it doesn't scroll when I rotate my application it will cover most of screen but I need to scroll there it doesn't working there. Is it right way to use scroll view in constraint layout. I have seen this type of example.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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">
<include layout="#layout/tool_bar" />
<ScrollView
android:id="#+id/scrollView2"
android:layout_width="match_parent"
android:layout_height="611dp"
android:layout_marginTop="70dp"
android:scrollbars="none"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<ImageView
android:id="#+id/main_bmi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/bmi" />
<ImageView
android:id="#+id/bmr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/bmr" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:weightSum="2">
<ImageView
android:id="#+id/ideal_weight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ideal" />
<ImageView
android:id="#+id/water_intake"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/water" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:weightSum="2">
<ImageView
android:id="#+id/calorie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/calories" />
<ImageView
android:id="#+id/nutrition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/nutrition" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
follow this layer it might work.put appbarlayout inside ConstraintLayout layout
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--add your scrollview or nestedscrollview here-->
</LinearLayout>
</android.support.design.widget.AppBarLayout>
Your ScrollView height is fixed to 611dp.
If you use a fixed size, then the content should be greater than 611dp to be able to scroll.
You should use
<ScrollView
android:id="#+id/scrollView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
....>
I need to made layout that will contain ScrollView with some dynamic length text and Button in FrameLayout after(below). With long text all is okay, but when text is short button stuck right below bottom of ScrollView text part. I want a FrameLayout in the bottom of the screen when text is short and right below ScrollView text part when text long .
Can it be done?
My XML
<android.support.constraint.ConstraintLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".TeacherFullActivity"
tools:showIn="#layout/app_bar_teacher_full">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
layout="#layout/listview_element_teachers"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/job_title"
style="#style/TitleMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/teacher_job_title" />
<android.support.v7.widget.RecyclerView
android:id="#+id/job_dynamic"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/subject_title"
style="#style/TitleMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/teacher_subject_title" />
<android.support.v7.widget.RecyclerView
android:id="#+id/subject_dynamic"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/workPlace_title"
style="#style/TitleMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/teacher_workplace_title" />
<android.support.v7.widget.RecyclerView
android:id="#+id/workplace_dynamic"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:paddingLeft="35dp"
android:paddingTop="5dp"
android:paddingRight="35dp"
android:paddingBottom="5dp">
<Button
android:id="#+id/teacher_external_link"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/btn_rounded"
android:text="#string/open_link"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:textSize="17sp"
android:textStyle="bold"
/>
</FrameLayout>
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
You can use the android:fillViewport attribute of ScrollView to accomplish this.
<?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"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
</FrameLayout>
</LinearLayout>
</ScrollView>
What this does is make it so that, in the case where the contents of the LinearLayout are not large enough to fill the screen, the LinearLayout is stretched to fill the screen anyway. That gives the FrameLayout room to grow (based on its layout_weight attribute), which in turn lets the Button float to the bottom of the screen (because of its layout_gravity attribute).
In cases where the contents of the LinearLayout are larger than the screen, no stretching happens. The button is pushed off-screen, but appears when you scroll down (with no space between it and the text).
You can try this code for your approach:
<android.support.constraint.ConstraintLayout 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".TeacherFullActivity"
tools:showIn="#layout/app_bar_teacher_full">
<ScrollView
android:id="#+id/scrollview"
android:layout_width="0dp"
android:layout_height="0dp"
app:constraintTop_toTopOf="parent"
app:constraintStart_toStartOf="parent"
app:constraintEnd_toEndOf="parent"
app:constraintBottom_toTopOf="#id/frame">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
layout="#layout/listview_element_teachers"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/job_title"
style="#style/TitleMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/teacher_job_title" />
<android.support.v7.widget.RecyclerView
android:id="#+id/job_dynamic"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/subject_title"
style="#style/TitleMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/teacher_subject_title" />
<android.support.v7.widget.RecyclerView
android:id="#+id/subject_dynamic"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/workPlace_title"
style="#style/TitleMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/teacher_workplace_title" />
<android.support.v7.widget.RecyclerView
android:id="#+id/workplace_dynamic"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<FrameLayout
android:id="#+id/frame"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingLeft="35dp"
android:paddingTop="5dp"
android:paddingRight="35dp"
android:paddingBottom="5dp"
app:constraintTop_toBottomOf="#id/scrollview"
app:constraintStart_toStartOf="parent"
app:constraintEnd_toEndOf="parent"
app:constraintBottom_toBottomOf="parent">
<Button
android:id="#+id/teacher_external_link"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/btn_rounded"
android:text="#string/open_link"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:textSize="17sp"
android:textStyle="bold"
/>
</FrameLayout>
</android.support.constraint.ConstraintLayout>
Explaination:
What i did was took FrameLayout containing Button out side of ScrollView so that it can always be at bottom of the Container.
some what new to android development. Currently, this is the code that I have for my layout:
<FrameLayout 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"
android:theme="#android:style/Theme.NoTitleBar"
tools:context="com.alibaba.weex.com.alibaba.app.WXPageActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:scrollIndicators="right"
android:scrollbars="vertical">
</FrameLayout>
<ProgressBar
android:id="#+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone" />
<TextView
android:id="#+id/index_tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignWithParentIfMissing="true"
android:layout_below="#+id/index_progressBar"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:text="loading...."
android:visibility="gone" />
</FrameLayout>
Can someone help me figure out how to get the vertical scroll bar to show? I've tried changing the all the framelayouts to relativelayouts, and then changing the second framelayout to a ScrollView, but the app crashes once I open it.
Thanks!
I think your layout should be like this .
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scrollbars="vertical"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
>
</FrameLayout>
<ProgressBar
android:id="#+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone" />
<TextView
android:id="#+id/index_tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignWithParentIfMissing="true"
android:layout_below="#+id/index_progressBar"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:text="loading...."
android:visibility="gone" />
</FrameLayout>
</ScrollView>
You can't change scollview in the second framelayout because the scollview can only have one child .
i have an activity in which, on top i have a image slider, below this a layout in the form of grid which covers almost all of my phone's screen and below this at last i have a list view of of size approax half of my phone's screen.(i am using scroll view to scroll elements)
so altogether they are nearly of size twice of my phone's screen.
now whenever i start this activity, the screen automatically gets focused to the bottom part of my elements. in simple words, it does not show me the image slider and upper part of my grid view, it just show the lower part of grid view and the list view.
so i need to scroll it up manually when i start this.
i searched a lot but didn't get any way to focus it automatically to top elements of my activity. please suggest me how to proceed now.
thank in advance.
this is my xml:
<?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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.gehlot.nitin.kitchenspices.Main2Activity"
android:id="#+id/scrollView"
tools:showIn="#layout/app_bar_main2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="10dp"
android:layout_weight="1.0"
android:id="#+id/viewPager"/>
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_weight="1.0"
android:text="Shop catagory"
style="#style/textStyle"
android:layout_gravity="center"
android:gravity="center"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:orientation="vertical"
android:background="#drawable/back"
android:layout_weight="1.0">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1.0">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:layout_margin="5dp"
android:scaleType="fitXY"
android:background="#drawable/back"
android:id="#+id/img1"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:layout_margin="5dp"
android:scaleType="fitXY"
android:background="#drawable/back"
android:id="#+id/img2"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:background="#drawable/back"
android:layout_margin="5dp"
android:scaleType="fitXY"
android:id="#+id/img3"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1.0">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:background="#drawable/back"
android:layout_margin="5dp"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:id="#+id/img4"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:background="#drawable/back"
android:layout_margin="5dp"
android:id="#+id/img5"/>
<ImageView
android:adjustViewBounds="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:scaleType="fitXY"
android:background="#drawable/back"
android:layout_margin="5dp"
android:id="#+id/img6"/>
</LinearLayout>
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="300dp"
android:id="#+id/lv_main"
android:layout_below="#id/img4">
</ListView>
</LinearLayout>
</ScrollView>
Try to add this line to your direct child of your ScrollView in xml. It prevents the auto scrolling to bottom part.
android:descendantFocusability="beforeDescendants"
Example:
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="beforeDescendants"
android:orientation="vertical">
//children views
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Add this to your root layout in xml:
android:id="#+id/root_layout"
In your Acivity.java add:
ViewGroup layout = (ViewGroup) findViewById(R.id.layout);
layout.requestFocus();
I'm beginner in android, and I don't know why this happens.
Without SwipeRefreshLayout the RecyclerView is fully visible, but with SwipeRefreshLayout, the first item isn't shown.
Maybe it's under the toolbar? (The hidden item exists, I checked it in numerous ways.)
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/swipe_layout">
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/MainRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
List:
<?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="wrap_content"
android:orientation="horizontal"
android:paddingTop="8dip"
android:paddingBottom="8dip"
android:paddingLeft="16dip"
android:paddingRight="16dip"
android:weightSum="10"
android:gravity="center">
<ImageView
android:id="#+id/CharacterItemRankImageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="0"
android:src="#drawable/challengemode_medal_bronze"/>
<TextView android:id="#+id/CharacterItemNameTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3.5"
tools:text="Name"/>
<TextView android:id="#+id/CharacterItemPointsTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_weight="6"
tools:text="Points"/>
<ImageButton
android:id="#+id/CharacterItemRemoveButton"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="0.5"
android:src="#drawable/ic_delete_grey600_48dp"
android:scaleType="fitXY" style="#style/Widget.AppCompat.Button.Borderless" />
</LinearLayout>
I guess you are using a CoordinatorLayout as root of your view. In that case, what is happenning is that the toolbar is being hidden by the first item of your RecyclerView.
To fix this issue, you just need to add the layout behavior to the SwipeRefreshLayout and remove from your RecyclerView.
<android.support.v4.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/swipe_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="#+id/MainRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>