Tord Larsen
Tord Larsen

Reputation: 2836

constraintlayout views jump to the right after migrate to androidX

In this constraintlayout I cant get the TextView to align left.

I have this strange thing that whatever I do the TextView jumps to the right.

Here´s a picture: enter image description here

I just migrated to androidX maybe that has to to with this dunno..

Here´s the xml layout place advice

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="wrap_content">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/pref_category_remote_battery_title"
        android:textAppearance="?android:attr/textAppearanceListItem"
        android:textColor="@color/default_400"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="8dp"
        android:text="@string/pref_category_remote_battery_switch_summ"

        android:textAppearance="?android:attr/textAppearanceSmall"
        app:layout_constraintStart_toStartOf="parent"
        tools:layout_editor_absoluteY="30dp" />

    <androidx.appcompat.widget.SwitchCompat
        android:id="@+id/switch_compat"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="24dp"
        android:layout_marginBottom="8dp"
        android:checked="false"
        android:textOff="OFF"
        android:textOn="ON"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline3"
        app:layout_constraintTop_toTopOf="parent"
        app:showText="true" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline3"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.74722224" />

</androidx.constraintlayout.widget.ConstraintLayout>

Here the build.gradle dependencies :

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    implementation 'androidx.preference:preference:1.0.0'
    implementation 'androidx.legacy:legacy-preference-v14:1.0.0'
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core:1.0.2'
    implementation 'androidx.multidex:multidex:2.0.1'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
    annotationProcessor 'androidx.lifecycle:lifecycle-compiler:2.0.0'

    implementation 'com.google.android.gms:play-services-auth:16.0.1'
    implementation 'com.google.firebase:firebase-core:16.0.9'
    implementation 'com.google.firebase:firebase-auth:17.0.0'
    implementation 'com.google.firebase:firebase-firestore:19.0.0'

    implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1'
    implementation 'com.firebaseui:firebase-ui-auth:5.0.0'

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'org.greenrobot:eventbus:3.1.1' } apply plugin: 'com.google.gms.google-services' apply plugin: 'kotlin-android-extensions' repositories {
    mavenCentral() }

Upvotes: 1

Views: 189

Answers (2)

Md Sufi Khan
Md Sufi Khan

Reputation: 1761

Why don't you try like below for your (TextView 2)?

<TextView
    android:id="@+id/textView2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="8dp"
    android:text="@string/pref_category_remote_battery_switch_summ"
    android:textAppearance="?android:attr/textAppearanceSmall"
    app:layout_constraintStart_toStartOf="parent"
    tools:layout_editor_absoluteY="30dp"

    app:layout_constraintTop_toBottomOf="@id/textView"
    app:layout_constraintEnd_toStartOf="@id/switch_compat"/>

Upvotes: 0

Tamir Abutbul
Tamir Abutbul

Reputation: 7661

If you want it to stay in the left side you can add android:layoutDirection="ltr" to your layout like this:

<?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"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layoutDirection="ltr">

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="pref_category_remote_battery_title"
    android:textAppearance="?android:attr/textAppearanceListItem"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView2" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingBottom="8dp"
    android:text="pref_category_remote_battery_switch_summ"
    android:textAppearance="?android:attr/textAppearanceSmall"
    app:layout_constraintEnd_toStartOf="@+id/guideline3"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/switch_compat"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="24dp"
    android:layout_marginBottom="8dp"
    android:checked="false"
    android:textOff="OFF"
    android:textOn="ON"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="@+id/guideline3"
    app:layout_constraintTop_toTopOf="parent"
    app:showText="true" />

 <android.support.constraint.Guideline
    android:id="@+id/guideline3"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:orientation="vertical"
    app:layout_constraintGuide_percent="0.74722224" /> 

</android.support.constraint.ConstraintLayout>

So it will stay to the left and look like this:

enter image description here

Upvotes: 1

Related Questions