My code to get the screen height dynamically:
Display display = ((Activity) getContext()).getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Error:
Error: java.lang.ClassCastException: android.app.Application
Complete log cat: http://pastebin.com/8zUNFUYn
Edit:
Changed code to this:
Display display = (activity).getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Worked but, gave a new error that is display.getSize(size) is not a method. Is that API depreceated?
Context
is base class for both Application and Activity. Since your a getting an Application with getContext(), casting it to Activity causes the ClassCastExecption.
replace your line with this :
if you are using this code inside activity then use this:
Display display = this.getContext().getWindowManager().getDefaultDisplay();
Related
I am using this code and the view does not fit the whole screen of the device.
DisplayMetrics metricsLandscape = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metricsLandscape);
relVideoHolder.setLayoutParams(new RelativeLayout.LayoutParams(metricsLandscape.widthPixels, metricsLandscape.heightPixels));
videoView.setLayoutParams(new RelativeLayout.LayoutParams(metricsLandscape.widthPixels, metricsLandscape.heightPixels));
Log.e("DIMENSIONS", "WIDTH by HEIGHT"+ metricsLandscape.widthPixels+"by"+metricsLandscape.heightPixels);
Log.e("VIDEOHOLDER dimension WxH", ""+relVideoHolder.getWidth()+"x"+relVideoHolder.getHeight());
Log.e("VIDEOVIEW dimension WxH", ""+videoView.getWidth()+"x"+videoView.getHeight());
I find this weird because the result of the Logcat is here;
02-14 17:17:25.881: E/DIMENSIONS(6314): WIDTH by HEIGHT 1196by720
02-14 17:17:25.881: E/VIDEOHOLDER dimension WxH(6314): 720x410
02-14 17:17:25.881: E/VIDEOVIEW dimension WxH(6314): 720x403
I wonder why they have different values. Sometimes, the view covers all the screen, but most of the time, it does not fit the full screen.
Please help me.
By the way, I call these codes onConfigurationChanged when Landscape orientation. Thank you.
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();
Before getSize was introduced (in API level 13), you could use the getWidth and getHeight methods that are now deprecated:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
I just add getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); to my code and it works like a charm.
This is not my activity! this is separate class file
i am using Surface view extended to my class file . i wanted to display a small bit map to my canvas my bit map is very small than my phone screen so i used a createscaledbitmap to fit it in my phone screen with this code here
Bitmap o2 = Bitmap.createScaledBitmap( hud, 800, 490, false);
it worked great in my phone but when i tested in a bigger phone i have my bitmap pasted only 3/4 of the screen .. so i want to make my bitmap to fix the phone screen size
Bitmap o2 = Bitmap.createScaledBitmap( hud, phonescreenwidth, phonescreenheight, false);
is there a function to get the phone display size and use in my code ?
like
int phonescreenwidth = surfaceView.getwidth ();
and there is a function called protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
i dont know how to use it . if someone know please tell me how too :)
Thank you all
BY this way you can get width and height of your device and pass it in your method..
WindowManager manager = (WindowManager) getApplicationContext().getSystemService(Activity.WINDOW_SERVICE);
int width, height;
LayoutParams params;
if (Build.VERSION.SDK_INT > VERSION_CODES.FROYO) {
width = manager.getDefaultDisplay().getWidth();
height = manager.getDefaultDisplay().getHeight();
} else {
Point point = new Point();
manager.getDefaultDisplay().getSize(point);
width = point.x;
height = point.y;
}
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
Refer here http://developer.android.com/guide/practices/screens_support.html#testing
and
Get screen dimensions in pixels
try this to get Screen size,,
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
then try passing context from ur baseActivity to the current activity
and then try context.getWindowManger....
I am displaying a toast in my screen, but the position of the toast appears to vary from device to device, i want the toast to be displayed at one particular position for all the screen sizes.Please help me out with this.
Thanks!
First you need to get the screen dimensions.
Use them to calculate the toast's position, and place it in the appropriate position.
For any one if facing this same problem would suggest you to try this out
IMP is that you are not in the activity class
WindowManager wm = (WindowManager) appContext.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width=size.x;
int height=size.y;
where appcontext is ur own appcontext.
but if you are in the activity class you can use this
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
You may create a custom Toast message with a custom xml layout. Refer this example on Android Developers.
I'm getting a "The method getHeight() from the type Display is deprecated" error when trying to get the Height and Width of the screen size, code:
public JumpboyView(MyView mv, Context contextPlay) {
super(contextPlay);
// TODO Auto-generated constructor stub
bBoy = BitmapFactory.decodeResource(getResources(), R.drawable.boy);
Display display = ((WindowManager) contextPlay.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
height = display.getHeight();
and when trying to use getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
I get "The method getWindowManager() is undefined for the type JumpboyView" with 2 fixes available change to getWindowToken() or create method..
Help please?
p.s it used to work on older versions :\
Try this:
Display display = ((WindowManager) contextPlay.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Point size = new Point();
display.getSize(size);
height = size.y
So i have a small problem, i'm writing a function which need to send screen width to server. I got it all to work, and i use:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
to get width. However .getWidht() function is deprecated and it says u need to use:
Point size = new Point();
display.getSize(size);
But that function is only avaible for api level 13 or more, and my minimum sdk is 8. So what can i do? Is it safe if i stay with getWidth? Why adding new function and not make them backward compatible?
May be this approach will be helpful:
DisplayMetrics displaymetrics = new DisplayMetrics();
mContext.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int screenWidth = displaymetrics.widthPixels;
int screenHeight = displaymetrics.heightPixels;
You can check for API level at runtime, and choose which to use, e.g.:
final int version = android.os.Build.VERSION.SDK_INT;
final int width;
if (version >= 13)
{
Point size = new Point();
display.getSize(size);
width = size.x;
}
else
{
Display display = getWindowManager().getDefaultDisplay();
width = display.getWidth();
}
Here is the latest Code without Deprecation.
Kotlin
val metrics: DisplayMetrics = this.getResources().getDisplayMetrics()
val width = metrics.widthPixels
Java
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
If you want to be correct, use this approach:
int sdk = android.os.Build.VERSION.SDK_INT;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR2) {
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
} else {
Point size = new Point();
display.getSize(size);
}