I have an Activity with linear layout. It has two Views - one Button and one custom View beneath. I want to be able to set the custom one's height programmatically (it will be calculated during the startup from an image drawn onto it). And I want the button above it to fill the remaining space. How to achieve this? So far I have this XML code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099cc"
android:orientation="vertical"
tools:context="eu.example.app.PianoActivity">
<Button
android:id="@+id/dummy_button"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="@string/dummy_button"/>
<eu.example.app.PianoView
android:id="@+id/pianoView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:keepScreenOn="true"/>
</LinearLayout>
As you can see I added the weight of 1 and height of 0dp to the button, so it fills the remaining space. And since it is required to provide some value, I have given a fixed 50dp to the custom View. Now I tried to change the height using this code:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100, 100);
this.setLayoutParams(layoutParams);
this.requestLayout();
But it didn't do anything no matter where I placed it - whether in the View's constructors or in the onDraw() method.
//EDIT: Additional info: the custom view inherits directly from View and overrides only onDraw() method to draw some stuff on itself.