John Pettersson
John Pettersson

Reputation: 41

Button gravity changes when the button is clicked

I have a big tablelayout with 28 buttons. (Making a calculator) Anyway when one of these buttons gets clicked the gravity of that button and even some buttons in the same row changes so that the text is aligned to the left end.

I have no idea and it´s starting to make me a bit crazy.

Here´s the layout.

<?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"
android:orientation="vertical" >

<LinearLayout
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/TableLayout1"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginBottom="10dp"
    android:background="#DDD"
    android:orientation="vertical" >

    <ScrollView
        android:background="#DDD"
        android:id="@+id/scrollView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <RelativeLayout
            android:id="@+id/RelativeLayout1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/textViewInput"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="#000" />

            <TextView
                android:id="@+id/textViewResult"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:layout_marginRight="5dp"
                android:gravity="right"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="#000" />

        </RelativeLayout>
    </ScrollView>
</LinearLayout>

<TableLayout
    android:id="@+id/TableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true" >

    <TableRow
        android:id="@+id/TableRow07"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:gravity="center_vertical"
        android:weightSum="5" >

        <Button
            android:id="@+id/Button06"
            style="@style/white_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1.2"
            android:text="@string/_root"
            android:textColor="@android:color/primary_text_light"
            android:gravity="center" />

        <Button
            android:id="@+id/bAsin"
            style="@style/white_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1.2"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:textColor="@android:color/primary_text_light"
            android:gravity="center" />

        <Button
            android:id="@+id/bAcos"
            style="@style/white_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1.2" 
            android:gravity="center"/>

        <Button
            android:id="@+id/batan"
            style="@style/white_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1.4"
            android:gravity="center" />
    </TableRow>

    <TableRow
        android:id="@+id/TableRow06"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:weightSum="5" >

        <Button
            android:id="@+id/Button04"
            style="@style/white_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1.2"
            android:text="@string/_pi"
            android:textColor="@android:color/primary_text_light"
            android:gravity="center" />

        <Button
            android:id="@+id/Button03"
            style="@style/white_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1.2"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:text="@string/_sin"
            android:textColor="@android:color/primary_text_light" 
            android:gravity="center"/>

        <Button
            android:id="@+id/Button02"
            style="@style/white_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1.2"
            android:text="@string/_cos"
            android:gravity="center" />

        <Button
            android:id="@+id/Button01"
            style="@style/white_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1.4"
            android:text="@string/_tan" 
            android:gravity="center"/>
    </TableRow>

    <TableRow
        android:id="@+id/TableRow05"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:weightSum="5" >

        <Button
            android:id="@+id/bPow"
            style="@style/white_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1.2"
            android:text="@string/_pow"
            android:textColor="@android:color/primary_text_light" 
            android:gravity="center"/>

        <Button
            android:id="@+id/blpar"
            style="@style/white_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1.2"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:text="@string/_lpar"
            android:textColor="@android:color/primary_text_light" 
            android:gravity="center"/>

        <Button
            android:id="@+id/brpar"
            style="@style/white_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1.2"
            android:text="@string/_rpar"
            android:gravity="center" />

        <Button
            android:id="@+id/bdiv"
            style="@style/white_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1.4"
            android:text="@string/_div"
            android:gravity="center" />
    </TableRow>

    <TableRow
        android:id="@+id/TableRow04"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:weightSum="5" >

        <Button
            android:id="@+id/b7"
            style="@style/white_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1.2"
            android:text="@string/_7"
            android:textColor="@android:color/primary_text_light" 
            android:gravity="center"/>

        <Button
            android:id="@+id/b8"
            style="@style/white_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1.2"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:text="@string/_8"
            android:textColor="@android:color/primary_text_light" 
            android:gravity="center"/>

        <Button
            android:id="@+id/b9"
            style="@style/white_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1.2"
            android:text="@string/_9"
            android:gravity="center" />

        <Button
            android:id="@+id/bmul"
            style="@style/white_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1.4"
            android:text="@string/_times"
            android:textStyle="bold"
            android:gravity="center" />
    </TableRow>

    <TableRow
        android:id="@+id/TableRow03"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:weightSum="5" >

        <Button
            android:id="@+id/b4"
            style="@style/white_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1.2"
            android:text="@string/_4"
            android:textColor="@android:color/primary_text_light"
            android:gravity="center" />

        <Button
            android:id="@+id/b5"
            style="@style/white_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1.2"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:text="@string/_5"
            android:textColor="@android:color/primary_text_light"
            android:gravity="center" />

        <Button
            android:id="@+id/b6"
            style="@style/white_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1.2"
            android:text="@string/_6"
            android:gravity="center" />

        <Button
            android:id="@+id/bsub"
            style="@style/white_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1.4"
            android:text="@string/_minus" 
            android:gravity="center"/>
    </TableRow>

    <TableRow
        android:id="@+id/TableRow02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:weightSum="5" >

        <Button
            android:id="@+id/b1"
            style="@style/white_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1.2"
            android:text="@string/_1"
            android:textColor="@android:color/primary_text_light" 
            android:gravity="center"/>

        <Button
            android:id="@+id/b2"
            style="@style/white_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1.2"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:text="@string/_2"
            android:textColor="@android:color/primary_text_light"
            android:gravity="center" />

        <Button
            android:id="@+id/b3"
            style="@style/white_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1.2"
            android:text="@string/_3" 
            android:gravity="center"/>

        <Button
            android:id="@+id/badd"
            style="@style/white_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1.4"
            android:text="@string/_plus" 
            android:gravity="center"/>
    </TableRow>

    <TableRow
        android:id="@+id/TableRow01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:weightSum="5" >

        <Button
            android:id="@+id/b0"
            style="@style/white_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1.2"
            android:text="@string/_0"
            android:textColor="@android:color/primary_text_light" 
            android:gravity="center"/>

        <Button
            android:id="@+id/bPoint"
            style="@style/white_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1.2"
            android:gravity="center"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:text="@string/dot"
            android:textColor="@android:color/primary_text_light" />

        <Button
            android:id="@+id/bClear"
            style="@style/white_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1.2"
            android:gravity="center"
            android:text="@string/clear" />

        <Button
            android:id="@+id/bEnter"
            style="@style/white_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1.4"
            android:text="@string/enter" />

        </TableRow>

    </TableLayout>
