1

There is a button with name "A" without any gravity. So A is at the left side. I want to set another text at the center of this button without changing string "A" and its place? How can I do that?

Sorry for my english hope you understand.

2 Answers2

1

I think you cannot do that within a single button but you can make a RelativeLayout that will look like a Button, and then set the different TextView for different characters.

In that case your code should look like this

<RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="A" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:text="New Text" />
    </RelativeLayout>

and also you can create a selector and apply it to RelativeLayout to achive the effect of a button.Follow this https://stackoverflow.com/a/14024007/3789993

Hope that helps

AbhayBohra
  • 2,047
  • 24
  • 36
0

The easiest way to do it, but not recommended is just add white spaces after "A":

<Button
  android:layout_width="WidthThatYouWant"
  android:layout_height="HeightThatYouWant"
  android:text="A            TextYouWantToAdd"
 />

And the best way to do it, and recommended, is create a Layout that looks like a button:

<RelativeLayout
    android:layout_width="WidthThatYouWant"
    android:layout_height="HeightThatYouWant"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextYouWantToAdd"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

Then you can customize the LinearLayout to have the same format that your button, adding this:

android:background="@drawable/circularRedButton.xml"

For example here, is a button red and circular.

The file circularRedButton.xml is:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="false">
        <shape android:shape="rectangle">
            <corners android:radius="1000dp" />
            <solid android:color="#ff0000" />
            <stroke
                android:width="2dip"
                android:color="#c20000" />
            <padding
                android:bottom="4dp"
                android:left="4dp"
                android:right="4dp"
                android:top="4dp" />
        </shape>
    </item>
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <corners android:radius="1000dp" />
            <solid android:color="#ff0000" />
            <stroke
                android:width="2dip"
                android:color="#c20000" />
            <padding
                android:bottom="4dp"
                android:left="4dp"
                android:right="4dp"
                android:top="4dp" />
        </shape>
    </item>
</selector>
Alex Bean
  • 493
  • 7
  • 19
  • I created Relativelayout and I set drawable to it. But when I trying to add textview inside that layout, text is not showing. My drawable is png image filled with blue. – Elcin shixaliyev Mar 29 '18 at 14:09
  • Try to add this in the two TextViews: android:textColor="#ffffff" Is to set the color of the texts to white: – Alex Bean Mar 29 '18 at 14:29