mp94
mp94

Reputation: 47

Android: Horizontal align ProgressBar with ratio enabled in ConstraintLayout

I'm trying to make a responsive layout in Android with ConstraintLayout. In particular I'm trying to make a ProgressBar having width equals to its height (width and height are not fixed). I achieved this by using app:layout_constraintDimensionRatio="h,1:1" attribute in my progress bar. However I'm not able to horizontal align it, because If use the following attributes...

app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"

...my app:layout_constraintDimensionRatio="h,1:1" attribute stops working. I tried several approaches (even programmatically) but none of them worked. How can I achieve what I want?

This is my layout full code:

<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:design="http://schemas.android.com/apk/res-auto"
tools:context="pvsys.mauro.heartcheck.MainFragment"
android:id="@+id/constraint_container"
android:background="@color/colorBackground">

<include
    layout="@layout/tool_bar"
    android:layout_height="wrap_content"
    android:layout_width="0dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>


<android.support.constraint.Guideline
    android:id="@+id/guidelineBottomBackground"
    android:layout_height="1dp"
    android:layout_width="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.65"/>

<View
    android:id="@+id/myBackgroundView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintBottom_toTopOf="@+id/guidelineBottomBackground"
    android:background="@android:color/white"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintVertical_bias="0.0" />


<ProgressBar
    android:id="@+id/loading_spinner"
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:layout_height="0dp"
    android:layout_width="0dp"
    android:indeterminateTintMode="src_atop"
    android:indeterminateTint="@color/colorPrimary"
    android:max="500"
    android:progress="500"
    android:progressDrawable="@drawable/circular"
    app:layout_constraintDimensionRatio="h,1:1"
    app:layout_constraintTop_toBottomOf="@+id/tool_bar"
    app:layout_constraintBottom_toTopOf="@+id/tabLayout"
    />

<include
    layout="@layout/tab_layout"
    android:layout_height="48dp"
    android:layout_width="0dp"
    app:layout_constraintBottom_toTopOf="@+id/viewpager"
    app:layout_constraintRight_toRightOf="@+id/viewpager"
    android:layout_marginBottom="8dp"
    app:layout_constraintLeft_toLeftOf="@+id/viewpager" />

<android.support.constraint.Guideline
    android:id="@+id/guidelineTopViewPager"
    android:layout_height="1dp"
    android:layout_width="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.55"/>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_height="0dp"
    android:layout_width="0dp"
    android:elevation="4dp"
    android:background="@drawable/corners_bg"
    app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
    app:layout_constraintTop_toBottomOf="@+id/guidelineTopViewPager"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp">

</android.support.v4.view.ViewPager>

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="0dp"
    android:layout_height="56dp"
    android:background="@android:color/white"
    app:itemIconTint="@drawable/nav_item_color_state"
    app:itemTextColor="@drawable/nav_item_color_state"
    app:menu="@menu/bottom_menu"
    android:elevation="16dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent" />

</android.support.constraint.ConstraintLayout>

This is the resulting GUI

Upvotes: 2

Views: 1259

Answers (2)

Cheticamp
Cheticamp

Reputation: 62831

I assume that you want loading_spinner to be square and centered in the parent. If so, try the following in the XML for loading_spinner:

    app:layout_constraintDimensionRatio="1:1"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"

If this doesn't address your issue, it would help if you could give the detail behind: "my app:layout_constraintDimensionRatio="h,1:1" attribute stops working".

Upvotes: 0

Rami Jemli
Rami Jemli

Reputation: 2641

As you used android:indeterminateTint, I suppose you want a circular progress bar. However, you are using style="@android:style/Widget.ProgressBar.Horizontal".

Try this

<ProgressBar
    android:id="@+id/loading_spinner"
    android:layout_height="0dp"
    android:layout_width="0dp"
    android:indeterminateTintMode="src_atop"
    android:indeterminateTint="@color/colorPrimary"
    android:max="500"
    android:progress="500"
    android:progressDrawable="@drawable/circular"
    app:layout_constraintDimensionRatio="1"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/tool_bar"
    app:layout_constraintBottom_toTopOf="@+id/tabLayout"
    /> 

Upvotes: 4

Related Questions