Issue with recycler view inside constraint layout - android

I'm trying to create a bottom sheet dialog based on a complex layout.
What I have to achieve is a dialog with an header layout, a recycler view with a list of element and a bottom bar with a couple of buttons.
Here is my code:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Layout injected inside this linear layout container programmatically -->
<LinearLayout
android:id="#+id/header_dialog_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="#id/form_dialog_recycler_view"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="#+id/form_dialog_recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scrollbars="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toTopOf="#id/bottom_sheet_container_buttons"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/header_dialog_container" />
<LinearLayout
android:id="#+id/bottom_sheet_container_buttons"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent">
<Button
android:id="#+id/dialog_sheet_button_reset"
style="#style/ActionSheetTitle"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="0.5"
android:background="#color/bottomBarContainerColor"
android:text="#string/general_reset"
android:textColor="#color/dangerColor" />
<Button
android:id="#+id/dialog_sheet_button_close"
style="#style/ActionSheetTitle"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="0.5"
android:background="#color/bottomBarContainerColor"
android:text="#string/general_close" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
</layout>
Unfortunately my recycler view height is always equal to zero. I tried to change android:layout_height="0dp" to android:layout_height="wrap_content" but as expected it goes below the bottom container. Am I missing something?

Try app:layout_constraintHeight_default="wrap" for RecyclerView if it's inside ConstraintLayout.

I ended up with a different solution. I changed constraint layout with a Linear Layout. And I set up bottom margins.
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/header_dialog_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
<android.support.v7.widget.RecyclerView
android:id="#+id/form_dialog_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"
android:scrollbars="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<LinearLayout
android:id="#+id/bottom_sheet_container_buttons"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="-60dp"
android:orientation="horizontal">
<Button
android:id="#+id/dialog_sheet_button_reset"
style="#style/ActionSheetTitle"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="0.5"
android:background="#color/bottomBarContainerColor"
android:text="#string/general_reset"
android:textColor="#color/dangerColor" />
<Button
android:id="#+id/dialog_sheet_button_close"
style="#style/ActionSheetTitle"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="0.5"
android:background="#color/bottomBarContainerColor"
android:text="#string/general_close" />
</LinearLayout>
</LinearLayout>
</layout>

Try this code:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- Layout injected inside this linear layout container programmatically -->
<LinearLayout
android:id="#+id/header_dialog_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="#id/form_dialog_recycler_view"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="#+id/form_dialog_recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:scrollbars="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toTopOf="#id/bottom_sheet_container_buttons"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/header_dialog_container" />
<LinearLayout
android:id="#+id/bottom_sheet_container_buttons"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent">
<Button
android:id="#+id/dialog_sheet_button_reset"
style="#style/ActionSheetTitle"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="0.5"
android:background="#color/bottomBarContainerColor"
android:text="#string/general_reset"
android:textColor="#color/dangerColor" />
<Button
android:id="#+id/dialog_sheet_button_close"
style="#style/ActionSheetTitle"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="0.5"
android:background="#color/bottomBarContainerColor"
android:text="#string/general_close" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
</layout>

Use app:layout_constrainedHeight="true" in your RecyclerView. It will adjust inside constraints and won't go below the bottom container.

Related

Sticky view with vertical scrolling using constraint layout or any other way in android

