auto size text in linear layout with weight android java

510 views Asked by At

I would like to auto-size my text. But it is in linearLayout and I m using weight. I tried things like that :

                            app:autoSizeTextType="uniform"
                            android:autoSizeMinTextSize="2sp"
                            android:autoSizeMaxTextSize="100sp"
                            android:autoSizeStepGranularity="2sp"

but nothing worked ! Can someone help me ??? (I searched a lot but nothing worked) Thank you !

2

There are 2 answers

0
Gautier On BEST ANSWER

Well, I resolved my problem :

What I'm doing is taking the size of a RelativeLayout, and my TextView is inside. Then I dimensioned the TextView programmatically and use setAutoTypeUniform function to fit the text in my layout.

rl_get_coins.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            rl_get_coins.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            height = rl_get_coins.getHeight(); //height is ready
            width = rl_get_coins.getWidth(); //height is ready

            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) text_get_coins.getLayoutParams();
            params.height = height;
            params.width = width;
            text_get_coins.setLayoutParams(params);
            text_get_coins.setText("Obtenir");
            TextViewCompat.setAutoSizeTextTypeUniformWithConfiguration(text_get_coins, 1, 17, 1,
                    TypedValue.COMPLEX_UNIT_DIP);
        }
    });

well guys, if you have any problem, tell it to me. I so happy that it works for me !

Made by a made by stackoverflow, so happy, I did it lol

0
Saksham Khurana On

autosize works on the principal that you give the view constant width & height ant then it will autosize the text appropriately. Refrain from using wrap_content on width... if possible try to give exact width/height or you have have variable height with wrap_content but fixed width with number or match_parent