I have a parent ConstraintLayout in my fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="400dp"
android:maxHeight="400dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/colorPopupBackground"
android:fadeScrollbars="false"
android:id="#+id/profile_layout"
android:elevation="1dp">
as you can see i've set a maxHeight and constrained it on every side - so you would think it should centre vertically on the screen. However the ConstraintLayout sits on the top vertically.
Any idea?
Just remove bottom constraint because when you set both side constraint and set custom width then it will be center of both constraint, SO update your code like bellow
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="400dp"
android:maxHeight="400dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/colorPopupBackground"
android:fadeScrollbars="false"
android:id="#+id/profile_layout"
android:elevation="1dp">
Related
Following is my xml layout. Trying to set height of ConstraintLayout based on screen width unable to do that it is not working. How to set height of ConstraintLayout based on screen size.
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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:layout_marginTop="16dp"
android:background="#color/accent_secondary"
app:cardBackgroundColor="#color/accent_secondary"
app:cardCornerRadius="10dp"
app:cardElevation="2dp"
app:cardUseCompatPadding="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:0.35">
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Shadows seem to cut off on some of my CardView usages.
Any idea why? looks like removing the padding on the parent resolves the issue, but I do want the padding. And I don't want to use margin on the inner card, because there are other views aligned and I prefer having the padding set on the parent to apply to all children
Any solutions?
layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
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:layout_marginTop="#dimen/standard_spacing"
android:layout_marginLeft="#dimen/standard_spacing"
android:layout_marginRight="#dimen/standard_spacing"
android:layout_marginBottom="#dimen/smaller_spacing"
app:cardCornerRadius="#dimen/standard_card_corner_radius"
app:cardElevation="10dp">
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="#dimen/standard_spacing">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardBackgroundColor="#color/light_gray_background"
android:minHeight="40dp"
app:cardElevation="5dp"
app:cardCornerRadius="#dimen/standard_card_corner_radius" >
</androidx.cardview.widget.CardView>
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.cardview.widget.CardView>
add
android:clipToPadding="false"
to LinearLayout
I have an issue with the design of a listview.
I would like to reduce the list when there are a few items but if I put wrap content it's on the middle of the screen.
How can I do ?
My list view :
<ListView
android:id="#+id/listView_location"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_below="#id/searchView_location"
android:layout_marginBottom="8dp"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toTopOf="#+id/searchView_location"
app:layout_constraintEnd_toEndOf="#+id/searchView_location"
app:layout_constraintStart_toStartOf="#+id/searchView_location"
app:layout_constraintTop_toTopOf="parent"/>
Constraint layout
<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:id="#+id/root_layout_search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true">
Change your ListView height
android:layout_height="wrap_content"
Remove top constraint
app:layout_constraintTop_toTopOf="parent"
Change its parent height to wrap_content i.e contraintlayout
<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:id="#+id/root_layout_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true">
I am trying to use constraint layout as below. But the aspect ratio is not respected in the actual layout. Instead, the height is actually wrapping the content inside
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/content"
android:layout_height="0dp"
android:layout_width="match_parent">
<!-- some content inside -->
</android.support.constraint.ConstraintLayout>
I then think it might be because I didn't set width="0dp" (let constraint decide the width). So I tried another way like below. But then the width becomes zero.
What is the correct way of doing it?
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/content"
android:layout_height="0dp"
android:layout_width="0dp"
app:layout_constraintStart_toEndOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<!-- some content inside -->
</android.support.constraint.ConstraintLayout>
EDIT: working solution after inspired by Anddenver 's answer (with modification):
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<FrameLayout
android:background="#android:color/holo_green_dark"
android:layout_width="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="2:1">
</FrameLayout>
</android.support.constraint.ConstraintLayout>
Further Edit:
It seems I need to wrap one more layer of constraint layout for my inner contents .... :
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<FrameLayout
android:background="#android:color/holo_green_dark"
android:layout_width="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="2:1">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<actual contents here..../>
</android.support.constraint.ConstraintLayout>
</FrameLayout>
</android.support.constraint.ConstraintLayout>
Or, just remove the FrameLayout, and wrap ConstraintLayout inside a ConstraintLayout:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.constraint.ConstraintLayout
android:background="#android:color/holo_green_dark"
android:layout_width="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="2:1">
contents here...
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
You need inner view for that, for example:
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:background="#color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="1:1">
<YOUR CONTENT>
</FrameLayout>
For a ListView, you'd need to inflate a custom list_item.xml
and this should be H,2:1, to constrain the height to a 2:1 aspect ratio.
The default android.R.layout.simple_list_item_1 is just a TextView:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_height="0dp"
android:layout_width="0dp"
app:layout_constraintDimensionRatio="H,2:1"
...
/>
see DimensionConstraints.
When I remove all views from my Constraint layout, I want it to have 0dp height(like wrap_content as it sounds) but it stretches as much as possible.
Is there documented way to do that? I mean besides setting maxHeight to 0 or hiding it.
I've tried app:layout_constrainedHeight="true" with "wrap_content" but it doesnt work.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constrainedHeight="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</android.support.constraint.ConstraintLayout>
I was unable to find a proper solution.
The simplest workaround I came up with is to add an extra view with height=0 and never remove it:
<?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">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/holo_red_dark"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<FrameLayout
android:id="#+id/somethingWithNoHeight"
android:layout_width="match_parent"
android:layout_height="0dp" />
<!--<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lorem ipsum" />-->
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
I believe you should fill a bug to be sure that developers know about such a problem