Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Support libraries to add for Autosizing TextViews

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"/>
like image 869
mtdavem Avatar asked Feb 27 '18 20:02

mtdavem


People also ask

Does the support library support the auto sizing textview feature?

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.

What's new in textview auto-sizing in Android?

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.

How do I change the size of text in a textview?

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.

How to auto-resize textview like a dial pad in Android?

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.


Video Answer


2 Answers

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).

like image 76
Ben P. Avatar answered Oct 21 '22 03:10

Ben P.


Try changing android: to app: namespace :

<android.support.v7.widget.AppCompatTextView
    app:autoSizeTextType="uniform"
    .../>
like image 5
Jéwôm' Avatar answered Oct 21 '22 03:10

Jéwôm'