The red portion is wrap content and can be expanded, depends on how much data it contains. The green part/view always sticks between red and white. The screen is vertically scrollable.
THE attached XML code having scrollview and layout weight but it divides the screen into two parts from the middle, which is not as per the requirement.
Please suggest how to achieve this.
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:id="#+id/card_img_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:elevation="10dp"
app:cardBackgroundColor="#color/back_screen_color_list"
app:cardElevation="10dp"
app:cardUseCompatPadding="true"
app:contentPadding="20dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/frm_club_img_wrapper"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#drawable/circle_border_orange_bg">
<ImageView
android:id="#+id/img_club"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:contentDescription="#string/img_desc"
app:srcCompat="#drawable/ic_logo_img_white" />
</FrameLayout>
<de.meinverein.app.view.RegularFontTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#+id/frm_club_img_wrapper"
android:text="Jetzt Profil\nverfolständigen"
android:textColor="#color/black"
android:textSize="#dimen/h1_lbl_size" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#0C0F12">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="40dp"
android:layout_marginTop="50dp"
android:orientation="vertical">
<ImageView
android:id="#+id/img_club1"
android:layout_width="#dimen/dashboard_logo_width"
android:layout_height="#dimen/dashboard_logo_height"
android:layout_marginBottom="20dp"
android:contentDescription="#string/img_desc"
app:srcCompat="#drawable/ic_logo_img_white" />
<de.meinverein.app.view.BoldFontTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="MEINVEREIN"
android:textColor="#color/refs_btn"
android:textSize="#dimen/dashboard_app_txt_size" />
<de.meinverein.app.view.BoldFontTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="30dp"
android:text="Hallo Daniel,\ngerade nichts Neues!"
android:textColor="#color/white"
android:textSize="#dimen/list_view_item_txt_size_h1" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#F6F6F6">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvAnimals"
android:layout_width="match_parent"
android:layout_marginTop="20dp"
android:layout_height="match_parent"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvAdnimals"
android:layout_below="#+id/rvAnimals"
android:layout_width="match_parent"
android:layout_marginTop="20dp"
android:layout_height="match_parent"/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>
You can use constraint layout inside a nestedScrollView like this :
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/redPortion"
android:layout_width="match_parent"
android:layout_height="wrap_content" //Red Portion
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.card.MaterialCardView
android:id="#+id/green_sticky_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#+id/redPortion" //Green Sticky View
app:layout_constraintBottom_toBottomOf="#+id/redPortion"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
<LinearLayout
android:id="#+id/whitePortion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" //White Portion
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/redPortion"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
Use any layout you want for your red, white and green views.
I hope This helps.

ScrollView stacked with another page

The issue is the background of my scrollview is somewhat have pretty same content as my scroll view. i have try looking but can't found the problem, because every place tell that i just have to place the content into the scrollview. Please Help!
The design of the layout
This is my layout code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".ui.home.HomeFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/category_rv"
android:layout_width="match_parent"
android:background="#color/colorAccent"
android:elevation="3dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/LinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/category_rv">
<include layout="#layout/horizontal_scroll_layout" />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/LinearLayout">
<include layout="#layout/sliding_ad_layout" />
</LinearLayout>
<LinearLayout
android:id="#+id/LinearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/LinearLayout1">
<include layout="#layout/grid_product_layout" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Put recyclerview and scrollview into nestedscrollview
This will solve your problem.
Add constraints to your scroll view, not to LinearLayout
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/category_rv">

Android Studio layer below each other while orientation is horizontal

doing this layer work and can't figure out how to put layers below ones I made when the android orientation is horizontal, it keeps pushing my layer to the outsidie I have a pics what I have done and how it has to look, if you have any ideas would be nice to hear, thank you in advance.How it has to look
and What I have done
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity2">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<TextView
android:layout_width="140dp"
android:layout_height="300dp"
android:background="#FA0000" />
<TextView
android:layout_width="140dp"
android:layout_height="300dp"
android:background="#03ED0F" />
<TextView
android:id="#+id/blue"
android:layout_width="140dp"
android:layout_height="300dp"
android:background="#0027C3"
/>
<TextView
android:layout_below="#id/blue"
android:layout_width="100dp"
android:layout_height="10dp"
android:background="#000000" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
If you are using
you should use these attribute to control the position
app:layout_constraintTop_toBottomOf
app:layout_constraintTop_toTopOf
app:layout_constraintTop_toStartOf
app:layout_constraintTop_toRightOf
based on what it should look like, you can try
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<LinearLayout
android:id="#+id/ll_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="140dp"
android:layout_height="300dp"
android:background="#FA0000" />
<TextView
android:layout_width="140dp"
android:layout_height="300dp"
android:background="#03ED0F" />
<TextView
android:layout_width="140dp"
android:layout_height="300dp"
android:background="#0027C3"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/ll_horizontal"
app:layout_constraintBottom_toBottomOf="parent">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#FA0000" />
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#03ED0F" />
<TextView
android:id="#+id/blue"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#0027C3"
/>
</LinearLayout>
it should look like this
Use 2 seperate LinearLayout one below other in root.
One for horigontal views. Second for vertical views.
And use weight_sum property in LinearLayout, along with weight property in child views to distribute child views eqally.

How to center a progressbar in screen

