Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

constraintlayout baseline to baseline with autosize

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?

like image 263
danil.farafontov Avatar asked Jan 31 '18 16:01

danil.farafontov


1 Answers

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)
like image 51
b2mob Avatar answered Sep 19 '22 06:09

b2mob