Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] Button inside Android ConstraintLayout refuses to show multi-character text centered in horizontal orientation

Posted on November 14, 2022

Solution 1 :

I was able to find the solution by guiding myself with an old project I had:

Inside the Fragment onViewCreated method which loads the ConstraintLayout, I create a ViewGroup object which sets up a ViewTreeObserver.OnGlobalLayoutListener object.

As the buttons are laid out, I change their singleLine attribute to true and even change the TypeFace of the text.

Here’s the code: It works perfectly:

         ViewGroup group = (ViewGroup)view;
        final ViewGroup myGroup = group.getRootView().findViewById(R.id.graphlay);

        if(myGroup!=null){

            ViewTreeObserver.OnGlobalLayoutListener listener = new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {

                    for(int u = 0; u < myGroup.getChildCount(); u++){
                        View view01 = myGroup.getChildAt(u);
                        try{
                            Button button = (Button)view01;
                            Log.d("inspectme","button: " + button.getText());
                            button.setTypeface(MainActivity.typeface0024);
                            button.setSingleLine(true);

                        }catch (Exception exx){

                        }
                    }

                    myGroup.getViewTreeObserver().removeOnGlobalLayoutListener(this);




                }


            };



            myGroup.getViewTreeObserver().addOnGlobalLayoutListener(listener);
        }

I still would like to know the way of doing this in the XML instead of creating a ViewTreeObserver.OnGlobalLayoutListener object!!

Problem :

I’m using ConstraintLayout with Flow Helper object in order to organize some buttons on the screen.
The buttons are not centering the text as it should.

I have the following button:

<Button
    android_id="@+id/x"
    android_layout_width="0dp"
    android:layout_height = "0dp"
    app_layout_constraintHeight_max="300dp"
    app_layout_constraintHorizontal_weight="1"
    android_layout_margin="1dp"
    android_text="x909"
    android_textSize="18sp"
    android_gravity="center"
    android_maxLines="1"

As you can see there are many values that will affect the size and positioning of the button upon rendering.

The button positioning is working fine. The problem is the text positioning inside the button.

So, in the button above the text:

  • x909 will show as ONLY an ‘x’ centered in the button, with maxLines = “1”
  • x909 shows as ONLY an ‘x9’ centered in the button , with maxLines = “2” , BUT the 9 shows below ‘x’
  • x909 shows as ONLY an ‘x90’ centered in the button, with maxLines =”3″ , BUT the x is on top, the 9 below it and the 0 below the 9
READ  [ANSWERED] flutter - Android studio and GitLab - how integration?
Powered by Inline Related Posts

The text is broken up into its characters and somehow a newline character is placed between characters so it displays vertically from top to down. I’m using LTR orientation

I’ve tried many different combinations using textAlignment, maxLines, lines, minLines, all types of gravity but every time the text is broken up into its characters and displayed vertically.

Any suggestions

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress