1

I would like to make button (the one between two other buttons) Square.

enter image description here

I tried to use following code:

toggleButton = (ToggleButton) findViewById(R.id.activity_dictionary_toggleButton1);
LinearLayout.LayoutParams toggleButtonLayoutParams = (LayoutParams) toggleButton.getLayoutParams(); 
toggleButtonLayoutParams.width = toggleButtonLayoutParams.height;
toggleButton.setLayoutParams(toggleButtonLayoutParams);

But it is not working (Button look like on the above image). Here is part of my XML file:

<LinearLayout
    android:id="@+id/activity_dictionary_linearLayout2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_marginTop="5dp" >

    <Button
        android:id="@+id/activity_dictionary_button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:layout_weight="0.4"
        android:enabled="false"
        android:maxWidth="200dp"
        android:text="Clear"
        android:textColor="@android:color/white" />

    <ToggleButton
        android:id="@+id/activity_dictionary_toggleButton1"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_gravity="center_vertical|center_horizontal"
        android:layout_weight="0.2"
        android:background="@drawable/settings"
        android:text="ToggleButton" />

    <Button
        android:id="@+id/activity_dictionary_button2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:layout_weight="0.4"
        android:enabled="false"
        android:maxWidth="200dp"
        android:text="Recognize"
        android:textColor="@android:color/white" />

</LinearLayout>

What should I change?

Update: android:layout_weight="0.2" is deleted, but Button still doesn't change its size when I try to do it through code (it is round but very small).

Renjith
  • 5,783
  • 9
  • 31
  • 42
Marek
  • 3,935
  • 10
  • 46
  • 70

1 Answers1

3

You should switch your LinearLayout to a RelativeLayout. Your size parameters should take effect then.

Jordan B.
  • 143
  • 7
  • But the parent Layout is LinearLayout. Why should I switch it to RelativeLayout? It is working but I don't understand why... – Marek Jun 13 '13 at 03:27
  • Forgive me for not having the most technical answer. LinearLayouts work best when you are trying to group things together and you want them to have the same display on all types of screens. You actually wanted to set heights and widths, and for that linearlayouts aren't the best thing. The relativelayout does not have these parameters, and actually uses the height and width you set. If you want more technical definitions, I would suggest looking at the developer notes http://developer.android.com/reference/android/widget/LinearLayout.html; – Jordan B. Jun 13 '13 at 03:44
  • The thing that I am confused about is that I thought you have to always yous LayoutParameters of Parent. For my Button, parent is LinearLayout. Why editor doesn't show the error? – Marek Jun 13 '13 at 04:22