There's a work that I personally made, and I want to place it on the top right of the program. Like n pixels from the top border, m pixels from the right border. In conclusion, you need to find the horizontal and vertical size of the screen and place the work in the desired position.
int px = screenWidth - m;
int py = screenHeight - n;
How do I find the size of screenWide and screenHeight in MainActivity?
android layout screen
If you want to get the size of the screen in pixels, you can use the getSize method.
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
If the situation is not within the activity, you can get the display (information about the screen) using WINDOW_SERVICE as shown below.
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Before the getSize method came out (API level 13), the getWidth and getHeight methods were used (not used now).
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
However, in the case of the above situation, it seems better to use the margin or padding of the layout.
Display Metrics is also available. Display Metrics provides a function to express general information about the screen (size, display density, font size, etc.) To access the components of Display Metrics, please initialize the object as follows.
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
I want to get the horizontal size of the screen in pixels. You can use the widthPixels method.
Log.d("ApplicationTagName", "Display width in px is " + metrics.widthPixels);
© 2024 OneMinuteCode. All rights reserved.