Sebastien Desemberg
Sebastien Desemberg

Reputation: 321

Spinner selected text no resizing when selected

I have a spinner with the selections at max character length of 100. The text fits within the spinner but when I select the entry it doesn't autosize in the result. If I remove the android:layout_height then the app crashes.

Spinner xml with autoSizeTextType

    <Spinner
        android:id="@+id/spinnerCompany"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="43dp"
        android:paddingLeft="16dp"
        android:spinnerMode="dropdown"
        app:autoSizeTextType="uniform"
        app:layout_constraintTop_toBottomOf="@+id/otpLabel"
        tools:layout_editor_absoluteX="27dp" />

Spinner open, text fits enter image description here

Spinner item selected, text is cut-off enter image description here

Upvotes: 0

Views: 539

Answers (3)

user12556987
user12556987

Reputation:

You can simply do this

 <Spinner
      android:id="@+id/bloodGrpSpinner"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_marginTop="10dp"
      android:layout_marginBottom="10dp"
      android:entries="@array/bloodGrps" />

You are giving wrap content to width in your layout simply make it match_parent and it will automatically pick the max length it can

Upvotes: 0

Denis95
Denis95

Reputation: 136

It's not a good practice to specify the layout height for a Text, instead, use textSize and let the height and width wrap_content.

<Spinner
    android:id="@+id/spinnerCompany"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="13sp" (this is just random, find the ones fits your usage)
    android:layout_marginTop="43dp"
    android:paddingLeft="16dp"
    android:spinnerMode="dropdown"
    app:autoSizeTextType="uniform"
    app:layout_constraintTop_toBottomOf="@+id/otpLabel"
    tools:layout_editor_absoluteX="27dp" />

Upvotes: 1

Viral Patel
Viral Patel

Reputation: 1316

activity_main.xml

<Spinner
        android:id="@+id/spinner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

spinner_item.xml create custom spinner:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20dp"
    android:padding="5dp"
    />

In your java class onCreate method write:

// Get reference of widgets from XML layout
        Spinner spinner = (Spinner) findViewById(R.id.spinner);

        // Initializing a String Array
        String[] plants = new String[]{
                "Black birch",
                "Bolean birch",
                "Canoe birch",
                "Cherry birch",
                "European weeping birch"
        };

        // Initializing an ArrayAdapter
        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
                this,R.layout.spinner_item,plants
        );
        spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item);
        spinner.setAdapter(spinnerArrayAdapter);

Upvotes: 0

Related Questions