I am traiyng to write a scrolling dialog layout. As the main element I'm using ScrollView and in it there is a ConstraintLayout that contains the displayed items. The size of the ScrollView has a fixed value but I would like the width of ConstraintLayout to be match_parent and the height to be wrap_content. When I set the width to match_parent and the height is wrap_content the dialog isn't displayed in the fragment. However, if I set the width as a constant value such as the ScrollView size and set height as warp_content the dialog appears. Should I nest the layouts somehow different or what?
Here is my code:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="700dp"
android:layout_height="400dp"
android:background="#color/main_blue"
android:fadeScrollbars="false"
android:fillViewport="true"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarFadeDuration="0"
android:scrollbarThumbVertical="#drawable/thumb_dialog"
android:scrollbarTrackVertical="#drawable/track_dialog">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="700dp"
android:layout_height="wrap_content">
<TextView
android:id="#+id/dialogTitle"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="#dimen/margin_xxxxlarge"
android:layout_marginTop="#dimen/margin_xxxxlarge"
android:layout_marginEnd="#dimen/margin_xxxxlarge"
android:text="#string/dialog_title"
android:textAllCaps="true"
android:textColor="#color/main_red"
android:textSize="#dimen/text_size_xl"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/dialogDescription"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="#dimen/margin_xxxxlarge"
android:layout_marginTop="#dimen/margin_xxxxlarge"
android:layout_marginEnd="#dimen/margin_xxxxlarge"
android:text="#string/dialog_message"
android:textSize="#dimen/text_size_m"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/dialogTitle" />
<Button
android:id="#+id/agreeButton"
style="#style/RedButton"
android:layout_width="188dp"
android:layout_height="49dp"
android:layout_marginTop="#dimen/margin_xxxxlarge"
android:layout_marginBottom="#dimen/margin_xxxxlarge"
android:background="#drawable/red_button"
android:text="#string/dialog_button_text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/dialogTitle"
app:layout_constraintTop_toBottomOf="#+id/dialogDescription" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
Layout blueprint:
Related
I don't known why FrameLayout inside ConstraintLayout, and both use wrap_content, then the last item in the RecycleView just show half content.
If I remove FrameLayout, direct use recycleview, it can work!
<?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="wrap_content"
android:background="#drawable/finance_bg_book_list_dialog">
<TextView
android:id="#+id/tvToolbarTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/finance_bg_book_list_toolbar"
android:drawablePadding="10dp"
android:padding="14dp"
android:text="#string/finance_book"
android:textColor="#android:color/white"
android:textSize="?fontSizeLarge"
app:drawableStartCompat="#drawable/finance_ic_toolbar_shelf"
app:layout_constraintBottom_toTopOf="#+id/vulturLayout"
app:layout_constraintTop_toTopOf="parent"/>
<FrameLayout
android:id="#+id/vulturLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvToolbarTitle">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_contentCode="successful"/>
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
You need some horizontal constraints on your textview.
ex : app:layout_constraintStart_toStartOf=
I recommend you set a dimension for the RecyclerView height. if you use the FrameLayout with a wrap_content height, your RecyclerView will not be scrollable and it will own show half the item at the last based on the screen size
I have an imageview in Linearlayout I want its width to be fill_parent. I want its height to be whatever the width ends up being. For example:
<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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1"
android:scaleType="centerCrop"
android:layout_margin="2dp"
tools:ignore="Suspicious0dp" />
</LinearLayout>
But it doesn't show the images in the android layout..
If I force change height of the imageview to 100dp in the code, it works, but I wonder why the height of image doesn't match to width even I wrote constraintDimensionRatio 1:1..
Thank you
You need to use ConstraintLayout as parent and set the dimension you want to make primary and another one to 0dp. app:layout_constraintDimensionRatio="H,1:1" means change height and make it 1:1 with width.
<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">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="H,1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Try adding weight to your imageview then you will get the height, otherwise you have to user wrap_content as height.
<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:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:layout_margin="2dp"
tools:ignore="Suspicious0dp" />
</LinearLayout>
<!--or if you don't want to use weight then user height = "wrap_content"-->
I want to create a layout as on the picture below:
So each button takes 50% of (parent width - space for margins)
If I set layout_constraintWidth_percent to 0.5 it doesn't work. I can achieve this using LinearLayout and weight, but how to do it with ConstraintLayout?
Here is the layout I use:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<Button
android:id="#+id/other_options"
style="?tertiaryButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/half_pad"
android:text="#string/report_suicide_other_options_action"
app:layout_constraintWidth_percent="0.5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
/>
<Button
android:id="#+id/yes"
style="?primaryButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="#dimen/half_pad"
android:text="#string/action_yes"
app:layout_constraintWidth_percent="0.5"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/other_options"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
Just add:
app:layout_constraintEnd_toStartOf="#+id/yes"
to the Button with id other_options
and you can remove
app:layout_constraintWidth_percent="0.5"
from both buttons.
How can I set maxHeight property for scrollView?
I have a LinearLayout and I add some view (rows) to this layout programmatically. How can I show scroll when rows reach maxHeight of scrollView? ScrollView is located in a CardView
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.cardview.widget.CardView
android:id="#+id/cardView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toTopOf="#+id/cardView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_max="100dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textViewTitle">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
Because you're using ConstraintLayout you need to add 2 constraints to your CardView
app:layout_constraintTop
app:layout_constraintBotttom
And you also need to add the following lines
app:layout_constrainedHeight="true"
app:layout_constraintHeight_max="300dp" // Change this to your maximum height
It should look something like this
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="wrap_content"
app:cardCornerRadius="#dimen/card_radius_10"
app:layout_constrainedHeight="true"
app:layout_constraintHeight_max="300dp" // Change this to your maximum height
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent" //Change based on your layout
app:layout_constraintTop_toTopOf="parent"> //Change based on your layout
use this attribute in your ScrollView and change the LinearLayout height to match_parent
android:fillViewport="true"
like this
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="#+id/layout" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</ScrollView>
Change the height of parent View of your scroleView. In your case, CardView is Parent layout.so set the height of CardView like
android:layout_height="350dp"
or use the weight according to your need by using linear layout in the parent of CardView. if you are using this layout in the Adapter or something like that then you only set the height with numbers.
Guys, I need to make my ConstraintLayout scroll-able, so I placed it inside an ScrollView like 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"
android:overScrollMode="never">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/img"
android:layout_margin="64dp"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="fitXY"
app:srcCompat="#drawable/avd"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#id/btn"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintHeight_min="64dp"
app:layout_constraintHeight_max="256dp"/>
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"
app:layout_constraintTop_toBottomOf="#id/img"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>
</ScrollView>
But the problem is that I like to scroll layout when all constraints has reached their minimum size(means all controls has reached to their specified margins and layout cannot get smaller anymore). But it seems when ScrollView needs to show scrolls, it specifies a very large size for ConstraintLayout's height, so my layout scales up to max possible size. If you try this layout in portrait and then landscape, you notice how it changes.
I wonder if there is any way to start scrolling when all constraint has reached their minimum size. Any idea how I can do this?