Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

layout_constrainedWidth not working properly

I’ve got EditText and TextView in my ConstraintLayout. TextView displays content of EditText.

On input I need EditText to expand untill the middle of the screen and TextView to follow it and fill the second half.

enter image description here

I set guideline at 50%, and use wrap_content и layout_constrainedWidth for both widgets.

Here is my xml:

<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.manualuser.android.polygon.MainActivity">

<EditText
    android:id="@+id/editText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="16dp"
    android:inputType="textNoSuggestions"
    android:minWidth="1dp"
    android:scrollHorizontally="true"
    app:layout_constrainedWidth="true"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/guideline"
    app:layout_constraintTop_toTopOf="parent"/>

<HorizontalScrollView
    android:id="@+id/horizontalScrollView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constrainedWidth="true"
    app:layout_constraintBottom_toBottomOf="@+id/editText"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toRightOf="@+id/editText"
    app:layout_constraintRight_toRightOf="parent">

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:maxLines="1"
        android:scrollHorizontally="true"
        android:singleLine="true"
        app:layout_constrainedWidth="true"
        app:layout_constraintBaseline_toBaselineOf="@+id/editText"
        app:layout_constraintEnd_toStartOf="@+id/textView"
        app:layout_constraintStart_toEndOf="@+id/editText"/>
</HorizontalScrollView>

<android.support.constraint.Guideline
    android:id="@+id/guideline"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.5"/>

</android.support.constraint.ConstraintLayout>

What I get is when TextView reaches end of the screen it supersedes EditText to the left.

enter image description here

It works as needed when I set TextView layout_width to match_constraint. But how do I make it work with wrap_content?

like image 869
Aleksey Bulin Avatar asked Jul 01 '18 00:07

Aleksey Bulin


People also ask

What is layout_constrainedWidth?

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.

What is ConstraintLayout in Android?

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.

What is bias in constraint layout Android?

Bias, in terms of ConstraintLayout , means "if there is extra room, slide the widget in this direction along the axis". The default bias is 0.5, meaning that the widget is centered in the available space.

How to get constraint layout in android studio?

ConstraintSet set = new ConstraintSet(); View layout = findViewById(R. id. crosshair); set. clone((ConstraintLayout) layout); set.


1 Answers

I had a very similar issue. Basically what I observed is that it works weird when used with app:layout_constraintHorizontal_bias set to 0.0.

so if you change

app:layout_constraintHorizontal_bias="0.0"

to

app:layout_constraintHorizontal_bias="0.001"

It should work (at least it worked for me)

Please report it as a bug if you have time. It's an issue even in 2.0.0-beta2

like image 129
svkaka Avatar answered Nov 15 '22 01:11

svkaka