0

I have a LinearLayout in my layout as show in the below diagram with blue background. How can I get the corner coordinate positions?Or How can I set them to specific positions?.For example I want the top-left corner to be at (50,50),top-right corner to be at (300,50),bottom left corner to be (50,200) and the bottom right to be at (300,50) as shown in the figure below.The position here are in px not in dp. My question somehow resemble this question How do android screen coordinates work?

enter image description here

The following is the xml file of the layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".TestActivity">

    <LinearLayout
        android:layout_width="300px"
        android:layout_height="150px"
        android:background="@color/colorAccent"
        android:orientation="vertical"
        android:layout_centerInParent="true"
        android:id="@+id/linearLayout"
        >
    </LinearLayout>

</RelativeLayout>

Is there any way to do this in android studio.I have searched a lot and went through the android developer documentation but did not understand anything. I will be very grateful for your kind feedback on this.

Thanks!

Asif Ali Khan
  • 49
  • 1
  • 12

2 Answers2

1

Think this differently, you can set a layout direct at any x or y. So if you load want to load your linear layout at given x and y from all your coordinates, find diagonal x and y. For example in your case xmid would be 50 + 300 / 2 and ymid will be the same. Then use this:

 val fromXDelta = xmid - linearLayouWidth/2
 val fromYDelta = ymid - linearLayoutHeight/2

 linearLayout.x = fromXDelta
 linearLayout.y = fromYDelta
Ashish Sharma
  • 566
  • 5
  • 20
1

It's just a case of setting a top and left margin of 50, and otherwise making all dimensions 300 or whatever else you want. I'm assuming the bottom corner should read 300,200? So the overall dimens are 300x150?

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:layout_width="300dp"
        android:layout_height="150dp"
        android:layout_marginTop="50dp"
        android:layout_marginLeft="50dp"/>

</FrameLayout>

Edit: The only layout I can think of that will allow you define all children with absolute values in XML is the AbsoluteLayout class.

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">

   <View
      android:layout_width="300dp"
      android:layout_height="150dp"
      android:layout_x="50dp"
      android:layout_y="50dp" />

</AbsoluteLayout>

However, you'll notice it has been deprecated for over a decade. Android doesn't encourage you to define layouts in absolute terms because they scale poorly. Instead, you should use another layout (probably ConstraintLayout) and define all your UI elements relative to one another.

You can also define everything in absolute values if you do it yourself in Java, by defining a custom view. That will involve a lot of work however,

PPartisan
  • 8,173
  • 4
  • 29
  • 48