Search code examples
androidrandomscreen-resolutionscreen-sizeandroid-screen-support

Getting Screen width and height in Android


I am using this code to get my screen width and height and send my button to a random position on the screen but sometimes my button is completely off-screen. I don't believe that the resolution my code gets me is the true res.

DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
float height = displayMetrics.heightPixels;
float width = displayMetrics.widthPixels;

float maxX = width;
float maxY = height;
float minX = width/10;
float minY = height/15;

Random rand = new Random();

float finalX = rand.nextFloat() * abs(maxX - minX);
float finalY = rand.nextFloat() * abs(maxY - minY);

_height.setText("Current Y: " + String.valueOf(finalY));
_width.setText("Current X: " + String.valueOf(finalX));
max_height.setText("Max height: " + String.valueOf(height));
max_width.setText("Max width: " + String.valueOf(width));
btnClick.setX(finalX);
btnClick.setY(finalY);
btnClick.setVisibility(View.VISIBLE);

Here is the XML

<?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:id="@+id/cLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
app:layout_constraintRight_toRightOf="@+id/width"
tools:context="com.jamblatech.clicker.MainActivity"
tools:layout_editor_absoluteY="81dp">

<Button
    android:id="@+id/buttonEnd"
    android:layout_width="363dp"
    android:layout_height="572dp"
    android:background="@android:color/transparent"
    android:onClick=""
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0" />

<TextView
    android:id="@+id/textViewCount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/zero"
    android:textColor="@color/colorAccent"
    android:textSize="36sp"
    android:visibility="invisible"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.03" />

<TextView
    android:id="@+id/timerView"
    android:layout_width="185dp"
    android:layout_height="41dp"
    android:text="@string/zero"
    android:textAlignment="center"
    android:textSize="24sp"
    android:visibility="invisible"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.502"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.937" />

<Button
    android:id="@+id/buttonClick"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_marginTop="200dp"
    android:background="@drawable/clickbutton"
    android:text="@string/Click"
    android:textSize="24sp"
    android:visibility="invisible"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textViewCount" />

<Button
    android:id="@+id/buttonReplay"
    android:layout_width="221dp"
    android:layout_height="103dp"
    android:text="@string/Replay"
    android:visibility="invisible"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.503"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/buttonBack"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Back"
    android:visibility="invisible"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.674" />

<TextView
    android:id="@+id/highScore"
    android:layout_width="261dp"
    android:layout_height="107dp"
    android:textAlignment="center"
    android:textColor="@android:color/holo_purple"
    android:textSize="30sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.149" />

<TextView
    android:id="@+id/height"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Test"
    android:visibility="invisible"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.029" />

<TextView
    android:id="@+id/width"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Test"
    android:visibility="invisible"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0" />

<TextView
    android:id="@+id/maxheight"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Test"
    android:visibility="invisible"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.06" />

<TextView
    android:id="@+id/maxwidth"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Test"
    android:visibility="invisible"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.09" />

<TextView
    android:id="@+id/testTExt"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Test"
    android:visibility="invisible"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0" />

<TextView
    android:id="@+id/startTimerText"
    android:layout_width="134dp"
    android:layout_height="95dp"
    android:text="@string/Test_number"
    android:textAlignment="center"
    android:textColor="@android:color/holo_green_dark"
    android:textSize="60sp"
    android:visibility="visible"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.54" />
    </android.support.constraint.ConstraintLayout>

Here are 2 pictures displaying my problem:

  1. Button slightly on-screen:
    Button slightly on-screen
  2. Button completely off-screen: Button completely off-screen

Solution

  • What you can do is: you already have randomly generated x and y, x + button.getWidth() to it and check if it is less than screen width if yes your x is well in bound if no regerenate your random x and y, similary if x is in bound do it for y and check y + button.getHeight() is less than screen height it will do.