1

I would like to add the new feature of textview which is autosizing the text. Which means that if you have a longer text in the textview, it will automatically resize or adjust the font to fit the textview.

I have included the appcompat-v7:26.0.0 in my build.gradle to have the feature available to low-end devices. But my problem is that I want to implement it in my custom Button. I know that the view button includes a textview in it.

How do I enable that feature in my custom button.

Here's my code for my custom button:

public class GotoButton extends Button {
    public static final String TAG = GotoButton.class.getSimpleName();

    public interface FloaterDragListener {
        void onFloaterDragStart(float screenX, float screenY);
        void onFloaterDragMove(float screenX, float screenY);
        void onFloaterDragComplete(float screenX, float screenY);
    }

    int[] screenLocation = {0, 0};
    boolean inFloaterDrag;
    boolean inLongClicked;
    int untouchableSideWidth = Integer.MIN_VALUE;
    FloaterDragListener floaterDragListener;

    public GotoButton(final Context context) {
        super(context);
    }

    public GotoButton(final Context context, final AttributeSet attrs) {
        super(context, attrs);
    }

    public GotoButton(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onConfigurationChanged(final Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        untouchableSideWidth = Integer.MIN_VALUE;
    }

    @Override
    public boolean onTouchEvent(final MotionEvent event) {
        try {
            final int action = MotionEventCompat.getActionMasked(event);

            float x = event.getX();
            float y = event.getY();

            if (action == MotionEvent.ACTION_DOWN) {
                if (untouchableSideWidth == Integer.MIN_VALUE) {
                    untouchableSideWidth = getResources().getDimensionPixelSize(R.dimen.nav_prevnext_width) - getResources().getDimensionPixelSize(R.dimen.nav_goto_side_margin);
                }

                if (x >= 0 && x < untouchableSideWidth || x < getWidth() && x >= getWidth() - untouchableSideWidth) {
                    return false;
                }
            }

            getLocationOnScreen(screenLocation);
            float screenX = x + screenLocation[0];
            float screenY = y + screenLocation[1];

            if (action == MotionEvent.ACTION_DOWN) { // reset long-clicked status
                inLongClicked = false;
            }

            if (!inLongClicked) { // do not continue if finger is still down but it's because long click is in progress
                if (!inFloaterDrag) {
                    if (action == MotionEvent.ACTION_MOVE) {
                        if (x < 0 || y < 0 || x > getWidth() || y > getHeight()) {
                            cancelLongPress();
                            inFloaterDrag = true;
                            floaterDragListener.onFloaterDragStart(screenX, screenY);
                        }
                    }
                }

                // do not use "else"!
                if (inFloaterDrag) {
                    if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
                        inFloaterDrag = false;
                        floaterDragListener.onFloaterDragComplete(screenX, screenY);
                    } else {
                        floaterDragListener.onFloaterDragMove(screenX, screenY);
                    }
                }
            }
        }catch (Exception e){}
        return super.onTouchEvent(event);
    }

    public void setFloaterDragListener(final FloaterDragListener floaterDragListener) {
        this.floaterDragListener = floaterDragListener;
    }

    @Override
    public boolean performLongClick() {
        inLongClicked = true;
        return super.performLongClick();
    }
}

Thank you!

Jayson Tamayo
  • 2,311
  • 2
  • 37
  • 75
  • If I'm reading the source correctly, you should just have to extend `AppCompatButton` instead of `Button`, then setup the autosizing per usual for the support library, as described in the "Using support library" sections on the [Autosizing TextViews](https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview.html) developer page. – Mike M. Nov 14 '17 at 03:08

1 Answers1

0

As said by Mike M. above, extend the AppCompatButton Class instead, like this

public class GotoButton extends AppCompatButton { ... }

Then use an XML like:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

   <GotoButton
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:autoSizeTextType="uniform" />

</LinearLayout>

In the definition of the GotoButton, it is important to use the app: prefix for the auto... attributes to refer to the Support Library.

Also specify a app:autoSizeMinTextSize attribute if you want your text to get smaller than the default 12sp.

ema3272
  • 877
  • 2
  • 10
  • 24
  • It didn't work. Even with custom button class nor directly using `AppCompatButton` in XML. Even with setting `maxLines=1`. – Dr.jacky Dec 12 '19 at 10:20
  • @Dr.jacky It did not work for small fontsizes: have a look at https://issuetracker.google.com/issues/69606337 I had to fix the issue described there by setting the fontsize myself, which does definitely not go into the direction of autosizing. – ema3272 Dec 23 '19 at 14:45
  • Don't worry. I could manage to solve it by myself: https://stackoverflow.com/a/59302886/421467 – Dr.jacky Dec 23 '19 at 14:47