I was using this layout_constraintWidth_default="wrap" in textview to keep the textview content wrapped(to right of it there is imageview), as the text in textview increases textview area will grow which moves the imageview to right.
layout_constraintWidth_default="wrap"
As it is deprecated any alternatives?
Logcat
app:layout_constrainedWidth="true" Defining width or height in percentage is much more helpful to make an expressive UI as width or height in dp doesn't work so well when viewed on mobile and tablet until different dimens are specified.
app:layout_constrainedHeight="true" will reduce the size of a view according to it's constraints. Example: stackoverflow.com/a/61826350/7210237.
A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. Note: ConstraintLayout is available as a support library that you can use on Android systems starting with API level 9 (Gingerbread). As such, we are planning on enriching its API and capabilities over time.
In above example, the app:layout_constraintVertical_bias=”0.6" indicates that the button is biased 60% from top rather than 50%(center by default) and app:layout_constraintHorizontal_bias=”0.4" indicates that button is biased 40% from start/left, i.e, left and bottom are shorter.
Following are the changes i have made for solution, imageview will not go outside the visible screen area :
android:layout_width="wrap_content"
app:layout_constrainedWidth="true"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintHorizontal_chainStyle="packed"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text asdasdasdasdasdasdasdasdasdadsasdasdasdasdasdasdasd"
app:layout_constrainedWidth="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/image"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/text"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
You can try this:
android:layout_width="wrap_content"
app:layout_constrainedWidth="true"
width cannot be “0dp”
As the documentation says in this link from Android Developers:
In versions before 1.1 they will be treated as a literal dimension -- meaning, constraints will not limit the resulting dimension. While in general, this is enough (and faster), in some situations, you might want to use WRAP_CONTENT, yet keep enforcing constraints to limit the resulting dimension. In that case, you can add one of the corresponding attributes:
app:layout_constrainedWidth="true|false"
Hence, use layout_constrainedWidth="true"
instead of layout_constraintWidth_default="wrap"
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With