Search code examples
androidtextviewandroid-linearlayoutandroid-textview-autosizeautosizing

auto size text in linear layout with weight android java


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 !


Solution

  • 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