Why doesn't Linear Layout maxHeight work?
As your parent view is ConstraintLayout
.
I have also implemented the max and min and for me
Replace these lines
android:maxHeight="400dp"
android:minHeight="400dp"
with
app:layout_constraintHeight_max="400dp"
app:layout_constraintHeight_min="400dp"
app:layout_constrainedHeight="true"
This code working fine. I hope it also works for you.
How to set max height to scrollView?
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
maxHeight does not work on RecyclerView
You can achieve this using only XML as long as your parent layout is ConstraintLayout
, which yours seems to be. Make these changes to your RecyclerView
tag:
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintHeight_default="wrap"
app:layout_constraintHeight_max="280dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/line/>
The key attributes are these three:
android:layout_height="0dp"
app:layout_constraintHeight_default="wrap"
app:layout_constraintHeight_max="280dp"
Normally you only use a 0dp
dimension when you constrain both sides of the view, but really all you have to do is provide enough constraints to define the view size (and constraining both sides is just the easiest way to do that). Once you have the height set to "match constraints", you can combine a default height constraint with a maximum height constraint to get exactly what you're looking for.
Note you must be using a 1.1 version of the constraint layout library for this. Make sure your app's build.gradle file has something like:
implementation 'com.android.support.constraint:constraint-layout:1.1.0-beta5'
Does LinearLayout have a max height?
The "implicit" max height of the layout is the height of its parent. Since you're using wrap_content
on both layouts, that means the parent is effectively the screen area, minus whatever other views you're using (such as the TextView
). Unless you place it in a scrolling container, such as a ScrollView
, it won't ever exceed the size of the screen.
The reason your ImageView
isn't showing up "full width and huge" when you remove the crop is because the default scaleType
for an ImageView
is fitCenter
. This particular view is bounded by the layout's width, so it shrinks the image while maintaining aspect ratio.
Setting max height for only one ConstraintLayout chain view
That behavior of layout_constraintHeight_max
is counter-intuitive. Try the following layout that should simulate a chain without the annoying layout_constraintHeight_max
behavior. It uses a barrier to mark the top of the bottom view.
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/topView"
android:layout_width="200dp"
android:layout_height="0dp"
android:background="@android:color/holo_blue_light"
app:layout_constraintBottom_toTopOf="@id/barrier"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_max="200dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="This is the top view." />
<TextView
android:id="@+id/bottomView"
android:layout_width="200dp"
android:layout_height="0dp"
android:background="@android:color/holo_red_light"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_min="100dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/topView"
tools:text="This is the bottom view." />
<androidx.constraintlayout.widget.Barrier
android:id="@+id/barrier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:barrierDirection="top"
app:constraint_referenced_ids="bottomView" />
</androidx.constraintlayout.widget.ConstraintLayout>
You say that the top view can shrink down to 0dp
. If you mean zero height, then the foregoing may not go that far by itself.
How to set the max height of a list view
here you go
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.transport.ScreenDelivery">
<LinearLayout
android:id="@+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView_expectedSum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:text="Очаквана сума"
android:textSize="16sp"
tools:text="Очаквана сума" />
<TextView
android:id="@+id/textView_expectedSumNum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:text="-"
android:textSize="16sp"
tools:text="-" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView_signature"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:text="Подпис"
android:textSize="16sp"
tools:text="Подпис" />
<TextView
android:id="@+id/textView_signatureField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView_returnSum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:text="Сума за връщане"
android:textSize="16sp"
tools:text="Сума за връщане" />
<TextView
android:id="@+id/_returnSumField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:textSize="16sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/textView_remarks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:text="Бележка"
android:textSize="16sp"
tools:text="Бележка" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical">
<Spinner
android:id="@+id/spinner_note"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/btn_dropdown"
android:spinnerMode="dropdown" />
<EditText
android:id="@+id/editText_note"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint=""
android:inputType="text"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="@+id/textView_image"
android:layout_width="269dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:text="Снимка"
android:textSize="16sp"
tools:text="Снимка" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="horizontal">
<Button
android:id="@+id/button_takePhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="150dp"
android:onClick="takePhoto"
android:text="Снимай"
tools:text="Снимай" />
<ImageView
android:id="@+id/imageView_deliveryPhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="Снимка на доставката"
android:maxHeight="200dp"
android:maxWidth="200dp"
android:minHeight="50dp"
android:minWidth="50dp"
app:srcCompat="?android:attr/alertDialogIcon" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center_horizontal"
android:orientation="horizontal">
<Button
android:id="@+id/button_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Запази"
tools:text="Запази" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/top_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/textView_customer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Клиент"
android:textSize="20sp"
tools:text="Клиент" />
<TextView
android:id="@+id/textView_palletCount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Брой палета"
android:textSize="20sp"
tools:text="Брой палета" />
<TextView
android:id="@+id/textView_palletCountNum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-"
android:textSize="20sp"
tools:text="-" />
</LinearLayout>
<ListView
android:id="@+id/listView_pallets"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottom_layout"
android:layout_below="@id/top_layout" />
</RelativeLayout>
Related Topics
How to Generate Buildconfigfield with String Type
When to Use a Content Provider
How to Automate the Android Sdk Installation
ASP.NET Web API Authentication
Action_View Intent for a File with Unknown Mimetype
How to Set Cursor Position in Edittext
How to Add Multiple Widgets in the Same App
How to Create an Android Application in Android Studio That Uses the Google Maps API V2
Android: Test Push Notification Online (Google Cloud Messaging)
How to Start Power Manager of All Android Manufactures to Enable Background and Push Notification
Vertical Line Using Xml Drawable
Android SQLite: Update Statement
Android Adb Stop Application Command Like "Force-Stop" for Non Rooted Device
Disable Logcat Output Completely in Release Android App
One Activity and All Other Fragments
How to Get Selected Items from Multi Select List View