0

I change the width of a view dynamically in my code using:

            final ImageButton ButtonTwo = (ImageButton)findViewById(R.id.callButton);

            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) ButtonTwo.getLayoutParams();
            params.weight = 1.3f;
            ButtonTwo.setLayoutParams(params);

The idea is that it matches another view in XML :

<ImageButton
    android:layout_height="match_parent"
    android:layout_width="0dp"
    android:id="@+id/imageButton"
    android:src="@drawable/softkeyboard"
    android:clickable="true"
    android:onClick="softkeyboardButton"
    android:layout_gravity="center_horizontal"
    android:layout_weight="1.3"
    android:background="@drawable/buttonstates"
     />

They're not the same width - the first button is wider than the second one. I know that this is because one is float and one is decimal, but how can I make them both the decimal size, or should I just do trial and error on the float value? My params.weight only accepts float values.

EDIT: Here's my XML code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:paddingBottom="0dp"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/ListView_2"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_weight="1">
    </ListView>
<!--Let's set the height of the linearlayout below to 0dp. Apparently it's less work on resources,
as otherwise we would be drawing it twice, because the size is also set dynamically-->
<LinearLayout
    android:id="@+id/LinearLayout2"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal"
        >
        <!--android:adjustViewBounds="true"-->
    <!-- we want the text to begin in the centre of the edittext view
    that's what gravity does-->

    <EditText
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:id="@+id/editText"
        android:inputType="phone"
        android:gravity="center"
        android:background="#d9d9d9"
        android:layout_weight="4"

        />


        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:id="@+id/callButton"
            android:src="@drawable/call"
            android:clickable="true"
            android:onClick="softkeyboardButton"
            android:background="#ffa62b"
            android:layout_weight="2"

            />
</LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal" >

    <CheckBox
        android:layout_height="match_parent"
        android:layout_width="0dp"
        android:background="#ffa500"
        android:text="New CheckBox"
        android:id="@+id/checkBox"
        android:layout_weight="5"
        />


        <ImageButton
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:id="@+id/imageButton"
            android:src="@drawable/softkeyboard"
            android:clickable="true"
            android:onClick="softkeyboardButton"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1.3"
            android:background="@drawable/buttonstates"
             />

        <View android:layout_height="fill_parent"
            android:layout_width="2px"
            android:background="#90909090"/>

        <ImageButton
            android:layout_height="match_parent"
            android:layout_width="0dp"
            android:id="@+id/imageButton2"
            android:src="@drawable/settingsicon"
            android:background="@drawable/buttonstates"
            android:layout_gravity="center_horizontal"
            android:layout_weight="1.3" />

    </LinearLayout>

    </LinearLayout>
CHarris
  • 2,693
  • 8
  • 45
  • 71
  • 1
    If the idea is to just have them match the `LayoutParams`, why not just retrieve the `LayoutParams` of the `ImageButton` indicated in the xml, then set it as the `LayoutParams` of the 'ImageButton` you want dynamically? Like so: `buttonTwo.setLayoutParams(buttonOne.getLayoutParams());` – AL. Mar 16 '16 at 01:51
  • That's an interesting idea. Will look at it when home. – CHarris Mar 16 '16 at 08:25
  • Let me know if it works, if it does, I'll add it as an answer. – AL. Mar 16 '16 at 08:26
  • 1
    You can add it as an answer, for getting me round to that way of thinking. I was having terrible trouble with the float - params.weight = 1.3f. The widths were never exactly the same, even though I was using your setLayoutParams = getLayoutParams. Probably something to do with floats never being accurate, I don't know. But then I changed it to params.width= 80, and it was exact. You might add that part in ur answer, for clarification purposes to other users. – CHarris Mar 17 '16 at 02:13
  • 1
    Posted it as an answer as you advised. But also just to ask, have you seen this particular [answer](http://stackoverflow.com/a/8780360/4625829) where it mentions something different in retrieving float values? – AL. Mar 17 '16 at 02:21
  • 1
    Interesting answer alright. But in relation to my issue with your solution, 'width' is working just fine. – CHarris Mar 17 '16 at 02:31

2 Answers2

1

Posting this as answer as advised. :)

If the idea is to just have them match the LayoutParams, why not just retrieve the LayoutParams of the ImageButton indicated in the xml, then set it as the LayoutParams of the ImageButton you want dynamically? Like so:

buttonTwo.setLayoutParams(buttonOne.getLayoutParams());

And to quote @Christophe Harris for other users:

I was having terrible trouble with the float - params.weight = 1.3f. The widths were never exactly the same, even though I was using your setLayoutParams = getLayoutParams. Probably something to do with floats never being accurate, I don't know. But then I changed it to params.width= 80, and it was exact.

AL.
  • 36,815
  • 10
  • 142
  • 281
0

You need to set layout_width to "0dp" for ButtonTwo for layout_weight to work correctly.

Hope this should work for you!