How to put weight to views added dynamically on an Android layout?

Viewed 60

I have the following Layout:

<com.google.android.material.card.MaterialCardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cat_card_list_item_card"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="8dp"
android:minHeight="118dp" >

    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/effectContainer"
            android:orientation="vertical"/>


</com.google.android.material.card.MaterialCardView>

enter image description here

which gets added dynamically with the objects as you see below:

Tremolo + button is one object, and the rest is 2 objects that have each one a vertical seekbar. I want the first object to have height wrap_content, which seems it does,a nd the rest to expand vertically as max as possible, so the seekbars grow. I've made their height "wrap_content":

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/paramWrapper"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="5dp">

    <TextView
        android:id="@+id/paramLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:textAppearance="@style/TextAppearance.MaterialComponents.Subtitle1" />

    <TextView
        android:id="@+id/curLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:textAppearance="@style/TextAppearance.MaterialComponents.Subtitle2" />

    <TextView
        android:id="@+id/minLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.25"
        android:textAppearance="@style/TextAppearance.MaterialComponents.Caption" />

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="10"
        android:rotation="270" />

    <TextView
        android:id="@+id/maxLabel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.25"
        android:textAppearance="@style/TextAppearance.MaterialComponents.Caption" />

</LinearLayout>

but they still wont grow. How do I make them take the entire space they can?

2 Answers

Have you tried

android:layout_width="match_parent"
android:layout_height="match_parent" 

for the SeekBar?

You can solve it out with this custom library... Just add this to your build.gradle

implementation 'com.h6ah4i.android.widget.verticalseekbar:verticalseekbar:1.0.0'

And your XML should be like this:

<com.h6ah4i.android.widget.verticalseekbar.VerticalSeekBarWrapper
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="10">
        <com.h6ah4i.android.widget.verticalseekbar.VerticalSeekBar
            android:id="@+id/mySeekBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:max="100"
            android:progress="0"
            android:splitTrack="false"
            app:seekBarRotation="CW90" /> <!-- Rotation: CW90 or CW270 -->
    </com.h6ah4i.android.widget.verticalseekbar.VerticalSeekBarWrapper>

I have tested this, and it will give you exactly what you want

Related