2

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.

Leprechaun
  • 852
  • 6
  • 25
  • Side note: if your `PianoView` extends `ImageView`, you may be able to do this entirely in XML. Take a look at http://stackoverflow.com/a/19316787/996592 for details. – Fabian Tamp Nov 04 '15 at 05:46
  • Actually, some more information about the PianoView in general could be helpful. Can you post the source? What does it inherit from? How custom is it? Does it override onMeasure() or onLayout()? – Fabian Tamp Nov 04 '15 at 05:48
  • refer this thread http://stackoverflow.com/questions/15501831/add-layout-with-parameters-programmatically-android – sarath19 Nov 04 '15 at 10:13
  • @Fabian Tamp There is no source with any additional information value. It inherits directly from View and so far only overrides the onDraw() method to draw something on it. – Leprechaun Nov 05 '15 at 00:05

3 Answers3

1

You should use

public void onWindowFocusChanged(boolean hasFocus) {}

method to set height and width.

Saeed Jassani
  • 1,079
  • 2
  • 11
  • 27
1

Put an id attribute ot your linear layout in xml as:-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:id="@+id/liLay"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#0099cc"
              android:orientation="vertical"
              tools:context="eu.example.app.PianoActivity">
<Button ---/>
<eu.example.app.PianoView---/>
</LinearLayout>

Now in your onCreate() method initialse the layout, as:-

LinearLayout liLay = (LinearLayout)findViewById(R.id.liLay);

and now set the layout Parameters progmatically,

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100, 100);
liLay.setLayoutParams(layoutParams);

This will help you.

Pravinsingh Waghela
  • 2,516
  • 4
  • 32
  • 50
0

Use this:

layoutParams.height = 130;
yourLayout.setLayoutParams(layoutParams);
Kaveesh Kanwal
  • 1,753
  • 17
  • 16