Search code examples
androidandroid-layoutkotlinprogress-barandroid-progressbar

progress bar doesn't fill the space it's supposed to


I'm trying to achieve something like that:

enter image description here

and I tried using card view and ended with that:

enter image description here

and I have got a problem becaues I want the "water left" progress bar to go to the end of the card (as shown in the picture 1) but it doesn't change it's length (and I don't want to hard code that lenth), any ideas what can I do ?

(code:

<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:backgroundTint="@color/white"
    android:orientation="horizontal"
    card_view:cardCornerRadius="40dp"
    card_view:cardElevation="5sp"
    card_view:contentPadding="16dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:background="@color/white"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center_vertical"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                tools:srcCompat="@tools:sample/avatars" />
        </LinearLayout>

        <TableLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1">

            <TableRow>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="3dp"
                    android:text="name:"
                    android:textSize="14dp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/plc_ModelId"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="3dp"
                    android:textSize="14dp"
                    tools:text="12345678" />
            </TableRow>

            <TableRow>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="3dp"
                    android:text="water left:"
                    android:textSize="14dp"
                    android:textStyle="bold" />

                <ProgressBar
                    android:id="@+id/progressBar2"
                    style="?android:attr/progressBarStyleHorizontal"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="3dp" />

            </TableRow>

            <TableRow>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="3dp"
                    android:text="time left:"
                    android:textSize="14dp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/plc_Version"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="3dp"
                    android:textSize="14sp"
                    tools:text="00h 20 m" />
            </TableRow>

        </TableLayout>

    </LinearLayout>
</androidx.cardview.widget.CardView>

Solution

  • To achieve a full width use android:layout_weight="1" in your ProgressBar

            <TableRow>
    
                <TextView
                    android:text="water left:"
                 ../>
    
                <ProgressBar
                    android:id="@+id/progressBar2"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="match_parent"
                   />
    
            </TableRow>
    

    enter image description here

    Also using a LinearProgressIndicator provided by the Material Components Library and the trackThickness attribute you can achieve a different height:

    Something like:

      <com.google.android.material.progressindicator.LinearProgressIndicator
                android:id="@+id/progressBar2"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                card_view:trackThickness="12dp"
                card_view:trackColor="@color/white"
                card_view:indicatorColor="@color/red600Light"
                android:progress="75"
                android:layout_margin="3dp"
                />
    

    enter image description here