so I am trying to build a UI for my Android-App that should look similar to this picture. The problem is that the textSize
of them is obviously different because the length of the strings is different, but I need them to be the same size. I found two solutions:
Leave them like this, get the four sizes and choose the smallest for them (still using the a
autoSizeTextType="uniform"
provided by Android)Loop through the textsizes for all the buttons until the
lineCount
is greater than one and choose the smallest once again
The problem is that both need the Layout fully loaded. If it isn’t fully loaded all three autosized Buttons will return the same size or lineCount
will return 0 (I am aware that there is a solution here on StackOverflow but it doesn't work for me).
I am aware that the lifecycle (https://developer.android.com/guide/components/activities/activity-lifecycle) specifies that the Activity should be fully loaded onReasume()
but that isn’t true as well.
Do you guys know a better solution than the two of mine or how to get them to work?
Here is my XML-File:
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.5" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<Button
android:id="@+id/btn_1"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#009688"
android:autoSizeTextType="uniform"
android:maxLines="1"
android:text="12"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_2"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#E91E63"
android:autoSizeTextType="uniform"
android:maxLines="1"
android:text="Test12"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline2"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/btn_3"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#FF5722"
android:autoSizeTextType="uniform"
android:maxLines="1"
android:text="Hi"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline" />
<Button
android:id="@+id/btn_4"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#673AB7"
android:autoSizeTextType="uniform"
android:maxLines="1"
android:text="Hello World"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline2"
app:layout_constraintTop_toTopOf="@+id/guideline" />
MainActivity:
package com.example.testsizes
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
Thanks in advance!