Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i center a view in a guideline?

Suppose i have a horizontal GuideLine at 30% of parent and a View (suppose a Button) how can i make that view be centered on the guideline?

Like this:

enter image description here

Update:

The view i'm using here has height based on ratio, and soltuion on answers doesnt works.

Here is current layout:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/imgLogo"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="@id/guideline_30"
        app:layout_constraintDimensionRatio="H,1:1"
        app:layout_constraintEnd_toStartOf="@+id/guidelineV_75"
        app:layout_constraintStart_toEndOf="@+id/guidelineV_25"
        app:layout_constraintTop_toTopOf="@+id/guideline_30"
        app:srcCompat="@drawable/ic_launcher_background" />

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

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

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

</android.support.constraint.ConstraintLayout>
like image 310
Victor Aurélio Avatar asked May 09 '18 02:05

Victor Aurélio


People also ask

How do I use guideline in ConstraintLayout?

The following steps can be used to make use of guidelines: Step 1: Use constraint layout in your application. Step 2: Click on the icon shown below or you can also search horizontal or vertical guidelines in palette. Step 3: Select guidelines which you want to use (horizontal or vertical).

What is ConstraintLayout?

A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. Note: ConstraintLayout is available as a support library that you can use on Android systems starting with API level 9 (Gingerbread). As such, we are planning on enriching its API and capabilities over time.

How can we use barrier in constraint layout?

To define a Barrier , you can select one or more View components from the “Design” view, open the “Guidelines” menu and select the Barrier . If you want to add it directly in the XML, you can use the following code snippet: The resulting layout looks like the screenshot of the “Design” layout editor view from below.


1 Answers

Using fix width or height

Need to use same Guideline for both top and bottom constraint like:

app:layout_constraintTop_toTopOf="@id/guideline_30"
app:layout_constraintBottom_toBottomOf="@id/guideline_30"

xml:

<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">

    <ImageView
        android:id="@+id/imgLogo"
        android:layout_width="150dp"
        android:layout_height="150dp"
        app:layout_constraintTop_toTopOf="@id/guideline_30"
        app:layout_constraintBottom_toBottomOf="@id/guideline_30"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:src="@drawable/splash_logo" />

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

</android.support.constraint.ConstraintLayout>

ouuput:

enter image description here

Using width and height based on ratio

  • As you are using both width and height based on ratio so that it's not centered.
  • But to get your desired result you can also set Guideline = 0.15 (0.3 / 2) and modify constraintTop and constraintBotttom

xml:

<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">

    <ImageView
    android:id="@+id/imgLogo"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintDimensionRatio="H,1:1"
    app:layout_constraintEnd_toStartOf="@+id/guidelineV_75"
    app:layout_constraintStart_toEndOf="@+id/guidelineV_25"
    app:layout_constraintTop_toTopOf="@+id/guideline_15"
    app:srcCompat="@drawable/ic_launcher_background" />

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

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

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

</android.support.constraint.ConstraintLayout>

ouput:

enter image description here

like image 84
Nikunj Avatar answered Oct 25 '22 21:10

Nikunj