Skip to content

Instantly share code, notes, and snippets.

@B0yma
Created May 26, 2019 11:29
Show Gist options
  • Save B0yma/4008a2b0db41dfd3be9b73fe3a8108fd to your computer and use it in GitHub Desktop.
Save B0yma/4008a2b0db41dfd3be9b73fe3a8108fd to your computer and use it in GitHub Desktop.
AutoSizeOfGroupTextView
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.util.TypedValue
import android.widget.TextView
import androidx.core.widget.TextViewCompat
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSimilarSize(listOf(
text1,
text2,
text3
))
}
private fun setSimilarSize(list: List<TextView>) {
val maxLengthIndex = getMaxLengthIndex(list)
list[maxLengthIndex].post {
val textSize = list[maxLengthIndex].textSize.toInt()
for (item in list){
TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(
item,
textSize,
textSize+1,
1,
TypedValue.COMPLEX_UNIT_DIP
)
}
}
}
private fun getMaxLengthIndex(list: List<TextView>): Int {
var maxLength = list[0].length()
var maxIndex = 0
for ((i, item) in list.withIndex()){
if (item.length() >= maxLength){
maxLength = item.length()
maxIndex = i
}
}
return maxIndex
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:gravity="center">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="20000000000000000000!"
app:autoSizeMaxTextSize="18dp"
app:autoSizeMinTextSize="6dp"
app:autoSizeStepGranularity="1dp"
app:autoSizeTextType="uniform"
android:gravity="center"
android:lines="1"
android:id="@+id/text1"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="55555555!"
app:autoSizeMaxTextSize="18dp"
app:autoSizeMinTextSize="6dp"
app:autoSizeStepGranularity="1dp"
app:autoSizeTextType="uniform"
android:lines="1"
android:gravity="center"
android:id="@+id/text2"
android:visibility="gone"
android:layout_weight="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="9999999999!!!"
app:autoSizeMaxTextSize="18dp"
app:autoSizeMinTextSize="6dp"
app:autoSizeStepGranularity="1dp"
app:autoSizeTextType="uniform"
android:lines="1"
android:gravity="center"
android:layout_weight="1" android:id="@+id/text3"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment