I am making a game that displays animated characters on the screen
Now I am at the stage where i want it to display correctly on other devices as well
increase the screen resolution on my own device and have it still display correctly
for this I want to get the scale factor so I can change all my pixel values to DP values
however the display metrics does not change no matter what I change the screen resolution to
for example
this.getHolder().setFixedSize(1024, 768);
scale = getResources().getDisplayMetrics().density;
DisplayMetrics dm = new DisplayMetrics();
((Activity) getContext()).getWindowManager().getDefaultDisplay()
.getMetrics(dm);
float xDpi = dm.xdpi;
float yDpi = dm.ydpi;
Now no matter what I change 'setfixedsize ' to, both xdpi and scale stay the same even though it does show the increased screen resolution on the display
You can get display metrics using below line.
DisplayMetrics metrics = getResources().getDisplayMetrics();
This is my code for display wallpaper may be its help full=====>
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Point size = new Point();
display.getSize(size);
float w = size.x;
float h = size.y;
width = ((w)/1000);
height = ((h)/1000);
Log.d("width===>", String.valueOf(width));
Log.d("height===>", String.valueOf(height));
(OR)
metrics = Resources.getSystem().getDisplayMetrics();
float w=metrics.widthPixels;
float h=metrics.heightPixels;
width = ((w)/1000);
height = ((h)/1000);
if(width == 0.72f && height == 1.28f){
width=1.08f;
height=1.776f;
}
Log.d("width===>", String.valueOf(width));
Log.d("height===>", String.valueOf(height));
Related
So I want to make an app compatible with multiple screen sizes, however the positioning of some buttons is done programmatically and it has to align with the background (timetable app, task aligned with proper day, start time and end time on a grid display). Hence, I need to get the screen dimensions.
Currently I'm testing this in Nokia 8.1 with height 2246px and width 1080px, however what I need is the actual usable height not the total height (minus the bar at the top and the bottom). After manually testing the usable height it seems to be 1887px, but I have not found a way to retrieve this number.
I tried these codes so far:
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics);
int displayHeight = displayMetrics.heightPixels; //2246px
int displayWidth = displayMetrics.widthPixels; //1080px
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int displayHeight = displayMetrics.heightPixels; //2034px
int displayWidth = displayMetrics.widthPixels; //1080px
WindowManager windowManager = (WindowManager) mainActivity.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int height = size.y; //2034px
int width = size.x; //1080px
I thought maybe navigation bar is the problem
Resources resources = mainContext.getResources();
int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
int navigation_bar_height = resources.getDimensionPixelSize(resourceId); //126px
2034 - 126 = 1908px != 1887px (which is close but still to big, it goes off screen)
Furthermore, when I tried to make a new hardware profile with the phone dimensions (2246 x 1080), using 1887px goes off screen on the xml Design display, even though it shows up exactly right on my phone...
So yeah I'm lost, no idea how to get the real size or why the profile doesn't match. Please help.
i need to set image height as activity height of the device programatically
So i need to retrieve activity size of the device, this size varies from device to device
i tried this
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
but this gives the entire size(including navigationbar and everthing) of the phone screen but i need the activity size
use a method like this:
public static Point getAppUsableScreenSize(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
return size;
}
to get the usable screen size
If you want visible activity height excluding toolbar. Calculate tool/action bar height and set your image view layout_height as
required height = activity height-toolbar height
I'm trying to make an Android snake game, and I want to have a grid that stretches in a square the width of the screen (Portrait).
I an using a drawable storing my png, and when i add the line grid.setBounds(0,0,screenWidth,screenWidth) my phone displays a white screen at first, though it works as expected once I minimize and reopen my application.
The app also works as expected if I put standard numbers in as parameters. This is on my nexus 6P on android 7. When I emulate it on an older device it works, but emulated on a newer device, I see the same issue. I get the screen width as follows:
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
scale = screenWidth/1024.0;
If anyone could help that would be much appreciated.
Try to get the screen size in onWindowFocusChnaged method of activity.
#Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
scale = screenWidth/1024.0;
}
In my android camera app, Im trying to make it so that the camera layout view fills the width of the screen and then is proportional (to a square) for the height of the layout. But its cropping it too small for some reason:
example of what is happening..
The green arrow represents where it should go to.
Ive tried fill_parent, etc. So, how can I make it go to the end of the screen?
I believe this is the problem. I know I did this because I was not sure how to make it automatically stretch to the right proportion.
int width = 352;
int height = 288;
camPreview = new CameraSurfaceView(width, height);
myCamHolder.addCallback(camPreview);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = width*288/352;
Maybe you don't need 288/352, but rather preview size.height/preview size.width
I have a view which is bigger than the screen.
How can I get the width of its currently visible area?
getWidth() returns the absolut size.
You can get the screen width using the following code:
If you're calling this outside of an Activity, you'll need to pass the context in (or get it through some other call). Then use that to get your display metrics:
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
Using services you might do:
Display display= ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();