linear layout (View) Size in android - android

In my application i have gridview in linearlayout. Due to different sizes of screen, i want to calculate the width of the gridview's container (which in linearlayout) and then i will decide, how many column should display on screen. My problem is. when i write
linearlayout.getwidth();
it returns 0 and its because its too early to get size of view in oncreate method.
What is the clean solution to get height or width of the view.
i tried
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int gridSize = display.getWidth();
but android says its deprecated :(

Display.getWidth() and Display.getHeight() are both deprecated as of API 13 in favor of Display.getSize(Point). In order to be compatible with both, you have to version-check and pick the appropriate one.
This is how I do so:
Point size = new Point();
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
if (Build.VERSION.SDK_INT >= 13) {
display.getSize(size); // API 13+
} else {
size.x = display.getWidth();
size.y = display.getHeight();
}
Then afterward, size.x is your width. (Don't forget to precede your method signature with #TargetApi(13) #SuppressWarnings("deprecation").)
Keep in mind, this is the unit's display width; if you want to simply get the size of one view, you have to call it after the UI is laid out. That's slightly unrelated to this answer, but you can find a solution in this thread's answer instead.

Related

Support Multiple screen sizes

I want to make my application support all screen size in my last published build i use wrap content and match parent for handle it but now i want to generate all layouts that respond for each screen size.
I know how to make this by creating folders for each size and put layouts in it but I ask for there is any plugins or feature that make this operation dynamically without make it by myself, I know i will edits in layout to handle many cases but i want to make it simple by generate layout and customize what I want. Thanks for help.
For supporting multiply devices, there are two ways, one from view side and other is programmatically. If you want to do all operations in xml, you can do it with RelativeLayout easily. It has some parameters, like android:layout_alignParentBottom, android:layout_alignLeft or etc, with these parameters, you can customise your views for multiple screens.
Other way is programmatically way, which you can do your operations in java side. For this way, first of all, you shoul get screen sizes:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int SCREEN_WIDTH = size.x;
int SCREEN_HEIGHT = size.y;
Before getSize was introduced (in API level 13), you could use the getWidth and getHeight methods that are now deprecated:
Display display = getWindowManager().getDefaultDisplay();
int SCREEN_WIDTH = display.getWidth(); // deprecated
int SCREEN_HEIGHT = display.getHeight(); // deprecated
And then, get display metrics and density:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
float density = metrics.density;
After getting these parameters, locate your views where you want. For setting view , you will need such these simple lines:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.width = (int) (40 * density);
layoutParams.height = (int) (40 * density);
imageView.setLayoutParams(layoutParams);

Get screen size in Android

It seems in Android there are two ways to get screen width & height:
The 1st way:
WindowManager windowManager =
(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
//We get width and height in pixels here
width = size.x;
height = size.y;
The 2nd way is:
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
//We get width and height in pixels here
int width = metrics.widthPixels;
int height = metrics.heightPixels;
Both ways get screen width and height in pixels. So, I get a bit confused about the following two questions:
Q1. What are the differences between these two screen sizes theoritically?
Q2. Why google provide two ways to get screen size? I think there must be a reason behind it.
You should read about those two Classes
Window Manager
http://developer.android.com/reference/android/view/WindowManager.html
Resources
http://developer.android.com/reference/android/content/res/Resources.html
Window Manager gets screen width and height by getting the width and height of display that the window manager instance is managing including secondary display.
Getting width and height from Resources return the current display metrics and the objects should be treated as Read Only.
looking at the docs about DisplayMetrics:
A structure describing general information about a display, such as
its size, density, and font scaling.
It seems that the DisplayMetrics class provides you with basic hardware display information. It does not care about any system UI components that may (or may not) be present.
While the Display class is more extended and takes the things mentioned above into account.
To answer your question:
int width = metrics.widthPixels;
int height = metrics.heightPixels;
will provide you with absolute values, i.e. you will always get 1080/1920 on a full HD display.
The description of the getSize() method is pretty much self-explanatory:
[...] The size returned by this method does not necessarily represent
the actual raw size (native resolution) of the display. The returned
size may be adjusted to exclude certain system decoration elements
that are always visible. [...]
DisplayMetrics is a data object which best describes the details of the current display, including widthPixels, heightPixels, xDpi, yDpi, density, densityDpi and scaledDensity.
Display is a more logical object which performs the logical operations behind finding these parameters. For example, Display can be used to extract DisplayMetrics like so:
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics); // metrics will now be populated with the required data.
Note: The above code is, in essence, how Android generates the DisplayMetrics instance inside the Resources object.
I most often use DisplayMetrics, but both options should work just fine.

Use alternative layout when there's not enough space

I have a RelativeLayout that has a number of controls in it. On some screens, there might not be enough room for this layout as well as the other screen elements. In this case, I'd like to use an alternate layout that excludes some controls and has less padding.
How can I implement this in a performant manner? The smaller layout should only be loaded if the larger layout definitely won't fit. I don't want to use the various screen size qualifiers because whether the layout will fit is a function of the width and height of the screen and the additional visible controls and that seems like it will get too complicated.
I Personally think you should use the new screen size qualifiers. It's how Android is designed and I think you should follow those rules. It will very probably also make the whole development process a lot easier!
But if you insist on doing it yourself, the only way I can think of to "hardcode" it, would be something like this:
Step 1: obtain the screen size
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
or if you're targeting a lower API level:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth(); // deprecated
int height = display.getHeight(); // deprecated
Step 2: pick an alternative layout depending on the screen size
if(width < value_x_small && height < value_y_small){
setContentView(R.layout.main_small);
} else if(width > value_x_small && height > value_y_small){
setContentView(R.layout.main_large);
} else {
setContentView(R.layout.main_larged);
}
This code allows you to select your own resources depending on the screen size in px. Its probably better to convert the px values to DP (DPI) and use those to make your decisions.You might also want to add a check on the current screen orientation.
You can implement your controls as separate Fragments and position them depending on screen size. Here is more info about Fragments and their Design Philosophy .

Disable ImageView if it doesn't fit on the screen

I have an ImageView with a dynamic src, and I want to display it, only if there is enough place to show all of it without scaling it.
How should I handle this?
you need to get the image size and screen size then check to see if its larger before displaying
If you want the the display dimensions in pixels you can use
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
If you don't have access to an activity to call getWindowManager on. You can use:
Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
For the use case you're describing however a margin/padding in the layout seems more appropriate.
then do
if (ImageView.getDrawable().getIntrinsicHeight() > height || ImageView.getDrawable().getIntrinsicWidth() >width){
//dont display image
}
coppied and compiled from two post
Get screen dimensions in pixels
and
Trying to get the display size of an image in an ImageView
imageView.setScaleType(ScaleType.FIT_XY);

How do I obtain screen resolution information?

I need to obtain the screen resolution, width & height, how do I do that?
Solution:
Display d = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
width = d.getWidth();
height = d.getHeight();
Display d=Display.getInstance();
int width = d.getDisplayWidth();
int height = d.getDisplayHeight();
According to http://forums.java.net/jive/message.jspa?messageID=479230
or possibly
Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
http://www.coderanch.com/t/435390/Android/Mobile/screen-size-at-run-time
PLEASE first ask yourself why you need to do this. You very probably don't. If it is to do anything based on the screen size (layout etc) this will return you the wrong information, since it is telling you about the physical resolution of the screen, not taking into account anything like the status bar, window decorations, etc. Even if you are running your app as full-screen, in some cases there may be parts of the screen that are used for system UI that you can't get rid of but will be included in the numbers returned by Display.
The correct thing to do is adjust for the size your view gets during layout, such as when its onSizeChanged() is called.

Categories

Resources