so I have written the following method in my activity:
private void setDisplayMetrics(){
DisplayMetrics metrics = this.getResources().getDisplayMetrics();
int dh = metrics.heightPixels;
int dw = metrics.widthPixels;
if(dw < dh){
deviceWidth = dw;
deviceHeight = dh;
}else{
deviceWidth = dh;
deviceHeight = dw;
}
System.err.println("--------------> dh : "+deviceHeight+" | dw "+deviceWidth);
}
And it works great, in the sense that it gets me the total width and height of the screen with great accuracy and reliability (which is what I have asked it to do).
Here is the problem. On older android devices the screen dimensions are the same as the dimensions the application can take up, and the script above helps me to set the size of elements in the app. BUT with android ICS I have this graphic button bar on the bottom of the screen, and it messes up my whole strategy.
What I would really like is the ability to get the available app dimensions for the portrait view as well as the landscape view at the same time in one method. And have these dimensions be accurate regardless of the presence of the bar pictured above.
Does anyone know how to achieve this?
Rect rectgle= new Rect();
Window window= getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int StatusBarHeight= rectgle.top;
int contentViewTop=
window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int TitleBarHeight= contentViewTop - StatusBarHeight;
Log.i("*** Jorgesys :: ", "StatusBar Height= " + StatusBarHeight + " , TitleBar Height = " + TitleBarHeight);
From what I've read so far you can't get this height directly. The approach via getWindowVisibleDisplayFrame() does not seem to work.
The only promising solution I have found so far is using a custom layout to measure the screen size as explained in
http://evgeni-shafran.blogspot.de/2011/01/android-screen-size-problem.html.
I am convinced that this will work but to me it seems to be more trouble than it is worth.
Related
I just began to develop a app with java, and I only got some experience in C. In my code in Activity.java (in android studio) I got things like, just to give some examples:
meteorite1.setX(meteoritePlacementX(meteorite1.getX()));
meteorite1.setY(-2000);
gnome.setX(330);
gnome.setY(800);
meteorite2.setX(meteoritePlacementX(meteorite2.getX()));
meteorite2.setY(meteoritePlacementY(meteorite1.getY()));
meteorite3.setX(meteoritePlacementX(meteorite3.getX()));
meteorite3.setY(meteoritePlacementY(meteorite2.getY()));
meteorite4.setX(meteoritePlacementX(meteorite4.getX()));
meteorite4.setY(meteoritePlacementY(meteorite3.getY()));
meteorite5.setX(meteoritePlacementX(meteorite5.getX()));
meteorite5.setY(meteoritePlacementY(meteorite4.getY()));
meteoritedestruction1.setX(0);
meteoritedestruction1.setY(-2000);
meteoritedestruction2.setX(0);
meteoritedestruction2.setY(-2000);
meteoritedestruction3.setX(0);
meteoritedestruction3.setY(-2000);
meteoritedestruction4.setX(0);
meteoritedestruction4.setY(-2000);
meteoritedestruction5.setX(0);
meteoritedestruction5.setY(-2000);
star1.setX(300);
star2.setX(150);
star3.setX(50);
star4.setX(500);
star5.setX(600);
star6.setX(350);
star7.setX(80);
star8.setX(450);
tinystar1.setX(302);
tinystar2.setX(240);
tinystar3.setX(57);
tinystar4.setX(660);
tinystar5.setX(400);
star1.setY(300);
star2.setY(-300);
star3.setY(-100);
star4.setY(100);
star5.setY(300);
star6.setY(500);
star7.setY(700);
star8.setY(900);
tinystar1.setY(300);
tinystar2.setY(-400);
tinystar3.setY(-200);
tinystar4.setY(150);
tinystar5.setY(30);
and
public float meteoritePlacementX(float X){
float MeteoriteNewX = 0f;
int random = (int )(Math.random() * 480 - 50);
MeteoriteNewX = random;
return MeteoriteNewX;
}
Which workes fine, but just on my phone (720 x 1280 pixels (~294 ppi pixel density)) which I tested my code at. Now I published my app, but on other device, the layout of the app is totally out of sync (which makes sense to me now, cause x and y are different for every screen). Buttons and pictures workes fine, but moving object like
meteorite1.setY(meteorite1.getY() + 20);
where I use x and y are broken on other devices. I use the relative layout.
So long story short; Is there a way to change x and y, so it becomes relative to the screen? Otherwise I need to change the whole code.
In general using placement based on hard coded pixel values is not a good practice. Not only would this break with backwards compatibility but also think about what you would have to do when 2k+ phones come out, you would need an entire refactor. Look at this question and the answer by Guillaume Perrot you can get get the maximum and minimum pixel values relative to the user's phone and use those instead of the 480 - 50 and your star set functions.
For the movement do
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager wm = (WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE); // the results will be higher than using the activity context object or the getWindowManager() shortcut
wm.getDefaultDisplay().getMetrics(displayMetrics);
int maxWidth = displayMetrics.widthPixels;
//Make this percentage whatever you want
float movementPercentage = 0.02
//Will move the object 2 percent up the y axis
meteorite1.setY(meteorite1.getY() + maxWidth*movementPercentage);
I want to get the actual screen height of the device that runs my app. To achieve this i try the following:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int Height = metrics.heightPixels;
TextView HeightView = (TextView) findViewById(R.id.screenHeight);
HeightView.setText("Screen Height: " + Height);
int Width = metrics.widthPixels;
TextView WidthView = (TextView) findViewById(R.id.screenWidth);
WidthView.setText("Screen Width: " + Width);
The device that I run using the emulator has a screen width of 1080 pixels and a screen height of 1920 pixels. The width is displayed correctly (1080 pixels) but the height is according to the app only 1776 pixels. What do I need to make my app display the correct screen height? Is metrics.heightPixels a bad way of getting the screen height?
see this answer
if your API level > 13 try this if you are in activity
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
If you're not in an Activity you can get the default Display via WINDOW_SERVICE:
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
getWidth and getHeight is for API level 13 or less
UPDATE
For API 17 and higher method Display.getRealSize() returns full size of screen, as mentioned in documentation:
Gets the real size of the display
without subtracting any window decor or applying any compatibility
scale factors.
The size is adjusted based on the current rotation of the display.
The real size may be smaller than the physical size of the screen when
the window manager is emulating a smaller display (using adb shell am
display-size).
int width=Resources.getSystem().getDisplayMetrics().widthPixels;
int height=Resources.getSystem().getDisplayMetrics().heightPixels;
I needed to get the actual available height, and out of all the options this is the only thing that worked for me.
Create a rectangle, and get its dimensions:
Rect r = new Rect();
activityRootView.getWindowVisibleDisplayFrame(r);
int screenHeight = r.bottom - r.top;
where activityRootView is the root view of your layout.
If you need to extract the toolbar height:
int availableScreenHeight = screenHeight - toolbar.getHeight();
where toolbar is your toolbar view.
availableScreenHeight will now be without the statusbar, without the toolbar, and without the navigation bar (if the device is using it).
you can use this code:
val display = windowManager.defaultDisplay
val size = Point()
display.getSize(size)
val width: Int = size.x
val height: Int = size.y
You can try this:
WindowManager wm = getWindowManager();
Display d = wm.getDefaultDisplay();
Point size = new Point();
d.getSize(size);
Log.i("LOG", "W=" + size.x + " , H=" + size.y);
I'm developing an android app which requires an imageview (and textviews) to be updated on button click, but I want the image to match the height of four of my textviews. When I set the height of the imageview, it seems to keep the height of the previous update, not the current one. Here is my code to update the image and its height.
int nameHeight = t_name.getHeight();
int timeHeight = r_time.getHeight();
int dateHeight = r_date.getHeight();
int locaHeight = r_loca.getHeight();
int totalHeight = nameHeight + timeHeight + dateHeight + locaHeight;
i_img.getLayoutParams().height = totalHeight;
Bitmap image = getBitmapFromURL(event_image.get(eventToUse));
i_img.setImageBitmap(image);
After touching the layout params, call requestLayout() to have the changes take effect.
I have a webView that I manipulate with this code: (this all works fine)
WebView myWebView = (WebView) findViewById(R.id.stats_webview);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.setPadding(0,0,0,0);
myWebView.setInitialScale(getScale());
Which gets the initial scale from this method
private int getScale(){
Display display = ((WindowManager)
getSystemService(StatsActivity.WINDOW_SERVICE)).getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
Double val = new Double(width)/new Double(720);
val = val * 100d;
return val.intValue();
}
That all works fine and loads the view slightly scaled down to fit the screen from my 720pixel web source. The problem is that once someone zooms in, they cannot zoom back out all the way to the initial scale. They can only zoom back out to a scale of 1, so they are unable to fit the entire page back on the screen. I've found references to a setMinimumScale method but it doesn't seem to be available any more. Am I just missing an include file or is there a new way to accomplish this?
I have tried:
myWebView.setMinimumScale(getScale());
and
myWebView.getSettings().setMinimumScale(getScale());
but neither works.
If editing the HTML file is an option, you can use the viewport meta tag.
check the documentation for more details.
i have used
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
cube.height=metrics.heightPixels;
cube.width=metrics.widthPixels;
to get height and width of my display but the resolution is low for my sony xperia p device which is qhd display. from this function i can only get height=569 px and width=320 px
i don't understand why this is showing lower. i think this is giving me virtual resolution than real one. i used simple view and ondraw method to draw the canvas.
please help.
Maybe you should try something like this:
Display display = getWindowManager().getDefaultDisplay();
Method mGetRawH = Display.class.getMethod("getRawHeight");
Method mGetRawW = Display.class.getMethod("getRawWidth");
int rawWidth = (Integer) mGetRawW.invoke(display);
int rawHeight = (Integer) mGetRawH.invoke(display);
As quoted from a website on this question.