0

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

ChrisK0043
  • 91
  • 7
  • It might because of width and height is set to zero. please share the complete xml here . – xaif Apr 18 '20 at 16:19
  • But if they are set to zero, why are they showing up on screen. The button shows is just that the text always stays with Gravity = center_horizontal | top @xaif – ChrisK0043 Apr 18 '20 at 17:56

0 Answers0