0

I'm making an application for an android and I have problem. I want change my XML code from LinearLayout to RelativeLayout. In one line, I want 4 progressbars. I can do it with LinearLayout but RelativeLayout is not.

Effect: enter image description here

Code:

      <LinearLayout
           android:id="@+id/lineral"
           android:layout_width="match_parent"
           android:layout_height="wrap_content">
       <ProgressBar
           android:id="@+id/kcal"
           style="@android:style/Widget.ProgressBar.Horizontal"
           android:layout_width="0dp"
           android:layout_height="25dp"
           android:layout_marginLeft="5dp"
           android:progress="25"
           android:layout_weight="1"
           android:progressTint="#69f0ae"
           />

       <ProgressBar
           android:id="@+id/protein"
           style="@android:style/Widget.ProgressBar.Horizontal"
           android:layout_width="0dp"
           android:layout_height="25dp"
           android:layout_marginLeft="5dp"
           android:progress="25"
           android:layout_toRightOf="@id/kcal"
           android:layout_weight="1"
           android:progressTint="#03A9F4"
           />

       <ProgressBar
           android:id="@+id/fat"
           style="@android:style/Widget.ProgressBar.Horizontal"
           android:layout_width="0dp"
           android:layout_height="25dp"
           android:layout_marginLeft="5dp"
           android:layout_toRightOf="@id/protein"
           android:layout_weight="1"
           android:progress="25"
           android:progressTint="#FFEB3B"
           />

       <ProgressBar
           android:id="@+id/carbohydrates"
           style="@android:style/Widget.ProgressBar.Horizontal"
           android:layout_width="0dp"
           android:layout_height="25dp"
           android:layout_marginLeft="5dp"
           android:layout_toRightOf="@id/fat"
           android:layout_weight="1"
           android:progress="25"
           android:progressTint="#F44336"
           />
       </LinearLayout>
Karan Mehta
  • 1,442
  • 13
  • 32
lakomika
  • 3
  • 4

2 Answers2

0

If you want to show all progress bar in the same width you have to use Linearlayout

If you are using relative layout, in all ProgressBar you have to set the width

Masoom Badi
  • 986
  • 9
  • 18
ThavaSelvan
  • 123
  • 6
0

You are using android:layout_weight=1 to set the weight of the progress bar But That is a property of LinearLayout.LayoutParams, but not of RelativeLayout.LayoutParams.

Alternative Solutions

  1. You can use Horizontally-oriented LinearLayout Manager in the Recycler View, and place each RelativeLayout in each item, of its Adapter. Check The Link

  2. If your RelativeLayouts are set to a fixed width and height, that is to the size of the screen, that you can get from DisplayMatrics, that will be OK. Check The Link

  3. If the contents of your RelativeLayouts are different, then you can use getItemViewType() method. Check The Link

Desired Constraint Layout View enter image description here

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ProgressBar
    android:id="@+id/oP"
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:layout_width="0dp"
    android:layout_height="25dp"
    android:progress="25"
    android:progressTint="#69f0ae"
    app:layout_constraintHorizontal_weight="1"
    app:layout_constraintRight_toLeftOf="@+id/oP2"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintLeft_toTopOf="parent"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Progress 1"
    app:layout_constraintEnd_toEndOf="@+id/oP"
    app:layout_constraintHorizontal_weight="1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintStart_toStartOf="@+id/oP"
    app:layout_constraintTop_toBottomOf="@+id/oP" />

<ProgressBar
    android:id="@+id/oP2"
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:layout_width="0dp"
    android:layout_height="25dp"
    android:progress="25"
    android:progressTint="#69f0ae"
    app:layout_constraintRight_toLeftOf="@+id/oP3"
    app:layout_constraintHorizontal_weight="1"
    app:layout_constraintLeft_toRightOf="@id/oP"
    app:layout_constraintLeft_toTopOf="parent"/>



<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Progress 2"
    app:layout_constraintEnd_toEndOf="@+id/oP2"
    app:layout_constraintHorizontal_weight="1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintStart_toStartOf="@+id/oP2"
    app:layout_constraintTop_toBottomOf="@+id/oP2" />

<ProgressBar
    android:id="@+id/oP3"
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:layout_width="0dp"
    app:layout_constraintRight_toLeftOf="@+id/oP4"
    android:layout_height="25dp"
    android:progress="25"
    android:progressTint="#69f0ae"
    app:layout_constraintHorizontal_weight="1"
    app:layout_constraintLeft_toRightOf="@id/oP2"
    app:layout_constraintLeft_toTopOf="parent"/>



<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Progress 2"
    app:layout_constraintEnd_toEndOf="@+id/oP3"
    app:layout_constraintHorizontal_weight="1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintStart_toStartOf="@+id/oP3"
    app:layout_constraintTop_toBottomOf="@+id/oP3" />


<ProgressBar
    android:id="@+id/oP4"
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:layout_width="0dp"
    android:layout_height="25dp"
    android:progress="25"
    android:progressTint="#69f0ae"
    app:layout_constraintLeft_toRightOf="@id/oP3"
    app:layout_constraintHorizontal_weight="1"
    app:layout_constraintLeft_toTopOf="parent"
    app:layout_constraintRight_toRightOf="parent" />


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Progress 4"
    app:layout_constraintEnd_toEndOf="@+id/oP4"
    app:layout_constraintHorizontal_weight="1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintStart_toStartOf="@+id/oP4"
    app:layout_constraintTop_toBottomOf="@+id/oP4" />

Attiq ur Rehman
  • 475
  • 1
  • 6
  • 21