vlkpo
vlkpo

Reputation: 199

Centering text in buttons

I have problem with my text in buttons. Everything is fine when is app running on mobile with big screen. In order to see all components also on small screen I set up weight on my buttons and other components. After button compress the text isn't in center but on button(I guess compression cut vertical dimension from bottom.)

xml file:

        <LinearLayout
            android:id="@+id/layout_image"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:background="@drawable/word_milk"
            android:gravity="center"
            android:orientation="vertical"
            android:visibility="visible" >

            <ImageView
                android:id="@+id/iv_cross"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/cross_small"
                android:visibility="invisible" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/layout_buttons"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:focusable="true"
            android:gravity="center_vertical"
            android:orientation="vertical"
            android:visibility="visible" >

            <Button
                android:id="@+id/b1"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="11dp"
                android:layout_weight="1"
                android:background="@drawable/shape_swarthy_green"
                android:gravity="center"
                android:maxHeight="45dp"
                android:onClick="B1Click"
                android:textColor="@color/Color_White"
                android:textSize="@dimen/TextSize_word" />

            <Button
                android:id="@+id/b2"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="11dp"
                android:layout_weight="1"
                android:background="@drawable/shape_swarthy_green"
                android:gravity="center"
                android:maxHeight="45dp"
                android:onClick="B2Click"
                android:textColor="@color/Color_White"
                android:textSize="@dimen/TextSize_word" />

            <Button
                android:id="@+id/b3"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_marginBottom="11dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="11dp"
                android:layout_weight="1"
                android:background="@drawable/shape_swarthy_green"
                android:gravity="center"
                android:maxHeight="45dp"
                android:onClick="B3Click"
                android:textColor="@color/Color_White"
                android:textSize="@dimen/TextSize_word" />
        </LinearLayout>
    </LinearLayout>

screenshot: I set up text size 24sp on first button,26sp on second ,28sp on thirth with text "llllll" for better ilustration of behavior.

enter image description here

Thank you for help.

Upvotes: 0

Views: 237

Answers (3)

iAndroid
iAndroid

Reputation: 951

set button property android:gravity="center" set property into java file also btn.setGravity(Gravity.CENTER);

Upvotes: 1

Daud Arfin
Daud Arfin

Reputation: 2499

Try with padding hopes it will work for you ...

<Button
            android:id="@+id/b2"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="11dp"
            android:layout_weight="1"
            android:background="@drawable/shape_swarthy_green"
            android:gravity="center"
            android:maxHeight="45dp"
            android:onClick="B2Click"
            android:textColor="@color/Color_White"
            android:textSize="@dimen/TextSize_word"

            // add padding here

            android:paddingTop="10dip"
            android:paddingBottom="10dip"                 


/>

Upvotes: 0

android developer
android developer

Reputation: 116332

my guess is that you didn't change the text size , so on other devices the text tries to fit on the button but it can't since the button is too small. if that's the case , set a different dimention for the text size depending on the screen .

however , since there is no code or screenshot here , i can't be sure if that's the case.

Upvotes: 0

Related Questions