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.
Related
I have three layouts :
/layout/layout.xml
/layout-xhdpi/layout.xml
/layout-xxhdpi/layout.xml
And i have two emulators :
Nexus 5x with Api level 28 screen size 1080x1920 420dpi
Pixel 3xl with Api 28 screen size 1440x2960 560dpi
What do I expect?
Nexux 5x should select the layout in /layout-xhdpi/layout.xml
Pixel 3xl should select layout in /layout-xxhdpi/layout.xml
What happens instead?
Both devices select layout in layout-xxhdpi.
I have also tried to replace by folder such as layout-sw420dp and layout-sw560dp one or both emulator select another layout.
Why does this happen, is there something?
Considering you are running one project on two devices you must go as per following approch:
set the setContentView(R.layout-xhdpi.layout.xml); in java
then tun the project on Nexux 5x
now set the setContentView(R.layout-xxhdpi.layout.xml); in java
then run the project on Pixel 3xl
If anything concerns you please leave a comment
First get the screen size of device by using:
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
After this you can use if else statement like
if(height==first phone display height)
{set the setContentView(R.layout-xhdpi.layout.xml);}
Else{setContentView(R.layout-xxhdpi.layout.xml);
}
Remember you need to pass exect screen size at place of first phone screen height
I have an application that should work in a portrait mode only.
it works great using just (Layout) resources.
Now I face a problem with some device,
The device has only Portrait mode, no orientation, it is ok for me.
The screen height is smaller than its width, and remember this is not landscape mode.
__________
| portrait |
|__________|
If I used Layout-land, it will not work, and the device pick the normal layout resource.
I tried layout-sw, layout-w, layout-h, without solution.
I want the normal devices to use "layout" and the devices which has height smaller than the width use another layout.
And remember please, this is not a landscape.
Thanks in advance.
===========================================================
Update
Is there any way to use Layout-11x00, where 11 is the width, and the 00 is the height? I tried it and it worked, but the problem is it worked for all devices, not the wide devices only. I tried Layout-774x480 as w=774 and h=480. but it worked also with my Samsung mobile, I do not want this.
Why when I set the layout to "Layout-774x480" it targeted my samsung? while my samsung width is not > height?! I mean what is the different between "Layout-774x480" and "Layout-480x774"?
You can set a qualifier along with the layout folder name to get your layout picked from that specific folder when you are running your application in a device. Here is the documentation where I recommend you to take a look.
In your case you just have to create a layout directory named res/layout-w600dp/main_activity.xml which indicates if the device screen is 600dp wide and bigger, the layout will be picked from this folder.
I checked that you have tried already with layout-w, layout-h and layout-sw settings. However, I think you are missing the expected width of the device screen which should be picked.
Hope that helps!
Update
If you are willing to set different layout based on the decision that the height is less than the width, then you might just consider checking the screen height and width before setting the content view of the activity.
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
if(height > width) setContentView(R.layout.main_activity.xml);
else setContentView(R.layout.main_activity_wide.xml);
I am developing an Android app that is causing me problems with devices that have 1024 as their width dimension. I have tried creating a layout folder layout-w1024 and layout-mdpi-sw1024dp; but the device uses the layout-mdpi folder instead. I am using 15 as the minimum android version.
Is there any other way using which I can separate 1024 devices?
You can do it programmatically if it's suitable for your project, something like this would call a different layout depending on the dimensions of the screen. This has some limitations though since it can only be called from an Activity (shouldn't be a problem in your case) and if you have problems with layouts all over your application it may not be very scalable.
The conditional width == 1024 should probably be replaced with something greater than or more specific, you'll need to play with this for a while to make it work. You should also account for the height of the screen, not only the width.
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
Or if you want it in pixels instead:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int width = metrics.widthPixels;
And then:
//Depending on the width, set one layout or anotherone
setContentView(width == 1024?R.layout.for1024:R.layout.normal);
Source
Is there a way to make Android believe the current device has a certain screen size (pixels, not density) which is smaller than the actual display size (again pixels, not density)?
I need the following: On a device (not emulator) with a 1280x720 pixel (for example) display I want to use only 800x480 pixels - resulting in a homescreen attached to one of the corners and the rest of the display left black (or whatever color).
This has nothing to do with screen resolution since changing the resolution would fill the complete screen - which is what I don't want (see above).
Similar questions on SO have not been answered with any help.
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
metrics.heightPixels;
metrics.widthPixels;
You can fix height and width of your main layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="800px"
android:layout_width="480px">
...
...
</RelativeLayout>
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));