constraintlayout baseline to baseline with autosize
Asked Answered
L

3

8

I'm trying to align 2 textviews by baseline and use autosize for textsize at the same time. Here is simplified code example. First textview has big size, so textsize auto set to some big value. Second textview smaller than first one so it has smaller auto determined textsize.

<?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">

    <TextView
        android:id="@+id/text1"
        android:layout_width="200dp"
        android:layout_height="100dp"
        android:text="text1"
        android:maxLines="1"
        android:autoSizeTextType="uniform"
        android:autoSizeMaxTextSize="200sp"
        android:autoSizeMinTextSize="2sp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/text2"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/text2"
        android:layout_width="50dp"
        android:layout_height="20dp"
        android:text="text2"
        android:maxLines="1"
        android:autoSizeTextType="uniform"
        android:autoSizeMaxTextSize="200sp"
        android:autoSizeMinTextSize="2sp"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/text1"
        app:layout_constraintBaseline_toBaselineOf="@id/text1"/>

</android.support.constraint.ConstraintLayout>

result of example code

The problem is that seems layout_constraintBaseline_toBaselineOf stops working as soon as I use autoSizeTextType

It would be nice if someone can point me right direction. Am I understand wrong how to use constraintlayout? Or is it just baseline_tobaseline does not work with autosize?

Lymphadenitis answered 31/1, 2018 at 16:25 Comment(0)
L
5

I fixed it as follows:

Kotlin:

text1.text = "This is text text text text"
text1.post{

text1.requestLayout()

}
Lewanna answered 29/5, 2020 at 8:26 Comment(1)
Thanks! That was really helpful.Caprice
P
2

Try removing the autoSize from textview and set the TextView text size. Things will start to work out by themselves. At least this worked for me.

    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="56dp"
        android:maxLines="1"
        android:text="text1"
        android:textSize="100sp"
        app:layout_constraintEnd_toStartOf="@+id/text2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <TextView
        android:id="@+id/text2"
        android:layout_width="50dp"
        android:layout_height="20dp"
        android:text="text2"
        android:maxLines="1"
        android:autoSizeTextType="uniform"
        android:autoSizeMaxTextSize="200sp"
        android:autoSizeMinTextSize="2sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/text1"
        app:layout_constraintBaseline_toBaselineOf="@id/text1"
    />
Poultice answered 31/1, 2018 at 16:46 Comment(2)
It will align to textviews by bottom bound. But I need to align two textviews by baseline. Texts with different textsize have to be on the same baseline.Lymphadenitis
Yes, you right, Baseline_toBaseline works if we use concrete textSize, but point of the question is how to use Baseline_toBaseline and autosize at the same time.Lymphadenitis
C
2

I'm using hack for this issue.

Luckily in my case I have two words, so finally I've came up with something like:

private fun getCorrectTextValue(value: Number): SpannableString {
    val currency = getString(R.string.common_currency)
    val formatter = NumberFormat.getInstance(Locale.getDefault())
    val ss = SpannableString("${formatter.format(value)} $currency")
    ss.setSpan(AbsoluteSizeSpan(resources.getDimensionPixelSize(R.dimen.max_text_size)), value.toString().length, value.toString().length + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
    return ss
}

It's working for me

Probably you can add an whitespace character using SpannableString and set it's height with:

ss.setSpan(AbsoluteSizeSpan(resources.getDimensionPixelSize(R.dimen.max_text_size)), value.toString().length, value.toString().length + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
Crankpin answered 13/8, 2018 at 8:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.