0

Why is my toggle button taking the wrong width and have the space of text?

enter image description here

I want this toggle button to be exactly the same width as the other buttons

Here is the code:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#242424"
        android:orientation="horizontal" >

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/ten_dp"
            android:layout_weight="30"
            android:background="@drawable/action_bar_btn_bg"
            android:text="Close"
            android:textColor="@color/abc_primary_text_material_dark" />


        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="70"
            android:gravity="right"
            android:orientation="horizontal"
            android:padding="@dimen/ten_dp" >

            <ToggleButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="@dimen/five_dp"
                android:background="@drawable/action_bar_btn_bg"
                android:button="@drawable/btn_fav"
                android:gravity="center"
                android:textOn="@null"
                android:textOff="@null" />

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="@dimen/five_dp"
                android:background="@drawable/action_bar_btn_bg"
                android:src="@drawable/ic_action_left" />

            <ImageButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/action_bar_btn_bg"
                android:src="@drawable/ic_action_right" />
        </LinearLayout>
    </LinearLayout>

@drawable/action_bar_btn_bg

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/action_bar_btn_bg_pressed" android:state_enabled="true" android:state_pressed="true"/>
<item android:drawable="@drawable/action_bar_btn_bg_pressed" android:state_enabled="true" android:state_focused="true"/>
<item android:drawable="@drawable/action_bar_btn_bg_pressed" android:state_enabled="true" android:state_selected="true"/>
<item android:drawable="@drawable/action_bar_btn_bg_normal"/>

@drawable/btn_fav

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/ic_action_star_normal" android:state_checked="false" />
<item android:drawable="@drawable/ic_action_star_active" android:state_checked="true"/>

Please help me and tell me where I am going wrong? Thank you.

cchapman
  • 3,269
  • 10
  • 50
  • 68
awaistoor
  • 786
  • 2
  • 11
  • 36

1 Answers1

0

Try adding a weight sum tag in the linear layout

    android:weightSum = "3"

Then add weight attributes to the toggle and image buttons

    android:weight="1"

EDIT: since you want to center the image within the toggleButton, I recommend trying this in addition:

<ToggleButton
    android:id="@+id/YourID
    android:layout_width="0"
    android:layout_height="75dp"
    android:layout_weight="1"
    android:checked="false"
    android:textOn=""
    android:textOff=""
    android:drawableTop="@drawable/yourImage"
    android:layout_gravity="center"
    android:textColor="#yourColor"
  • Sir thank you for reply. I have tried that before but the problem is the star (which you can see in the image) stays on the left side of the button, it should be in the middle like others. I have tried `android:gravity="center"` but it didn't work. – awaistoor Mar 14 '15 at 00:32
  • not helpful as the star is still not in the middle, also why did you gave static height as you can see from picture the this bar is on the bottom of the screen – awaistoor Mar 14 '15 at 21:45