-1

I am trying to bring an older app back to life. I used a library to position locations of stores on a floor plan, but it seems deprecated.

My attempt with ConstraintLayout: I positioned all items in the editor and pushed the "Infer Constraints" button, as suggested somewhere on StackOverflow. It worked great... until I ran my app on my device, and noticed all poi's all over the place.

There is a deprecated view which supports layout_marginLeftPercent etc, but I can't seem to find the ConstraintLayout alternative? Any suggestions?

TomCB
  • 3,983
  • 9
  • 40
  • 66
  • So you want to positions views with a percentage of the width and height of the layout? – Michiel Nov 13 '19 at 11:01
  • It seems that you are working with the deprecated PercentRelativeLayout. So either you can use LinearLayout together with layout_weight or just use ConstraintLayout. Here is an article how to migrate a PercentRelativeLayout to a ConstraintLayout: https://developer.android.com/reference/android/support/percent/PercentRelativeLayout – Mark P. Nov 13 '19 at 11:19
  • I think the reason why your layout is messed up because the `Infer Constraints` button didn't arrange your layout in the correct way. My suggestion, don't use `Infer Constraints` button. You must define `layout_constrainttop_totopof="@+id/anotherId"` and many of other field. Your XML code gonna be so long in the end, but that's the way of constraint layout. – Giovanka Bisano Nov 13 '19 at 11:42
  • Thank you for your suggestions. I have to place lots of dots (ImageButtons of about 25dp w/h) on an image. It's important that for instance dot one is placed 24% from the top and 14% from the left. The width percentage etc. is of no use for this case. It looks like there is no out-of-the-box solution in ConstraintLayout now to do this properly. – TomCB Nov 13 '19 at 12:04

1 Answers1

0

This can be accomplished with ConstraintLayout using Guidelines. So if you want a dot to be placed 24% from the top of the screen and 14% from the left, your code might look something like this:

<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.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.14" />

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

    <ImageView
        android:id="@+id/dot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toTopOf="@+id/guideline2"
        app:layout_constraintEnd_toStartOf="@+id/guideline"
        app:layout_constraintStart_toStartOf="@+id/guideline"
        app:layout_constraintTop_toTopOf="@+id/guideline2"
        app:srcCompat="@android:color/background_dark" />
</android.support.constraint.ConstraintLayout>

More documentation and information about this can be found here

kjanderson2
  • 1,209
  • 12
  • 23