Change Orientation while converting android handset app to tablet - android

I am converting and android handset application to tablet (1024*600 and 1280*720). I have given the support for different screen- size, but stuck in orientation part. I want some pages to be visible as landscape. For example after logged in by user the next intent should be in landscape form. After searching I found clue but not very understandable answer such as
By adding activity in manifest.xml file
At runtime by using getOrientation().
Thanks

The question is not clear, if you're forcing screen orientation by size (e.g. on tablet device there's only landscape orientation), i think you should get the screen width and height first then evaluate it. If the screen width and height are 1024*600 or 1280*720, you change the orientation with setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
To get the width and height, do this in your activity:
Display display = getWindowManager().getDefaultDisplay();
int width, height;
try {
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
}
catch (NoSuchMethodError e) {
width = display.getWidth();
height = display.getHeight();
}
See this

Related

Get reported screen width and height

This may end up being a "yes" or "no" question...
Starting in Nougat (maybe marshmallow?), users can change the display size of their device. Specifically on the Galaxy S8 and S8+, the user can change the display resolution.
I've seen the solutions here: Get Screen width and height
QUESTION: Do these solutions give the actual height/width of the screen regardless of changed resolution or do they give the adjusted height/width of the screen if resolution changed?
If the first, how do I get the adjusted height/width?
Interesting question. It gives me adjusted dimensions in pixels. I have tested it now in my Pixel running Android O.
Method 1:
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
Method 2:
Configuration configuration = getResources().getConfiguration();
int screenWidthDp = configuration.screenWidthDp;
int screenHeightDp = configuration.screenHeightDp;
int smallestScreenWidthDp = configuration.smallestScreenWidthDp;
Both methods returned the adjusted dimensions. The first method returns the device screen dimensions. The second method returns the dimensions of the app screen. These two result would vary when there are two apps open in multi-window mode.

Android DisplayMetrics display absolute size varies with orientation

Has anyone noticed this issue and resolved the way of getting the absolute display size consistently in both orientations?
Example, Nexus 5 (5.0.1):
Portrait: width = 1080 height = 1776
Landscape: width = 1794 height = 1080
I would have thought that the height in portrait would match the width in landscape. Initially suspected the status bar, but docs are clear. Anyway the status bar height in this example is 75px in either orientation and the diff in the example is 18px.
Code snippet for display width and height in pixels:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
String dimensions = String.format("width = %d height = %d", width, height);
Log.v(TAG, dimensions);
Assuming the display is FullHD acording to your logging there's something of 144px (48dp) height in portrait and 126px (42dp) width in landscape occupying the display (when scaling factor is 3 which is xxhdpi). I bet it's the navigation bar with Back, Home and Recent buttons. This is sufficient for choosing layouts.
EDIT:
If you need the full display size the following code is available from API 17:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
if (Build.VERSION.SDK_INT >= 17) {
display.getRealSize(size);
} else {
display.getSize(size); // correct for devices with hardware navigation buttons
}
EDIT2:
If you want to make sure you get correct result on API 14 through 16 consider following this answer along with its comments
https://stackoverflow.com/a/11004877/2444099 .
It's not the entire screen size, its the size of the UI that display metrics gives you. You have to factor in the amount of screen size taken up by the area taken up by the status bar (clock, battery level, signal strength bar) which will differ on orientation.

auto resizing textView with different density devices android

At the top of my app, I have a title which should be shown in the middle, and a button on the right. As the textViews length is behind my control, I sometimes have my title crossing the button due to long length of the content of it.
After following this, I somehow tend to solve the problem. My device was HTC desire. Unfortunately, if I check with Galaxy SIII, it doesn't do the trick.
I am wondering how I can manage this in terms of different devices with different densities.
My controls inside the relative layout
You can also check the device screen density by this--
WindowManager wm = (WindowManager) _context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
And can manage accordingly whats your apps needed..
just use weightsum in your xml and make width of all the views as fill parent ..... this makes auto resizing of your textview
you can maintain layouts according to their DPI`s
replicate the same XML data in XHDPI (As S3 falls in XHDPI) and test it similarly replicate the XML data in HDPI
but
keep in mind the following Thing Pixel Ratio of the layOut as
following
in LDPI its 1:0.75
in MDPI its 1:1
in HDPI its 1:1.5
in XHDPI its 1:2
Display display = ((WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
DisplayMetrics dm = new DisplayMetrics();
display.getMetrics(dm);
float density = dm.density;
int screenWidth = display.getWidth();
With this code above, you'll have your screen density as float.. So you can use it to calculate your textView's width like:
int newWidth = (int) (density * 100);
which 100 is here based size.
Or you can have a ratio according to your screenWdith.
int newWidth = screenWidth / 2;

Android WindowManager shows same screen size for different devices

I am debugging my application on 2 different Droid devices: Bionic and Droid3. When I use WindowManager to display device width & height, it shows 540x960 (portrait) and 960x540 (landscape) for both devices! How is that possible? The 2 devices are clearly of a different size. Here is the code:
Display display = getWindowManager().getDefaultDisplay();
final int width = display.getWidth();
final int height = display.getHeight();
Thanks,
Igor
I read that getWidth()/getHeight() is deprecated.
Try to use this:
Display display = getWindowManager().getDefaultDisplay();
Point displayDimensionInPixel = new Point();
display.getSize(displayDimensionInPixel);
I'm not shure right now because the Documentation isn't there anymore, but I think I can remember that getWidth()/getHeight() of Display dont return pixel dimensions.

Android, get screen width and height?

I am trying to get the width and height of the screen in Android, i have:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
stageWidth = displaymetrics.widthPixels;
stageHeight = displaymetrics.heightPixels;
The width is 1280 but its coming back with 752.
Any ideas?
Try this instead:
Display display = getWindowManager().getDefaultDisplay();
stageWidth = display.getWidth();
stageHeight = display.getHeigth();
Is that the same width? Are you sure the width is 1280 and not 752? What device are you testing it on and what height are you getting?
Use this method on the display object (from the docs):
void getSize(Point outSize) -
Returns the raw size of the display, in pixels.
Rotate your screen and see.I think you will get your correct screen size.
The above code worked with me and should work with you.

Categories

Resources