</RelativeLayout>

Anyone got any idea of whats wrong? I´m not touching the buttons through code, just checking the id to know which button was clicked. Nothing more, not changing gravity there or anything.

EDIT

oh yeah right, i forgot the white style. I´m pretty sure thats not it since i trie I dont know why it´s behaving like this.

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

<item android:state_pressed="true" >
    <shape android:shape="rectangle">
        <corners android:radius="0dp"/>
        <gradient android:startColor="#a8a8a8" android:endColor="#c8c8c8" android:angle="270"></gradient>
    </shape>
</item>

<item android:state_focused="true" >
    <shape>
        <solid android:color="#DDDDDD" />
     </shape>
 </item>



    <item>
        <shape android:shape="rectangle" >
        <corners android:radius="0dp"/>
        <solid android:color="#c8c8c8"/>
        </shape>
     </item>
</selector>

Upvotes: 3

Views: 377

Answers (1)

Daniel Wilson
Daniel Wilson

Reputation: 19824

I know I'm 5 years late to the party but this is still a fairly inexplicable bug even in Oreo.

For me it only happens sometimes (I have another app with the exact same layout in a "fragment A and fragment B side by side on the same screen situation" where it's not an issue).

When I throw the table layout onto a single screen and click a button (which updates a TextView above the table), the gravity of the button changes itself and won't change back.

My solution (and this is definitely a hack but it works), is to wrap the TableLayout in a FrameLayout and the TextView I update in it's own FrameLayout. This must force Android to skip trying to relayout the table because they are independent frames.

Upvotes: 1

Related Questions