summaryrefslogtreecommitdiff
path: root/app/src/main/java/de/danoeh/antennapod/view/NoRelayoutTextView.java
blob: cbb2ef0af51f3e53e9b4041727392d3ff0f77c73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package de.danoeh.antennapod.view;

import android.content.Context;
import android.util.AttributeSet;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

public class NoRelayoutTextView extends AppCompatTextView {
    private boolean requestLayoutEnabled = false;
    private float maxTextLength = 0;

    public NoRelayoutTextView(@NonNull Context context) {
        super(context);
    }

    public NoRelayoutTextView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public NoRelayoutTextView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void requestLayout() {
        if (requestLayoutEnabled) {
            super.requestLayout();
        }
        requestLayoutEnabled = false;
    }

    @Override
    public void setText(CharSequence text, BufferType type) {
        float textLength = getPaint().measureText(text.toString());
        if (textLength > maxTextLength) {
            maxTextLength = textLength;
            requestLayoutEnabled = true;
        }
        super.setText(text, type);
    }
}