I am confused about adding the correct support libraries in Android Studio to be able to use AutoSizing TextViews.
https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview.html
The description of what support libraries to use is kind of vague on this page. I have tried to import the suggested libraries, but either I am requesting the wrong libraries (they are not found, such as the android.support.v4.widget package) or I am doing something else incorrectly.
My min SDK is 21 and max SDK is 27, so the AutoSizing feature should be backwards compatible for my app if I have the correct libraries imported. However, in the screen design view, I get the warning "Attribute autoSizeTextType is only used in API level 26 and higher (current min is 21)"
As far as I can tell, I have the correct support libraries included. Project Structure Dependencies are as follows:
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'android.arch.lifecycle:compiler:1.0.0'
implementation 'android.arch.persistence.room:runtime:1.0.0'
implementation 'android.arch.persistence.room:compiler:1.0.0'
implementation 'android.arch.paging:runtime:1.0.0-alpha4-1'
implementation 'android.arch.core:core-testing:1.0.0'
implementation 'android.arch.persistence.room:testing:1.0.0'
implementation 'android.arch.lifecycle:common-java8:1.0.0'
implementation 'com.android.support:support-v13:27.0.2'
implementation 'com.android.support:appcompat-v7:27.0.2'
androidTestImplementation 'com.android.support.test:runner:1.0.2-alpha1'
implementation 'com.android.support:support-annotations:27.0.2'
implementation 'com.android.support:support-v4:27.0.2'
implementation 'com.android.support:preference-v14:27.0.2'
implementation 'com.android.support:recyclerview-v7:27.0.2'
implementation 'android.arch.lifecycle:extensions:1.1.0'
implementation 'com.google.android.gms:play-services-location:11.8.0'
implementation 'com.android.support:preference-v7:27.0.0'
implementation 'com.android.support:support-compat:27.0.0'
Any suggestions? I also could use some links on how to convert the suggested libraries to the actual libraries to be imported. Thanks
Edit: partial list of layout with android:autoSizeTextType problem. I get the warning message for both of the textviews listed.
<?xml version="1.0" encoding="utf-8"?>
<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">
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="134dp"/>
<android.support.v7.widget.AppCompatTextView
android:id="@+id/temperature_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="@string/temperature"
android:autoSizeTextType="uniform"
app:layout_constraintEnd_toStartOf="@+id/guideline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:id="@+id/temperature"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:hint="@string/temperature"
android:autoSizeTextType="uniform"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toRightOf="@+id/guideline"
app:layout_constraintTop_toTopOf="parent"/>
Edit: My Solution for now.
This layout is what I ended up with that seems to work for now. Not sure if the autoSizing is resizing or not, but this looks good on the devices I am testing for at this time. Tried the plain TextView using the app: prefix for autoSizeTextType as was suggested, but received the error "Unexpected namespace prefix "app" found for tag TextView". When I changed it to android.support.v7.widget.AppCompatTextView, the error went away. Thanks for the help.
<?xml version="1.0" encoding="utf-8"?>
<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">
<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.38"/>
<android.support.v7.widget.AppCompatTextView
android:id="@+id/temperature_label"
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="@string/temperature"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
app:autoSizeTextType="uniform"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<android.support.v7.widget.AppCompatTextView
android:id="@+id/temperature"
android:layout_width="match_parent"
android:layout_height="0dp"
android:hint="@string/temperature"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
app:autoSizeTextType="uniform"
app:layout_constraintLeft_toRightOf="@+id/guideline"
app:layout_constraintTop_toTopOf="parent"/>
The Support Library provides full support to the auto sizing TextView feature on devices running Android versions prior to Android 8.0 (API level 26). The android.support.v4.widget package contains the TextViewCompat class to access features in a backward-compatible fashion.
Android making this much easier to implement with the introduction of TextView auto-sizing. With Android O and Support Library 26.0+, TextView gains a new property auto-size text type which allows you to optimize the text size when working with dynamic content.
Set the autoSizeText attribute to either none or uniform . none is a default value and uniform lets TextView scale uniformly on horizontal and vertical axes. Set autoSizeMinTextSize , autoSizeMaxTextSize, and autoSizeStepGranularity attributes to define the dimensions for the autosizing of TextView.
As we have taken the dial pad as an example to demonstrate the auto-resizing of the TextView, so invoke the following code in the activity_main.xml file to make the UI like a dial pad. One can see attributes of TextView in above code app:autoSizeMaxTextSize=”80sp”, this is the initial sizes of TextView.
Change this:
<android.support.v7.widget.AppCompatTextView
android:autoSizeTextType="uniform"
.../>
to this:
<TextView
app:autoSizeTextType="uniform"
.../>
There's no need to reference AppCompatTextView
directly. The LayoutInflater
used by the support library will automatically inject AppCompat widgets in place of standard widgets.
Additionally, to get auto-sizing working pre-API-26, you need to be using the support library implementation, which means you need to be using the AppCompat attributes (with the app:
namespace) instead of the platform attributes (with the android:
namespace).
Try changing android:
to app:
namespace :
<android.support.v7.widget.AppCompatTextView
app:autoSizeTextType="uniform"
.../>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With