I am new to android developing please help me in this problem.
static Display display=((WindowManager)context
.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
static int height=display.getHeight();
static int width=display.getWidth();
This shows error so give me solution.
11-26 17:40:17.806: ERROR/AndroidRuntime(422):
java.lang.ExceptionInInitializerError
you can Try this for Get Height-width and dpi
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
width = displayMetrics.widthPixels;
height = displayMetrics.heightPixels;
dpi = displayMetrics.densityDpi;
try with this
Display display = getWindowManager().getDefaultDisplay();
public static int width = display.getWidth();
public static int height = display.getHeight();
Related
I want to change the screen width and height for whole platform. So I want to know where should I change ?
As I understand that DisplayMetrics provides the width and height for all apps. How DisplayMetrics gets those width and height ? Any leads is appreciated.
For getting : width and height :
DisplayMetrics metrics = this.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
i use this when i made a code to setup wallpaper :
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
I know there are many answered questions about how to get screen width and height, but I tried all I could find and I can't get any of them to work.
For example, in this code I get error for context:
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
Use getWindowManager().getDefaultDisplay().get height()/get width().
Try to get Context of Application with getApplicationContext().
DisplayMetrics metrics = getApplicationContext().getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
If I extend activity in my app I can get width and height:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int wwidth = displaymetrics.widthPixels;
or
Display display = getWindowManager().getDefaultDisplay();
stageWidth = display.getWidth();
stageHeight = display.getHeigth();
But at present I extend fragment and I can't use the above code to get the width.
Try below modifed code of yours
DisplayMetrics displaymetrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
or
Display display = getActivity().getWindowManager().getDefaultDisplay();
int stageWidth = display.getWidth();
int stageHeight = display.getHeight();
Basically you just required to put getActivity() (to get the context) before getWindowManager() function.
This will give you what you want without needing for context or view:
import android.content.res.Resources;
int width = Resources.getSystem().getDisplayMetrics().widthPixels;
int height = Resources.getSystem().getDisplayMetrics().heightPixels;
This code also works with Fragments:
int width = getResources().getConfiguration().screenWidthDp;
int height = getResources().getConfiguration().screenHeightDp;
For comparing orientation:
if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){...}
I am trying to get the size of the screen. The size I have set for emulator is 540 x 960 but what I am getting is 320 x 480. Any suggestions on it to get the right size. Here is the code;
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
width = dm.widthPixels;
height = dm.heightPixels;
Try this:
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
try this one:
int height = getWindowManager().getDefaultDisplay().getHeight();
int width = getWindowManager().getDefaultDisplay().getWidth();
this is a really simple question on which I've found no answer :/
How can I quickly access the screen resolution (width, height) as integer values?
I've tried this one, but it always shows zero on my emulator:
DisplayMetrics dm = new DisplayMetrics();
int width = dm.widthPixels / 2;
In my case I want to dynamically create a table with tableRows, each containing two cols. This cols all shall fill half of the screen in width.
Can someone give me a hint?
The trashkalmar answer is correct.
However, the results will be specific to the activity context. If you want the whole device screen resolution:
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 screenWidth = displayMetrics.widthPixels;
int screenHeight = displayMetrics.heightPixels;
Note that the results also depend on the current device orientation.
You can get screen metrics in this way:
Display d = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = d.getWidth();
int height = d.getHeight();
1.Simply use This inside activity to get screen width and height pixels.
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay()
.getMetrics(metrics);
int width = metrics.widthPixels;
int height = metrics.heightPixels;
2.This can also be used But requires Api level inlined check
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
display.getSize(size);
int width2 = size.x;
int height2 = size.y;
} else {
int width2 = display.getWidth();
int height2 = display.getHeight();
}
3.Use This when you are not in activity but having context
WindowManager wm = (WindowManager) context.getSystemService(
Context.WINDOW_SERVICE);
Display display1 = wm.getDefaultDisplay();
Point size1 = new Point();
int width1;
int height1;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
display1.getSize(size1);
width1 = size1.x;
height1 = size1.y;
} else {
width2 = display.getWidth();
height2 = display.getHeight();
}
I use the following:
int scrWidth = getWindowManager().getDefaultDisplay().getWidth();
int scrHeight = getWindowManager().getDefaultDisplay().getHeight();
No muss, no fuss, just 2 class variables
The easiest way will be to implement onSizeChanged. You are probably getting a value of 0 because the view hasn't initialized yet. You can't call the above code in the View constructor for example.