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>
Related
I'm going through a problem in an application here my related to this issue. See if you can help me:
In my case I have a ImageView showing a ruler, and then I need to show this rule in real size, where it does not need to grow or shrink as the screen size or density. Using as a basis my Moto G3, it works perfectly, but when testo other devices, it loses the actual size of a ruler because the image tries to fit the screen size.
The image of the ruler is in PNG and measures 3600px x 155px and has measured up to 30cm, it is within a LinearLayout orizontal. In my Moto G3 visible area it is in 10cm, a larger screen for example it should show a larger area of the ruler (11 to 15cm for example), but it contunua only 10cm in the visual field of the screen, showing that it grows and shrinks as the display settings and density of the device.
Before I had a picture of RAGUA for each resource (xxhdpi, xhdpi, etc), so I decided to migrate it to the assets folder of Android, but still with the same problem.
Do you have a light on how to fix it?
Follows the code:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/rulerPolegada"
android:layout_width="1780dp"
android:layout_height="103dp"
android:layout_marginTop="-10dp"
android:layout_marginLeft="#dimen/left_ruler"
/>
</RelativeLayout>
</HorizontalScrollView>
</LinearLayout>
And as I said, after I image for assets, I started to set it in java:
rulerPolegada = (ImageView) view.findViewById(R.id.rulerPolegada);
rulerPolegada.setImageDrawable(Controller.getImageAssets(getActivity(), "ruler_polegada.png"));
Due to Understanding Density Independence In Android, you can calculate those values as follows
Here is how you can calculate for your device (Moto G3)
30cm = 11.811 in
Moto G3 screen density bucket is xhdpi - 320dpi
320px ~= 1 in
320px * 11.811in ~= 3780px
With this steps you can calculate how big your image should be.
Now, instead of providing different ruler's images for different screen densities, place your high quality image in drawable-anydpi folder, because A drawable in res/drawable-anydpi/ also is valid for any screen density. However, in this case, the -anydpi variant trumps any density-specific variant. due to '-nodpi, -anydpi, and WTF?' article from The CommonsBlog
Unfortunatelly, you can't used calculated values in layout xml file directly - ImageView size needs to be changed dynamically in Java code.
Above solution should give you decent accurancy (it can be diffent by 10%), but you can use DensityMetrics together with default display to get actual horizontal and vertical density, which will help you calculate image pixel size more precisely.
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
// these will return the actual dpi horizontally and vertically
float xDpi = dm.xdpi;
float yDpi = dm.ydpi;
Edit
Calculation for 480dp:
30cm = 11.811 in
density bucket xxhdpi - 480dp
480px ~= 1in
480px * 11.811in ~= 5669px
I'm not sure if you get your answers correct because it appears that previous answers were not satisfied. I had once the same problem - I need this ImageView to have the same size on a plethora of devices without scaling. I solved my problem by providing defined value in dp for layout_width and layout_height
You should read this guide for run in multiscreen
I have several buttons in my application that are displayed at the bottom of the screen. Right now the buttons have text in them. When running on the emulator, the buttons with text fit nicely. Now, that I am running on the actual device, some buttons' text takes more than two lines and the screen is not very presentable. I could change the font to make it work for the device in question, but there is no guarantee that it will work on some other device. Should I create button images (with text embedded as part of the image) and then have multiple versions, depending on the size of the device screen being used? That seems like a lot of work, is there a simpler solution to this?
Thank You,
Gary
You need to give equal weights to all buttons.So that all of them look similar and occupy same amount of space.
You have to get screen resolution and set sizes as a proportion of this resolution.
Here is the sample code to obtain screen width and height.
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
It is not hard but little bit tricky.
In this purpose you can use built in draw-able folder. In android project there are many draw-able folder like drawable-hdpi, drawable-mdpi, drawable-xhdpi where you can put different size of images and it will automatically render image based on device screen. Check this tutorial for more understanding Supporting Multiple Screens
Or you can take screen size dynamically. Based on the screen size you can set the button height and width.
You can find multiple screen size handling tutorial here:
Supporting Multiple Screens
your emulator may have specific resolution that is different than the one of your actual device.
I've a Photo gallery in my app. The set of pictures are stored in the drawable folder. I'm making use of ViewPager when the user swipes through the images.. What is the width and height in pixels and dps for xhdpi, hdpi, mdpi??
I know that the Android documentation says px = dp*dpi/160.. but I'm still confused about what should be the pictures download pixels so that it fits in all screen sizes??
There isn't one magic size that guarantees that it fits all screens perfectly. There are more than one screen size that fall under each density.
You should look at making your layout scale well on as many devices as possible. In your case it sounds like you shouldn't be focusing on an a specific pixel size, but rather how to display correctly on the common screens and gracefully display on less common ones. That being said I would do the following:
I'd look at the dashboard here. I'd make layouts targeting first targeting hdpi with a normal screen size(50.1% of the market) followed by xhdpi(25.1%) with a normal screen size, followed by mdpi(11%) normal screen size. Check table 3 on this page for common screen sizes for those values. Since you will be most likely using an image view make sure to check out the scale type attribute to help handle when the image view isn't at
As a side note(maybe useful later)if you are having difficulties translating sizes between densities and raw pixel values use this calculator.
Use following function which give you height and width of current display
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
deviceHeightPix = displaymetrics.heightPixels;
deviceWidthPix = displaymetrics.widthPixels;
based on this, you can pass data on server to fetch relative images.
I am making an app for a tablet which has a resolution of 1280 x 800, and I want custome graphics for it. But when i took a screenshot of the screen the resolution was 1024x600, why is this? What size background image would I make?!
As mentionned here : http://developer.android.com/reference/android/util/DisplayMetrics.html
if you want the pixel of your screen, you can write
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
and then, you can call :
int width = metrics.widthPixels;
int height = metrics.heightPixels;
in the simulator it is actually resized to fit your screen and takes in your screens resolutions nothing wrong with your code or images.
Screen shot dimension maynot be same as resolution.
you need to make graphics with 1280 x 800.
screen shot taken depend on theway how it is created,it doesnt have to be same resolution as display.
Is there an API in Android that returns the full screen height of the device? I'm interested in the full height as specified in the device spec, not the height of the viewable screen as returned by
android.view.Display.getHeight();
I spent some time looking for this and didn't find anything like what you're asking for. Part of the problem is that the bar that takes up the pixels they're not including can potentially be of different sizes.
What I ended up doing is measuring the width of the device (which is the full width) and the not-quite-full-height to match against the standard resolutions with an approximate. It's not great. If you really need it exact, you can force an orientation change to match both dimensions exactly. That's pretty gross, though.
The real answer is that you're not supposed to care about the exact dimensions of the hardware and design using the OS-provided size buckets.
Does this not work for you?
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
displayHeight = dm.heightPixels;
For me, displayHeight returns 960 pixels on my HTC Sensation
Try heightPixels on a DisplayMetrics object.