I would like to change the position of one element (editText) in my UI when the user taps it. The positioning should be ~20 dp under the actionBar, no matter screen-size or DPI the device is using.
How do I accomplish this using only (java)code? Please try to give a more exact answer. I have seen other examples similar to this but none that explains an exakt scenario? I was thinking of a start like the code below? Then what?:
DisplayMetrics dm = new DisplayMetrics();
mSearchField = (EditText)v.findViewById (R.id.something);
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
int [] loc = new int[2];
mSearchField.getLocationOnScreen(loc);
int distance = dm.heightPixels - loc[1];
Get the Display height and width using DisplayMetrics.
DisplayMetrics displayMetrics = new DisplayMetrics();
mContext.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
Log.e("width",width+""+height);
Related
Is it possible to have a dynamic color scheme that adjusts to the type of display each device has? Greys between screens can look different and I am wondering if there is an efficient way to create similar looks between the colors of screens.
catch the screen oration and create a color .
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
float oration = height / width;
Color backgroundColor = new Color();
backgroundColor.red(Math.abs(Integer.valueOf((String.valueOf(oration)))*10));
........
This question already has answers here:
How to get screen dimensions as pixels in Android
(46 answers)
Closed 9 years ago.
Can i get resolution of android phone..?
if yes then how..?
it will really helpful for me..
Thank you..
If you want the display dimensions in pixels you can use getSize:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
This method was introduced in Android API 13, so if you want to get display metrics for previous APIs use:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int wwidth = displaymetrics.widthPixels;
If you're not in an Activity, you can get the default Display via WINDOW_SERVICE. Also be sure to set your minSdkVersion to at least 4 in AndroidManifest.xml to enable calls to display.getWidth().
WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
Edit: PhoneGap
Use the following to figure out the display size in pixels
function getDeviceDimention() {
console.log("Device Dimention using PhoneGap");
console.log("Width = " + window.innerWidth);
console.log("Height = " + window.innerHeight);
}
For Cordova / Phonegap, I've found that using
var width = screen.width
var height = screen.height
is more reliable than using
var width = window.innerHeight
var height = window.innerWidth
in order to obtain the resolution of the current display. The latter is unfortunately inaccurate if your mark-up either overflows or does not take up all of the space available on the screen.
For phonegap you can use this
You can use device-width and device-height
<meta name="viewport" content="width=device-width; user-scalable=no" />
You can even get device height and width using jquery & jquery mobile like this
var width = $(window).width();
var height = $(window).height();
NOTE:- Do this when device is ready.
You can call
Display display = getWindow().getWindowManager().getDefaultDisplay();
Point outSize = new Point();
display.getRealSize(outSize);
outSize.x is the width and outSize.y is the height. Hope it help.
like this
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
look at this answer Android: How to get screen dimensions
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
Good luck!
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
good luck
행운을 빈다.
u can get screen resolution like this
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height=display.getHeight();
Please get me correctly over here :
I want to get the height/width of the space available to the Activity/Layout in onCreate() method to calculate the height that can be given to child layouts. I can get the screen size using :
root = (LinearLayout) findViewById(R.id.mainroot); // Main layout of LinearLayout
android.view.Display display = getWindowManager().getDefaultDisplay();
int height = Display.getHeight(); // I know this is deprecated have hence used
int width = Display.getWidth(); // DisplayMetrics
int childWidth, childHeight;
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
//int density = metrics.densityDpi;
height = metrics.heightPixels; //480
width = metrics.widthPixels; //320
This both the methods/ways gives me same height and width i.e. size of full screen. What I am looking for is to get actual height that is avaialbe for the layout after deduction of Application Title, Status Bar, etc.
Any idea how to get this. OR to get the sizes of titles, etc - what all should be counted over here. On emulator I see 2 bars on top - 1 must be application titile what an be the other one. I can get heights of them all and deduct from screen height.
ONE more point : In this case I will be setting the height programamtically so it will be pixel based (as I can setheight in pixels only I guess) will that affect in density factor with differnet screen sizes. What can be a way to calculate height (lets say 50%) for child layout that will be same for any density o so.
SOLUTION :
In my onCreate(), I added the following lines :
setContentView(R.layout.mainpage);
root = (LinearLayout) findViewById(R.id.mainroot);
root.post(new Runnable() {
public void run() {
Rect rect = new Rect();
Window win = getWindow();
win.getDecorView().getWindowVisibleDisplayFrame(rect);
int statusHeight = rect.top;
int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop();
titleHeight = contentViewTop - statusHeight;
Log.i(Utility.TAG, "titleHeight = " + titleHeight + " statusHeight = " + statusHeight + " contentViewTop = " + contentViewTop);
// CALCULATE THE SIZE OF INNER LAYOUTS
calculateChildSize();
}
});
With the above code, I get the values of titleBar & statusBar. On deducting it from metrics.heightPixels; I get the required height of the screen.
Good Point is this code works for all density's.
Hope this helps others too.
FOR IMPROVEMENT : I have to do similar calculations for all Activities in my application, so was thinking about writing this code only once. I can save teh titleHeight to a static variable so can use in all activities.
BUT
Can the user change the phone's density at runtime.
If so, then the Activity's onCreate will be called again or not ?
If not, then can I trap the density change event where I can add this code and make the current activity to refresh.
Any idea suggestions for improving is appreciated.
//Where rootView is the object for the root view of your application
final int viewWidt = rootView.getMeasuredWidth();
There is very easy method for that. doOnLayout() method is called as soon as layout is measured and ready:
rootView.doOnLayout {
rootView.getMeasuredWidth()
rootView.getMeasuredHeight()
}
I have a layout with an imageview. This imageview should have a 4/3 ratio. In portrait mode i get the width of the screen then compute the height and set the imageView dimensions.
int[] tailles = new int[2];
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int widthScreen = dm.widthPixels;
tailles[0] = widthScreen;
tailles[1] = (tailles[0]*3)/4;
This part works great.
In landscape mode i need to set the imageView width according to the available height. So i need to get the height of the screen minus the status/notification bar.
I saw the value of 48px for the bar in this question. Is it a standard size ?
What the best solution to get the available height regardless of the used device ?
Display d = ((Activity) mContext).getWindowManager()
.getDefaultDisplay();
int h = d.getHeight();
int w = d.getWidth();
in portrait h > w and in landscape w > h
If you're creating an Activity, than you have to have some sort of layout. Get it's root component, and retrieve it's size (getWidth, getHeight). That'll be the available width and height without counting the status bar.
I made an application with a dynamic layout for different screen sizes.
It also worked for all screen sizes except of 320x240 screens.
I used the same code to get the width and height for all and it also return the correct value but the it does not return the correct value for manually create emulator means 320x240 and 240x400.
I used the following code:
DisplayMetrics metrics = new DisplayMetrics();
WindowManager manager = (WindowManager)getSystemService(Context.WINDOW_SERVICE);
manager.getDefaultDisplay().getMetrics(metrics);
int width=metrics.widthPixels;
int height=metrics.heightPixels;
I am late..but would like to share :)
Please try below code snippets:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();