Gravity Center refuses to work for Text inside buttons defined in ConstraintLayout of Fragment object

46 Views Asked by At

I'm trying to change the Gravity of the text inside a button. Do know that the button is defined inside a ConstraintLayout as follows:

<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="x"
        android:textColor="@color/greybackgroundd"
        style="@style/style13buttontext"
        android:onClick="EditorClicksModifiedForEditorLayout"
        android:background="@drawable/alternativecalcbuttonyellow"
        tools:ignore="MissingConstraints"
        />

here is how the Style attributes are defined:

 <style name="style13buttontext">
    <item name="android:fontFamily">@font/dosis_extrabold</item>
    <item name="android:textSize">13sp</item>
    <item name="android:textStyle">bold</item>
    <item name="android:textAllCaps">false</item>        
</style>

Also, keep in mind that I use a Flow helper in order to organize the buttons in the ConstraintLayout which serves as the Layout for a Fragment object that I create in code.

So, this layout is inflated inside the Fragment object onCreateView. I have been trying to modify the Gravity of the text but it always appears centered horizontally at the top of button.

The weird behavior is that say the layout in the fragment is showing on screen an a chat bubble appears from Facebook in the screen, the layout organizes itself and magically all the buttons display the text centered.

Inside the onAttach method of the Fragment object I get a hold of the buttons and change properties of the button text such as text color and set singleLine = true. All these changes work , but the one that never works is centering the text.

I have tried:

button.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICALLY); //no working
button.setGravity(Gravity.CENTER); //no working

The weird behavior is that when I use button.setGravity(Gravity.CENTER_VERTICALLY) it places the text at the start and top position of the button.

Any ideas or suggestions

Thanks

0

There are 0 best solutions below