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>