Set same height as width at a ToggleButton
Asked Answered
P

1

0

I have following Layout, with 7 ToggleButtons in a row. Independently from the resolution, I have always 7 Buttons in the complete horizontal width. Now I want to set the same height as the width but it does not work. I tried following without success ():

    tb_Mo = (ToggleButton) findViewById(R.id.tB_Mo);

    int btnSize=tb_Mo.getWidth();
    tb_Mo.setHeight(btnSize);
      //and so on for the other Buttons...

Second try without success:

    int btnSize=tb_Mo.getLayoutParams().width;
    tb_Mo.setLayoutParams(new LinearLayout.LayoutParams(btnSize, btnSize));
         //and so on for the other Buttons...

XML:

<ToggleButton
    android:id="@+id/tB_Mo"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/btntoggle_selector"
    android:textColor="@android:color/white"
    android:textOff="OFF"
    android:textOn="ON" 
    android:layout_weight="1"/>  

    <ToggleButton
    android:id="@+id/tB_Di"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/btntoggle_selector"
    android:textColor="@android:color/white"
    android:textOff="OFF"
    android:textOn="ON" 
    android:layout_weight="1"/>  

        <ToggleButton
    android:id="@+id/tB_Mi"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/btntoggle_selector"
    android:textColor="@android:color/white"
    android:textOff="OFF"
    android:textOn="ON" 
    android:layout_weight="1"/> 
          and so on ....

What can I do?

Here I want have the same height as the width

Phonic answered 20/9, 2013 at 17:39 Comment(5)
You should be using android:layout_width="0dp" for your <ToggleButton>s. Perhaps that will work.Soubise
Thanks for the Answer, but its unfortunately not workingPhonic
Could you provide the full xml layout? If the parent is taking up the whole screen and these are the only things in it right now, you would probably be getting this effect.Tacnaarica
Could also be that your layout_height is set to wrap_content, this may prevent override from your Java function. Try setting it to a specific dp.Soubise
Yes it worked!!! I set it to 50dp. Thanks a lot!!!Phonic
P
0

Solution:

   <ToggleButton
    android:id="@+id/tB_Sa"
    android:layout_width="0dp"
    android:layout_height="50dp"
    android:background="@drawable/btntoggle_selector"
    android:textColor="@android:color/white"
    android:textOff="OFF"
    android:textOn="ON" 
    android:layout_weight="1"/>  

And:

    int btnSize=tb_Mo.getWidth();
    tb_Mo.setHeight(btnSize);
Phonic answered 20/9, 2013 at 18:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.