Home > front end >  Android studio (0,0) is top right in device
Android studio (0,0) is top right in device

Time:09-22

After trying to position views programmatically, I noticed the views in my device (as well as other devices with this app) maps (0,0) to top right corner instead of left.

In a blank project (0,0) is the top left, so I included simplified version of game_screen.xml to show the problem (including gameLayout caused the problem in my real app), but even after I removed it the problem remains.

API 25

activity_main.xml:

<?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="match_parent"
    tools:context=".MainActivity">


    <RelativeLayout
        android:id="@ id/lay"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@ id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!" />
    </RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

activityMain.java:

package com.benmassarano.mirroredscreendemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.gridlayout.widget.GridLayout;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RelativeLayout lay = findViewById(R.id.lay);
        TextView text = findViewById(R.id.text);
        positionView(lay, text, RelativeLayout.LayoutParams.WRAP_CONTENT,
                     RelativeLayout.LayoutParams.WRAP_CONTENT);

    }

    private void positionView(RelativeLayout parent, View view, int widthSetting,
                              int heightSetting) {
        parent.post(new Runnable() {
            @Override
            public void run() {
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(widthSetting,
                                                                                     heightSetting);
                params.setMargins(0, 0, 0, 0);
                view.setLayoutParams(params);
            }
        });
    }
}

game_screen.xml:

<?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="match_parent">

    <RelativeLayout
        android:id="@ id/gameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@ id/timer"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="30s"
            android:textSize="28sp" />

    </RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

emulator screenshot:

emulator

personal device screenshot:

device

CodePudding user response:

From your provided screenshot, it is clear that your device configuration is set to RTL(Right to Left) layout.

If you want your app to follow layout direction set by the device, then make sure to put android:supportsRtl="true" in your manifest.xml: details

<application android:supportsRtl="true">
    ...
</application>

But if you want your app not to support RTL (or device layout direction), put android:supportsRtl="false" in your manifest.xml; or just remove this attribute since it is false by default.

  • Related