The progress bar is appearing on the left of the screen. I would want it centered, like in the example.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:gravity="center"
android:background="#drawable/semi_circle"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="50dp"
android:layout_gravity="center_horizontal"
android:id="#+id/relative">
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="visible" />
<LinearLayout android:layout_width="match_parent"
android:id="#+id/lilPolicy"
android:layout_height="match_parent"
android:layout_marginBottom="20dp">
</LinearLayout>
You should get the ProgressBar outside the ScrollView, because logically you can know the height of the ScrollView at Runtime, so you can't figure out where the ProgressBar will place until running your app.
So, get out the ProgressBar from the ScrollView, create another root layout, wrap both ProgressBar & ScrollView in it; I picked RelativeLayout as the root, you can decide another layout manager if you wish.
layout_centerInParent attribute is used to center a view within a RelativeLayout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/semi_circle"
android:gravity="center">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp">
<LinearLayout
android:id="#+id/lilPolicy"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="20dp">
</LinearLayout>
</RelativeLayout>
</ScrollView>
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="visible" />
</RelativeLayout>
Note: I don't know what you're going to accomplish, so I left android:background="#drawable/semi_circle in the root layout
Inside your scrollView
Using RelativeLayout:
<?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">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
Using ConstraintLayout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Add ConstraintLayout to your project, Build a Responsive UI with ConstraintLayout
You can easily implement it.
The way I would have done it is:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ScrollView
android:layout_width="0dp"
android:layout_height="0dp"
android:fillViewport="true"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/item_1"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_above="#id/item_2"
android:background="#0000FF"
android:layout_marginTop="10dp"
android:orientation="horizontal"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="#+id/item_2"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_below="#id/item_1"
android:background="#FF0000"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="#id/item_1" />
<LinearLayout
android:id="#+id/item_3"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_below="#id/item_2"
android:background="#FFFF00"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="#id/item_2" />
</android.support.constraint.ConstraintLayout>
>
</ScrollView>
</android.support.constraint.ConstraintLayout>
If you have used relative layout as a parent layout . so we need to add this attribute android:layout_centerInParent="true" to align view in parent center. and scroll view by default remove extra spacing in view . add android:fillViewport="true" to make scroll view match parent
Try this 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"
android:gravity="center"
android:background="#000"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="50dp"
android:layout_gravity="center_horizontal"
android:id="#+id/relative">
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="visible" />
<LinearLayout android:layout_width="match_parent"
android:id="#+id/lilPolicy"
android:layout_height="match_parent"
android:layout_marginBottom="20dp"
android:orientation="horizontal">
</LinearLayout>
</RelativeLayout>
</ScrollView>
use android:layout_centerInParent="true" as you are using RelativeLayout.

align view on top and buttons in bottom

I need to achieve the following view
I tried using constraint layout:
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="#id/segmentedButton">
</FrameLayout>
<Buttons
android:id="#+id/segmentedButton"
android:layout_width="match_parent"
android:layout_height="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
</Buttons>
</android.support.constraint.ConstraintLayout>
It works well when there are many items, but when there are just 2-3, it shows them in the middle.
Also, I tried using LinearLayout but no luck.
you can use RelativeLayouttoo to get your desired results. By using the below code you can show the items on top if they are 2 or 3 left.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/segmentedButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<Button
android:id="#+id/segmentedButton"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
try with linear layout, weight property should push the button to align at the end of parent
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<FrameLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_weight="1"
>
</FrameLayout>
<Buttons
android:id="#+id/segmentedButton"
android:layout_width="wrap_content"
android:layout_height="50dp"
>
</Buttons>
</LinearLayout>
I supposed here that Buttons stand for a custom Button you made
Just add Top Constraint to your FrameLayout, should fix your issue.
Your final layout will be like this:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/segmentedButton"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"></FrameLayout>
<Buttons
android:id="#+id/segmentedButton"
android:layout_width="0dp"
android:layout_height="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"></Buttons>
</android.support.constraint.ConstraintLayout>
<?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">
<FrameLayout
android:id="#+id/segmentedFrame"
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#+id/segmentedButton"
android:background="#drawable/bg_scan_edittext">
</FrameLayout>
<Button
android:id="#+id/segmentedButton"
android:layout_width="wrap_content"
android:layout_height="50dp"
app:layout_constraintTop_toBottomOf="#+id/segmentedFrame"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
</Button>
</android.support.constraint.ConstraintLayout>

Categories

Resources