How to get content height of Activity? - android

I have an Activity with no actionbar, and try to get its height.
Tested on Nexus 7 2013 with resolution 1200x1920;
Using DisplayMetrics:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenHeight = metrics.heightPixels;
Then the screenHeight is 1824.
The different between real height (1920) vs. screenHeight is because of the status bar.
But when I check its root view:
final View root = findViewById(R.id.root);
root.post(new Runnable() {
#Override
public void run() {
Log.d("TAG", "Root #w=" + root.getWidth() + " #h=" + root.getHeight());
}});
And result of height is: 1774.
Content height is smaller than screen height is because of the virtual key (BACK, HOME, SWITCH APP).
To sum up: screen height = statusbar height (if have) + actionbar height (if have) + root content height + virtual key height (if have)
So, if we get content height, do not rely on DisplayMetrics. I think get height from root content is the best solution.
Well, now is the real question: I want to get root content height immediately without delay (it means don't run in a Runnable). Any suggest?

Related

Dynamically change editTexts position based on screen Size / DPI

I would like to change the position of one element (editText) in my UI when the user taps it. The positioning should be ~20 dp under the actionBar, no matter screen-size or DPI the device is using.
How do I accomplish this using only (java)code? Please try to give a more exact answer. I have seen other examples similar to this but none that explains an exakt scenario? I was thinking of a start like the code below? Then what?:
DisplayMetrics dm = new DisplayMetrics();
mSearchField = (EditText)v.findViewById (R.id.something);
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
int [] loc = new int[2];
mSearchField.getLocationOnScreen(loc);
int distance = dm.heightPixels - loc[1];
Get the Display height and width using DisplayMetrics.
DisplayMetrics displayMetrics = new DisplayMetrics();
mContext.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int height = displayMetrics.heightPixels;
int width = displayMetrics.widthPixels;
Log.e("width",width+""+height);

Screen height - Android

In an Android Application, I want to know the height and the width of the screen. I'm using the following code:
Point size = new Point();
display.getSize(size);
screenWidth = size.x;
screenHeight = size.y;
The problem is than when the soft navigation bar exists, the returned height = the actual height - navigation bar height.
I want to get the total value.
Note: I'm using Android SDK 13 (v 3.2).
Check this, it may help:
FrameLayout root = (FrameLayout) findViewById(R.id.root);
root.post(new Runnable() {
public void run() {
Rect rect = new Rect();
Window win = getWindow(); // Get the Window
win.getDecorView().getWindowVisibleDisplayFrame(rect);
// Get the height of Status Bar
int statusBarHeight = rect.top;
// Get the height occupied by the decoration contents
int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop();
// Calculate titleBarHeight by deducting statusBarHeight from contentViewTop
int titleBarHeight = contentViewTop - statusBarHeight;
// By now we got the height of titleBar & statusBar , Now lets get the screen size
int screenHeight = dm.heightPixels;
// Now calculate the height that our layout can be set If you know that your application doesn't have statusBar added, then don't add here also.
//Same applies to application bar also
int layoutHeight = screenHeight - (titleBarHeight + statusBarHeight);
}
});
You can use
display.getRealSize(size); instead of display.getSize(size);
source:
http://developer.android.com/reference/android/view/Display.html#getRealSize(android.graphics.Point)
you can use DisplayMetrics. This class holds information about the screen such its size, density, font scaling. For instance
getResources().getDisplayMetrics().widthPixels
returns
The absolute width of the display in pixels.
to retrieve the height
getResources().getDisplayMetrics().heightPixels
In onCreate method of your class write down this code, and you will get the actual width and height of screen:
Display mDisplay = this.getWindowManager().getDefaultDisplay();
final int width = mDisplay.getWidth();
final int height = mDisplay.getHeight();
System.out.println("width " + width + " height " + height);

How to use the taken width and height in xml file in andorid?

I have identified the width and height of the screen programatically. Now I want to use that width and height in my xml file. Kindly give me a way to achive this.
Here is my Activity class.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DisplayMetrics display = this.getResources().getDisplayMetrics();
float width = display.widthPixels;
float height = display.heightPixels;
Toast.makeText(TestProjectActivity.this, "width and height is: "+width + ": "+height, Toast.LENGTH_LONG).show();
float w = convertPixelsToDp(width, this);
float h = convertPixelsToDp(height, this);
Toast.makeText(TestProjectActivity.this, "DP" +w+": "+h, Toast.LENGTH_LONG).show();
}
public static float convertPixelsToDp(float px,Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float dp = px / (metrics.densityDpi / 160f);
return dp;
}
Now I want to send the w and b to the xml for the further work.
Any help would be appricieted?
Thanks.
You cannot pass information back to XML. At most, you can get your widget using findViewById and then programmatically set the widget size.
I am developing the application which runs on any screen device. So the issue with the screen size. If I am setting the width of textview to 200dp, then its ok in my device but on other device,its not getting appear. So I just want a way where I can use this above detected width and height in my XML
You should then use a LinearLayout to give your image a size relative to the available width (like a percentage of the width of the screen
Here is how your XML would look (pseudo code)
LinearLayout (layout_width: match_parent, layout_height: wrap_content)
ImageView (layout_width: 0dip, layout_height: wrap_content, layout_weight: 50)
ImageView (layout_width: 0dip, layout_height: wrap_content, layout_weight: 50)
In the above example, each image view will take 50% of the screen width, no matter the number of pixels.
I think you need to Google a good tutorial about layouts in Android.

Android: Get Screen size for the root Layout only

Please get me correctly over here :
I want to get the height/width of the space available to the Activity/Layout in onCreate() method to calculate the height that can be given to child layouts. I can get the screen size using :
root = (LinearLayout) findViewById(R.id.mainroot); // Main layout of LinearLayout
android.view.Display display = getWindowManager().getDefaultDisplay();
int height = Display.getHeight(); // I know this is deprecated have hence used
int width = Display.getWidth(); // DisplayMetrics
int childWidth, childHeight;
DisplayMetrics metrics = new DisplayMetrics();
display.getMetrics(metrics);
//int density = metrics.densityDpi;
height = metrics.heightPixels; //480
width = metrics.widthPixels; //320
This both the methods/ways gives me same height and width i.e. size of full screen. What I am looking for is to get actual height that is avaialbe for the layout after deduction of Application Title, Status Bar, etc.
Any idea how to get this. OR to get the sizes of titles, etc - what all should be counted over here. On emulator I see 2 bars on top - 1 must be application titile what an be the other one. I can get heights of them all and deduct from screen height.
ONE more point : In this case I will be setting the height programamtically so it will be pixel based (as I can setheight in pixels only I guess) will that affect in density factor with differnet screen sizes. What can be a way to calculate height (lets say 50%) for child layout that will be same for any density o so.
SOLUTION :
In my onCreate(), I added the following lines :
setContentView(R.layout.mainpage);
root = (LinearLayout) findViewById(R.id.mainroot);
root.post(new Runnable() {
public void run() {
Rect rect = new Rect();
Window win = getWindow();
win.getDecorView().getWindowVisibleDisplayFrame(rect);
int statusHeight = rect.top;
int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop();
titleHeight = contentViewTop - statusHeight;
Log.i(Utility.TAG, "titleHeight = " + titleHeight + " statusHeight = " + statusHeight + " contentViewTop = " + contentViewTop);
// CALCULATE THE SIZE OF INNER LAYOUTS
calculateChildSize();
}
});
With the above code, I get the values of titleBar & statusBar. On deducting it from metrics.heightPixels; I get the required height of the screen.
Good Point is this code works for all density's.
Hope this helps others too.
FOR IMPROVEMENT : I have to do similar calculations for all Activities in my application, so was thinking about writing this code only once. I can save teh titleHeight to a static variable so can use in all activities.
BUT
Can the user change the phone's density at runtime.
If so, then the Activity's onCreate will be called again or not ?
If not, then can I trap the density change event where I can add this code and make the current activity to refresh.
Any idea suggestions for improving is appreciated.
//Where rootView is the object for the root view of your application
final int viewWidt = rootView.getMeasuredWidth();
There is very easy method for that. doOnLayout() method is called as soon as layout is measured and ready:
rootView.doOnLayout {
rootView.getMeasuredWidth()
rootView.getMeasuredHeight()
}

How to get available height in landscape?

I have a layout with an imageview. This imageview should have a 4/3 ratio. In portrait mode i get the width of the screen then compute the height and set the imageView dimensions.
int[] tailles = new int[2];
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int widthScreen = dm.widthPixels;
tailles[0] = widthScreen;
tailles[1] = (tailles[0]*3)/4;
This part works great.
In landscape mode i need to set the imageView width according to the available height. So i need to get the height of the screen minus the status/notification bar.
I saw the value of 48px for the bar in this question. Is it a standard size ?
What the best solution to get the available height regardless of the used device ?
Display d = ((Activity) mContext).getWindowManager()
.getDefaultDisplay();
int h = d.getHeight();
int w = d.getWidth();
in portrait h > w and in landscape w > h
If you're creating an Activity, than you have to have some sort of layout. Get it's root component, and retrieve it's size (getWidth, getHeight). That'll be the available width and height without counting the status bar.

Categories

Resources