Search code examples
androidandroid-drawableandroid-vectordrawable

How to resize vector drawable to device font size


Is it possible to set a vector drawable to automatically resize itself based on device's set font size? Whenever I change the font size in the Settings menu, the vector drawable never seems to adapt it's size and always stays the same but yet the text does without fail.

Drawable

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="48dp" android:width="48dp" android:viewportHeight="24" android:viewportWidth="24">
    <path android:fillColor="#FFFFFF" android:pathData="M7.41,7.84L12,12.42l4.59,-4.58L18,9.25l-6,6 -6,-6z"/>
</vector>

Small font size

enter image description here

Normal font size

enter image description here

Large font size

enter image description here

Huge font size

enter image description here


Solution

  • You can use sp units in the definition of the vector drawable. The whole point of the sp unit is that 1sp == 1dp when the font size is normal, but 1sp is more than 1dp when the font size is scaled up.

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <vector
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:height="48sp" <!-- use sp here instead of dp -->
        android:width="48sp"  <!-- use sp here instead of dp -->
        android:viewportHeight="24"
        android:viewportWidth="24">
    
        <path
            android:fillColor="#FFFFFF"
            android:pathData="M7.41,7.84L12,12.42l4.59,-4.58L18,9.25l-6,6 -6,-6z"/>
    
    </vector>