Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PercentRelativeLayout is deprecated in v26

I want to display a layout with 6 views which I am setting by width and height percentage. But PercentageLayout is deprecated in Android v26 What is the alternative for PercentageLayout. Android documentation is saying to use ConstraintLayout with app:layout_constraintGuide_percent. (https://developer.android.com/reference/android/support/percent/PercentRelativeLayout.html) But for PercentageLayout we can use

 app:layout_heightPercent="33.4%"
    app:layout_widthPercent="45%"
like image 945
Ameer Avatar asked Dec 18 '17 13:12

Ameer


2 Answers

There're two ways to implement your layout with ConstraintLayout.

#1 Guidelines

Considering you need to lay out six views you need to add two vertical guidelines with percentage position 33% and 66% of parent width. And you also need to add two horizontal guidelines with percentage position 45% and 90% of screen height. Then constrain your six views to those guidelines as shown on this screenshot:

Layout Editor

Here's XML source code for layout build with guidelines:

<android.support.constraint.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">

    <View
        android:id="@+id/view1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_blue_bright"
        app:layout_constraintBottom_toTopOf="@+id/horizontal_guideline_1"
        app:layout_constraintEnd_toStartOf="@+id/vertical_guideline_1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/view2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_green_dark"
        app:layout_constraintBottom_toTopOf="@+id/horizontal_guideline_1"
        app:layout_constraintEnd_toStartOf="@+id/vertical_guideline_2"
        app:layout_constraintStart_toStartOf="@+id/vertical_guideline_1"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/view3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_purple"
        app:layout_constraintBottom_toTopOf="@+id/horizontal_guideline_1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/vertical_guideline_2"
        app:layout_constraintTop_toTopOf="parent" />

    <View
        android:id="@+id/view4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_orange_light"
        app:layout_constraintBottom_toTopOf="@+id/horizontal_guideline_2"
        app:layout_constraintEnd_toStartOf="@+id/vertical_guideline_1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/horizontal_guideline_1" />

    <View
        android:id="@+id/view5"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_red_light"
        app:layout_constraintBottom_toTopOf="@+id/horizontal_guideline_2"
        app:layout_constraintEnd_toStartOf="@+id/vertical_guideline_2"
        app:layout_constraintStart_toStartOf="@+id/vertical_guideline_1"
        app:layout_constraintTop_toTopOf="@+id/horizontal_guideline_1" />

    <View
        android:id="@+id/view6"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_blue_dark"
        app:layout_constraintBottom_toTopOf="@+id/horizontal_guideline_2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/vertical_guideline_2"
        app:layout_constraintTop_toTopOf="@+id/horizontal_guideline_1" />

    <android.support.constraint.Guideline
        android:id="@+id/vertical_guideline_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.33" />

    <android.support.constraint.Guideline
        android:id="@+id/vertical_guideline_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.66" />

    <android.support.constraint.Guideline
        android:id="@+id/horizontal_guideline_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.45" />

    <android.support.constraint.Guideline
        android:id="@+id/horizontal_guideline_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.9" />

</android.support.constraint.ConstraintLayout>

#2 Chains with weight

Chains is another powerful way to achieve sizing your views with percentage values. You use weight value in ConstraintLayout chain like you use weight in LinearLayout.

Here's XML example showing how to use chains with weight:

<android.support.constraint.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">

    <View
        android:id="@+id/view1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_blue_bright"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintHorizontal_weight="33.3"
        app:layout_constraintVertical_chainStyle="spread"
        app:layout_constraintVertical_weight="45"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/view2"
        app:layout_constraintBottom_toTopOf="@+id/view4" />

    <View
        android:id="@+id/view2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_green_dark"
        app:layout_constraintHorizontal_weight="33.3"
        app:layout_constraintTop_toTopOf="@+id/view1"
        app:layout_constraintStart_toEndOf="@+id/view1"
        app:layout_constraintEnd_toStartOf="@+id/view3"
        app:layout_constraintBottom_toBottomOf="@+id/view1" />

    <View
        android:id="@+id/view3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_purple"
        app:layout_constraintHorizontal_weight="33.3"
        app:layout_constraintTop_toTopOf="@+id/view2"
        app:layout_constraintStart_toEndOf="@+id/view2"
        app:layout_constraintBottom_toBottomOf="@+id/view2"
        app:layout_constraintEnd_toEndOf="parent" />

    <View
        android:id="@+id/view4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_orange_light"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintHorizontal_weight="33.3"
        app:layout_constraintVertical_weight="45"
        app:layout_constraintTop_toBottomOf="@+id/view1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/view5"
        app:layout_constraintBottom_toTopOf="@+id/space" />

    <View
        android:id="@+id/view5"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_red_light"
        app:layout_constraintHorizontal_weight="33.3"
        app:layout_constraintTop_toTopOf="@+id/view4"
        app:layout_constraintStart_toEndOf="@+id/view4"
        app:layout_constraintEnd_toStartOf="@+id/view6"
        app:layout_constraintBottom_toBottomOf="@+id/view4" />

    <View
        android:id="@+id/view6"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/holo_blue_dark"
        app:layout_constraintHorizontal_weight="33.3"
        app:layout_constraintTop_toTopOf="@+id/view5"
        app:layout_constraintStart_toEndOf="@+id/view5"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="@+id/view5" />

    <Space
        android:id="@+id/space"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintVertical_weight="10"
        app:layout_constraintTop_toBottomOf="@+id/view4"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

</android.support.constraint.ConstraintLayout>

Both of examples shown above lead to this result:

Result view

Read more about:

  • Guidelines
  • Chains
like image 126
Eugene Brusov Avatar answered Sep 21 '22 12:09

Eugene Brusov


What is the alternative for PercentageLayout

Use ConstraintLayout, as indicated in your question.

With the now-current production release of ConstraintLayout (1.0.2), set up a guideline at the desired percent location and constrain your widgets to the guideline.

With the upcoming 1.1.x series (now 1.1.0-beta4), you can use layout_constraintWidth_percent and layout_constraintHeight_percent on any axis for which you set the size to 0dp (a.k.a., MATCH_CONSTRAINT).

like image 25
CommonsWare Avatar answered Sep 21 '22 12:09

CommonsWare