Obtain screen size and detect its direction in a variety of Android system
6 minute(s) read | Published on: May 01, 2021 Updated on: Dec 14, 2021 |
They have different screens and sizes. To be. In other words, even phones with different operating systems are different in screen size. Therefore, when designing applications, we must also consider the size of the screens and design applications that fit them easily and without problems. If we do not pay attention to its of the devices and design an application, the application may not be displayed properly or may cause some problems. In this tutorial, we want to discuss this and design applications suitable for screen sizes. It is recommended that you follow this tutorial.
Get the screen-size
Some designers, programmers, and application developers need to get the screen size in pixels, or we may even need to get the DPI pixel density, which stands for Dots per inch.
To do this, we create an Android project. In this tutorial, we choose its name, Screen Size.
Select the activity in which we will operate the Empty Activity type. The language used to complete and complete this tutorial in Java.
Two classes, Display and Display Metrics, are used to obtain the screen size. In addition to using the Display Metrics class to find the size of the screens, we can also use this DPI class to find the page.
1- Create two Text Views in the layout.
2- Add the code below in activity_main.xml.
Obtain screen-size using the Display class
1- First, we need to create an example of this class, the Display class.
2- Using the codes inserted below, we can create a sample of the Display class.
By entering the above code, all screen information such as page name and size is stored in displaySize.
3- Then, we have to make an example of the Point class to use this class to get the coordinates of the length and width of the screen from displaySize.
4- The codes inserted below take the information and details related to the coordinates of the length and width of the screen.
The information of screen length and width coordinates is received by entering the above code and using the getSize () method.
5- After receiving the coordinates of the length and width of the screen, we convert them to the value of int.
6- How to convert page length and width coordinates to int type is as follows.
In the code above, size.x is used to find the width and size. y is used to find the length size.
7- Display the coordinates in TextView.
8- The complete activity code that should be included in the MainActivity.java file is as follows:
9- We will implement the project.
10- After execution, numbers will be displayed on the screen. The first number indicates the size of the page width, and the second number indicates the size of the page length. For example, we can say that the number 1080 is the screen's width, and the number 2034 is the length of the screen.
Obtain size using the DisplayMetrics class
In the previous section, we obtained this using the Display class. This section wants to obtain the screen size using the DisplayMetrics class.
1- I will add the codes listed below to the activity section.
In this part of the tutorial, like the previous part, using the DisplayMetrics class, we get it in pixels.
metricSize.widthPixels has a value that indicates the screen's width, and metricSize.heightPixels also has a value that indicates the length of the screen. metricSize.densityDpi also has a value of DPI and returns it.
Using metricSize.xdpi and metricSize.ydpi, screen length, and width coordinates are obtained.
2- We will execute the project.
3- The output of this class will be the same as the output of the previous class, except that this class also returns the DPI value.
4- The complete activity codes that should be included in MainActivity.java are as follows:
Screen direction
To determine the screen's orientation, we need to create a project called Orientation, and the type of activity we choose should be Empty Activity. The language used to do this part of the tutorial is Java.
1- To do this, first create two TextViews in the layout.
2- The codes that should be in the activity_main.xml section are as follows:
Detect its Orientation using the Configuration class
1- The codes that should be included in this class to detect the screen orientation in the MainActivity.java section are as follows:
2- We define a variable called ScreenOrientation and type int that detects the current Orientation of the page and stores it in itself.
3- Defining the variable and recognizing the page's orientation by adding the codes inserted below.
Note that the values stored in the variable defined above are 1 and 2, with the number 1 indicating the direction and position of the portrait and the number 2 indicating the direction of the Landscape.
Then in the codes listed above, a condition is defined if the variable's value is equal to the value of Configuration.ORIENTATION_LANDSCAPE, or in other words, is the number 2, the first part of the condition is checked and done; otherwise, the second part of the condition is done.
4- We will execute the project.
5- The value that will show in the output depends on the direction and position of the screen. If it is in portrait mode, the value of portrait will be displayed, and if it is in landscape mode, the value of Landscape will be displayed.
Detect this Orientation using another method
In this method, I use a resource to determine the direction of the screen.
1- Right-click on the project res folder and select Android Resource File.
2- In the file name field, enter the layouts.
3- In the Directory name field, enter values-land.
4- Create the ayouts.xml file.
5- Put the code below in the values-land / layouts.xml file.
6- In this file, we define and create a bool property called is_landscape and set its value to true.
7- We create another file again in the same way and like this, but I create and save it in the values-port directory.
8- The code below can be located in the values-port / layouts.xml file.
9- In this file, we defined a property called is_landscape, but this time we set its value to false.
10- In activity, we define a variable of type boolean.
11- We define a condition for this part as in the previous part:
12- I will implement the project.
13- will show the output according to the vertical or horizontal direction of the screen.