Button text is not centered

I have a bunch of buttons on a very complex layout. The problem is, what ever I do, the buttons will not center the text inside them. I tried to add android: gravity = "center", I tried to set gravity from the code, I tried to remove all formatting from the button. What am I doing wrong? The text on the button should be centered.

This is my layout:

<?xml version="1.0" encoding="utf-8"?> 

 <FrameLayout android:layout_width="wrap_content" android:layout_height="130dp" android:layout_gravity="center" android:orientation="vertical"> <!-- android:background="@drawable/picture_frame_94x94_suggested_matches" --> <ImageView android:layout_width="wrap_content" android:src="@drawable/picture_unknown" android:layout_height="fill_parent" android:scaleType="centerInside" android:paddingTop="12dp" android:paddingBottom="40dp" android:id="@+id/suggest_icon" android:layout_gravity="center"></ImageView> <TextView android:layout_width="wrap_content" android:layout_gravity="center_horizontal|bottom" android:paddingLeft="4dp" android:paddingRight="4dp" android:layout_height="wrap_content" android:id="@+id/suggest_name" android:textAppearance="?android:attr/textAppearanceMedium" android:paddingBottom="12dp" android:textColor="@color/black"></TextView> </FrameLayout> <LinearLayout android:orientation="horizontal" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="0.4"> <Button android:id="@+id/unmatch_button" android:text="Un-match" android:textColor="@color/white" android:background="@drawable/button_background" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="8dp" android:layout_marginLeft="8dp" android:gravity="center" android:visibility="gone" android:textAppearance="?android:attr/textAppearanceMedium" /> <Button android:id="@+id/nextmatch_button" android:text="@string/next" android:textColor="@color/white" android:background="@drawable/button_background" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="8dp" layout_marginLeft="8dp" android:gravity="center" android:visibility="gone" android:textAppearance="?android:attr/textAppearanceMedium" /> <Button android:id="@+id/donematchpicker_button" android:text="@string/done" android:textColor="@color/white" android:background="@drawable/button_background" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="8dp" layout_marginLeft="8dp" android:gravity="center" android:visibility="gone" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_gravity="center_horizontal|bottom" android:paddingLeft="4dp" android:paddingRight="4dp" android:layout_height="wrap_content" android:id="@+id/suggest_count" android:textAppearance="?android:attr/textAppearanceMedium" android:paddingBottom="12dp" android:textColor="@color/black"></TextView> <include layout="@layout/facebook_list" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <View android:id="@+id/list_devider" android:layout_width="fill_parent" android:layout_height="1dp" android:background="?android:attr/listDivider" android:visibility="gone" /> <LinearLayout android:orientation="horizontal" android:layout_weight="0.4" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:id="@+id/exit_button" android:text="@string/exit" android:textColor="@color/white" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_background" android:layout_marginRight="8dp" layout_marginLeft="8dp" android:visibility="gone" android:gravity="center" android:layout_marginTop="12dp" android:textAppearance="?android:attr/textAppearanceMedium" /> <Button android:id="@+id/donematch_button" android:text="@string/done" android:textColor="@color/white" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/button_background" android:layout_marginRight="8dp" layout_marginLeft="8dp" android:visibility="gone" android:gravity="center" android:layout_marginTop="12dp" android:textAppearance="?android:attr/textAppearanceMedium" /> </LinearLayout> 

This is the XML of the background dialog:

 <?xml version="1.0" encoding="utf-8"?> 

 <solid android:color="#969798" /> <stroke android:width="3dp" android:color="#7dd6ee" /> <corners android:bottomRightRadius="6dp" android:bottomLeftRadius="6dp" android:topLeftRadius="6dp" android:topRightRadius="6dp"/> 

screenshot: http://i39.tinypic.com/fc8aoo.png

This is a sample button initialization:

  nextButton = (Button)findViewById(R.id.nextmatch_button); nextButton.setVisibility(View.VISIBLE); nextButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { public void run() { // bunch of code }); nextButton.setGravity(Gravity.CENTER); 

This is the button background definition:

  <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/button_small_hover" /> <item android:state_focused="true" android:drawable="@drawable/button_small_hover" /> <item android:state_enabled="false" android:drawable="@drawable/button_small2_disabled" /> <item android:drawable="@drawable/button_small2" /> </selector> 

What causes text alignment, not centering, what am I doing with it? I don't seem to understand.

+4
source share
4 answers

Set the equal padding of the button, for example:

 nextButton.setPadding(2,2,2,2); 

Hope this solves the problem.

+3
source

In your code for each button you

Android: width = "wrap_content"

So, regardless of the length of the text, the button will not take up so much space,

therefore there is no question of creating a text center

0
source

Try making your button a fixed width.

 layout_width="100dip" 
-1
source

I have one random solution for this, just specify spaces at the start and end of the text, for example "android: text =" xyz ".

-1
source

Source: https://habr.com/ru/post/1382153/


All Articles