I need to know how to set the LayoutParam according to the resolution of the mobile phones..
Im using a GridView where I'm using GridView.LayoutParams(45, 45).. Now if I'm using mobile with small screen size is ok.. But if I test with the big screen device like HTC Desire HD then its looking so small.. How to make everything as similar?
Does using android:numColumns or setNumColumns(int numColumns) not work for your needs?
http://developer.android.com/reference/android/widget/GridView.html#setNumColumns(int)
Divide your display's width in 3 parts (for 3 columns or 4 for 4 columns....)
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
imageView.setLayoutParams(new GridView.LayoutParams(width/3 , width/3));
Related
I started to create an app with the gridlayout. After I wrote the XML-knote of the gridlayout I placed within the gridlayout several buttons.
But if I change the Android Device from Nexus N to Nexus S the Layout starts to look like this:
and the same problem with the size goes on with Nexus XL
What should I do to keep the layout size of Nexus N to all the other devices?
DisplayMetrics displayMetrics = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
height = displayMetrics.heightPixels /NumberOfButtons;
width=displayMetrics.widthPixels/3;
You need to set the button height dynamaically at the runtime
Through above code you can get height and width of screen.
I know there are a lot of questions similar to my title but I couldn't find the answer that I want.
In my app, I am using window.innerWidth and window.innerHeight to get the screen size, but these two values return different size. For example when I run my app on Note 3, I get 360x615 while it should be 1080x1920.
The following code in Java gives me what I want:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
So is there any way in Phonegap to get the same screen size value as Java?
Update: when I test the same code on Note 2 with Jelly Beans I get the correct size 720x1230
I am using this code
hope it help you
var physicalScreenWidth = window.screen.width * window.devicePixelRatio;
var physicalScreenHeight = window.screen.height * window.devicePixelRatio;
I am assuming you want to figure out the actual device pixels instead of the more commonly measured CSS pizels? You could read up on this here.
Basically, if you want to know actual device pixels you could use screen.width and screen.height. If you want to know CSS pixels you could use window.innerWidth and window.innerHeight.
The meta viewport tag may be ignored on certain Android devices, those are my experiences with Samsung Galaxy tab devices anyway.
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
screen_width = display.getWidth();
screen_height = display.getHeight();
I understand that the code above could drive me to get the screen width and height. I put this code in "onCreate" of "MainActivity".
But my friend's mobile device could not show the desired layout. Yet, it is too trouble for me to debug in his mobile device. Thus, I look forward to the help here.
My Question is ...
Is it right to get the width and height in "onCreate"?
Is it useful to get the width and height of all devices? (of course, API>=11)
Yes it's right to get the Screen Height and width in onCreate() method because if your Screen rotates then your onCreate will be called. and it's not mandatory that you should take Screen Height & width if needed OK or else you can manage it in Xml as well. To get Screen height and width i think this is the best way...
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
for more details you use this link as well
http://developer.android.com/reference/android/view/Display.html
You could try metrics instead:
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
int displayHeight = metrics.heightPixels;
int displayWidth = metrics.widthPixels;
Doing it in onCreate should be fine.
But honestly, I don't think you are really getting the wrong height and width. The screen size is probably just very different from your test devices.
You will need to debug with his phone at least once to find out what the problem is.
In my opinion it is a good approach to get the width and height, because it gives you more control. Most layout stuff could be done by working with proper layouts (eg LinearLayouts with weight), but there are cases where doing it manually will work better. You can do things more precisely, but it will be more work.
Also if you don't have a large variety of devices for testing it will be tough to adjust it properly. Just think about all the different sizes the Android world can confront you with. I for example had some problems with very small devices, like a Galaxy Mini. Also very big devices like tablets might be a problem. Theres just no way around testing edgecases if you want to do it manually.
3.2 introduced a great new way to create layouts... by using layout-swXXXdp in the res folder.
The problem is it controls what layout is used by the SmallestWidth in dp. This is great, but if you have a tablet that's 1024x600 and a tablet that's 1024x768, there is an issue. My app is landscape only. It is a gridview with a certain number of items in a row. The problem is that I need the gridview to adjust how many items are in a row based on the height resolution. All screens that are 1024 pixels should have 5 items in a gridview row, while all tablets that are 1280 pixels should have 6 items in a gridview row. It should not be based on width because then the gridview gets a little squished. How can I do this? (base my layout choise on the height, not width)
Please try this
Get Height and Width of the Display
Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Check the height after declaration of GridView and set Number of columns accordingly
For example
GridView gridView = (GridView)findViewById(R.id.app_gridview);
if(height <= 1024){
gridView.setNumColumns(5);
}else {
gridView.setNumColumns(6);
}
I think this will solve the problem
I am creating an application for android OS. The problem im facing is that running the application on different devices with different resolutions, the layout eitehr become too small, on hi res screens, or too chunky on low res screens. How do i make it so that my layouts adapt to the screen on the device??
Do i use relative layout Or is there a way i can just find out the max screen length and width...?
Use RelativeLayout -- thats the best option.
In anycase, if you want to find out the max dimensions:
private void setupGlobal() {
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Global.screenHeight = height;
Global.screenWidth = width;
}