Wrap_content view inside a ConstraintLayout stretches outside the screen
Outdated: See better answer
No, you cannot do what you want with ConstraintLayout as it is today (1.0 beta 4):
wrap_content
only asks the widget to measure itself, but won't limit its expansion against eventual constraintsmatch_constraints
(0dp) will limit the size of the widget against the constraints... but will match them even ifwrap_content
would have been smaller (your first example), which isn't what you want either.
So right now, you are out of luck for that particular case :-/
Now... we are thinking about adding extra capabilities to match_constraints
to deal with this exact scenario (behaving as wrap_content
unless the size ends being more than the constraints).
I cannot promise that this new feature will make it before the 1.0 release though.
Edit: we did add this capability in 1.0 with the attribute app:layout_constraintWidth_default="wrap"
(with width set to 0dp). If set, the widget will have the same size as if using wrap_content, but will be limited by constraints (i.e. it won't expand beyond them)
Update
Now those tags are deprecated, instead use layout_width="WRAP_CONTENT" and layout_constrainedWidth="true".
How to make view wrap_content but not larger than with ConstraintLayout?
Title and version should be in the chain and app:layout_constraintWidth_default="wrap"
used:
<?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:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:background="#b3b2b2">
<!-- information button -->
<ImageView
android:id="@+id/LibraryWithVersionItem.info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="@dimen/margin8"
android:paddingLeft="@dimen/margin8"
android:paddingRight="@dimen/margin8"
android:paddingBottom="@dimen/margin8"
android:scaleType="center"
android:src="@drawable/ic_info_outline_white_24dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<!-- -->
<TextView
android:id="@+id/LibraryWithVersionItem.title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ellipsize="middle"
android:textColor="@color/mySecondaryDarkColor"
android:textSize="@dimen/fontSize18"
android:textStyle="bold"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="wrap"
tools:text="ExampleLibrary 01234567890123456789012345"
app:layout_constraintRight_toLeftOf="@+id/LibraryWithVersionItem.versions"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp"
android:paddingBottom="1dp"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintHorizontal_bias="0.0"/>
<Spinner
android:id="@+id/LibraryWithVersionItem.versions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/fontSize16"
android:textColor="@color/mySecondaryDarkColor"
tools:listitem="@layout/library_version"
app:layout_constraintRight_toLeftOf="@id/LibraryWithVersionItem.info"
app:layout_constraintLeft_toRightOf="@+id/LibraryWithVersionItem.title"
android:layout_marginRight="0dp"
app:layout_constraintBottom_toBottomOf="@+id/LibraryWithVersionItem.title"
android:layout_marginBottom="0dp"/>
<TextView
android:id="@+id/LibraryWithVersionItem.sentence"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/LibraryWithVersionItem.title"
tools:text="Some library description in one sentence\nbut two lines"
android:layout_marginTop="5dp"
android:layout_marginLeft="@dimen/margin8"
app:layout_constraintRight_toLeftOf="@+id/LibraryWithVersionItem.install"
android:layout_marginRight="8dp"
app:layout_constraintHorizontal_bias="0.0"
android:layout_marginStart="@dimen/margin8"
android:layout_marginEnd="8dp"/>
<TextView
android:id="@+id/LibraryWithVersionItem.isInstalled"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/LibraryManager.installed"
android:textColor="#1a7c1a"
android:layout_marginTop="@dimen/margin8"
android:layout_marginLeft="@dimen/margin8"
android:layout_marginRight="@dimen/margin8"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/LibraryWithVersionItem.sentence"
app:layout_constraintRight_toLeftOf="@+id/LibraryWithVersionItem.install"
app:layout_constraintHorizontal_bias="0.0"
android:layout_marginStart="@dimen/margin8"
android:layout_marginEnd="@dimen/margin8"/>
<!-- install button -->
<ImageView
android:id="@+id/LibraryWithVersionItem.install"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="@dimen/margin8"
android:paddingRight="@dimen/margin8"
android:paddingTop="@dimen/margin8"
android:paddingBottom="@dimen/margin8"
android:scaleType="center"
android:src="@drawable/ic_get_app_white_24dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/LibraryWithVersionItem.info"/>
</android.support.constraint.ConstraintLayout>
I've tried to align version to title baseline but if the title is 2 or more lines it's aligned to the first line and it's not desired. So i had to align version to title bottom and hardcode title -3
bottom padding.
However, it looks as desired in Android Studio:
but on hardware device it looks different:
When analyzing in Layout Inspector
i can see title width is calculated wrong:
Probably it's side effect of using it in RecyclerView
but anyway...
ConstraintLayout inside of ViewPager wrap_content not working
Set all constraints to parent
then use 0dp
in height or width instead of `wrap_content'
ConstraintLayout height with `wrap_content`
Oké, first of all you probably should not put an ConstraintLayout inside of an RelativeLayout.
Second, you can not "make wrap_content work" if your Constraintlayout has no defined size and its childeren also have no defined size
to make this work you can A: set Constraintlayout height and width to 100dp or match_parent, or B: set the height and width of the childeren of Constraintlayout to 100dp
100dp is an example
ConstraintLayout stick view to wrap_content view
Try placing the three views into a horizontal chain while shifting the horizontal bias of nameText
to zero.
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:background="#99776633"
tools:ignore="MissingDefaultResource">
<TextView
android:id="@+id/nameText"
android:layout_width="0dp"
android:layout_height="0dp"
android:ellipsize="end"
android:foreground="#66ff0000"
android:lines="1"
android:textSize="17dp"
app:layout_constraintEnd_toStartOf="@+id/counter"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="wrap"
tools:text="Short text" />
<View
android:id="@+id/seperator"
android:layout_width="1dp"
android:layout_height="1dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/counter"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/counter"
android:layout_width="0dp"
android:layout_height="0dp"
android:foreground="#9900fff0"
android:lines="1"
android:text="(47)"
android:textSize="17dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintEnd_toStartOf="@+id/seperator"
app:layout_constraintStart_toEndOf="@+id/nameText"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_default="wrap" />
</androidx.constraintlayout.widget.ConstraintLayout>
See the documentation on chains.
Android ConstraintLayout TextView go off the screen
Set android:layout_width="0dp"
for street_name
. That will cause it to match constraints and should keep it in bounds.
Not working? street_name
is constrained on the right by guideline30
at 395dp
. Is 395dp
off the right of your screen?
Related Topics
Will Google Android Ever Support .Net
How to Center Icon and Text in a Android Button with Width Set to "Fill Parent"
Android: Automatically Choose Debug/Release Maps API Key
Android Speech Recognition Continuous Service
Android: How to Check If the Server Is Available
Android - Acceleration Down (Smash)
How to Securely Store Access Token and Secret in Android
Android.App Fragments VS. Android.Support.V4.App Using Viewpager
Differencebetween Background, Backgroundtint, Backgroundtintmode Attributes in Android Layout Xml
How to Have a Transparent Imagebutton: Android
Can't Include C++ Headers Like Vector in Android Ndk
How Is Staticlayout Used in Android
Why Do I Get Com.Google.Android.Gms.Common.Api.Apiexception: 10:
Run Code When Android App Is Closed/Sent to Background
How to Make Android Apps Which Support Both 32-Bit and 64-Bit Architecture
Centering Recyclerview Items with Flexboxlayoutmanager
How Does the Getview() Method Work When Creating Your Own Custom Adapter