Amar Mishra
Amar Mishra

Reputation: 89

Android Button loses its Text Alignment

I have some buttons and a Spinner in my Layout. I Play Music on button click. When Button is pressed then button text gets aligned to left. I don't know why its happening. If I comment button click listener and then if I change items in Spinner , then also it gets Left Aligned.

Here is the part of my code.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" >

        <Spinner
            android:id="@+id/spnList"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:padding="5dp" >

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <Button
                android:id="@+id/btnCall"
                android:layout_weight="1"
                android:text="C" />

            <Button
                android:id="@+id/btnCut"
                android:layout_weight="1"
                android:text="X" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <Button
                android:id="@+id/btn1"
                android:layout_weight="1"
                android:text="1" />

            <Button
                android:id="@+id/btn2"
                android:layout_weight="1"
                android:text="2" />

            <Button
                android:id="@+id/btn3"
                android:layout_weight="1"
                android:text="3" />
        </TableRow>
    </TableLayout>

</RelativeLayout>

Here is Activity Code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main1);
    btn1 = (Button) findViewById(R.id.btn1);
    btn2 = (Button) findViewById(R.id.btn2);
    btn3 = (Button) findViewById(R.id.btn3);
    spnList = (Spinner) findViewById(R.id.spnList);
    List<String> list = new ArrayList<String>();
    list.add("First");
list.add("Second");

ArrayAdapter<String> adapter = new ArrayAdapter<String>(
        BabyMobile1.this, android.R.layout.simple_spinner_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spnList.setAdapter(adapter);
spnList.setOnItemSelectedListener(new OnItemSelectedListener() {

    public void onItemSelected(AdapterView<?> arg0, View arg1,
            int arg2, long arg3) {
        // TODO Auto-generated method stub
    activeItem = (String) arg0.getItemAtPosition(arg2);
}

public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

    }
});

Upvotes: 1

Views: 738

Answers (3)

Devrim
Devrim

Reputation: 15543

You are not defining width and height attributes on your buttons. Try with adding below attributes.

android:layout_width="0dp"
android:layout_height="wrap_content"

Example:

<Button
    android:id="@+id/btnCall"
    android:layout_weight="1"
    android:text="C" 
    android:layout_width="0dp"
    android:layout_height="wrap_content"/>

Edit: Answer above gives a solution to keep texts of buttons center horizantal but their texts are shifted up when I tested. I searched this a while and found similar problem (not answered yet) at stack. I guess there is a small bug when using Spinners(or dialogs) inside a RelativeLayout. I tried to change your layout below(changed root to LinearLayout) and it worked as expected. If you have a chance to replace your layout with mine(below) your problem will be solveld. But I have no idea why using RelativeLayout causes a problem like this.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Spinner
            android:id="@+id/spnList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/btnCall"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="C" />

            <Button
                android:id="@+id/btnCut"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="X" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <Button
                android:id="@+id/btn1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="1" />

            <Button
                android:id="@+id/btn2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="2" />

            <Button
                android:id="@+id/btn3"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="3" />
        </TableRow>
    </TableLayout>

</LinearLayout>

Upvotes: 1

balaji koduri
balaji koduri

Reputation: 1321

check this link it may helps you.

http://developer.android.com/reference/android/widget/Spinner.html#attr_android:gravity

here there is a tag in Spinner

android:gravity="   "

using this align the selected item in required position.

Upvotes: 0

balaji koduri
balaji koduri

Reputation: 1321

Check the alignments of the button and spinner, if you are using Custom items in spinner then you should check the custom item alignment also.

better understanding purpose give me the code what u are writeing and where you are getting problem?

Upvotes: 0

Related Questions