Reputation: 948
I have a ListFragment that adds 2 views in the onActivityCreated() method - a MapView generic View (persistent header). I'd like the ListView to display underneath the MapView and header in portrait mode and on the right of the MapView and underneath the header in Landscape mode. I'd also like the MapView's height (in portrait) and width (in landscape) to be a percentage of the available window size (minus action bar, status bar and navigation bar - if one exists on the device). To accomplish this I believe I would need to get the window size and calculate the appropriate heights, widths and layout margins for all 3 views. I've tried a few things recommended in similar questions but the solution always returns the height/width of the device and not the window size of the Activity.
Any ideas. Thanks in advance.
Upvotes: 4
Views: 23336
Reputation: 948
I was able to solve this by using a ViewTreeObserver.
ViewTreeObserver vtObserver = view.getViewTreeObserver();
vtObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// check if the view's height and/or width > 0
// don't forget to remove the listener when done using removeOnGlobalLayoutListener
}
});
Upvotes: 4
Reputation: 53
Like others have suggested, I'd use different layouts per orientation, and just handle it differently:
if(getResources().getBoolean(R.bool.is_land)){
//Set up mapview on the side
} else{
//Add header to listfragment
}
Using a boolean in res/values-land
Upvotes: -1
Reputation: 1830
For the scaling you can achieve by doing the following:
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
LayoutParams params = new LayoutParams(metrics.widthPixels * desiredWidthPercentage, metrics.heightPixels * desiredHeightPercentage);
and then apply the params to the view you want.
For the different orientations I'd suggest using different layouts in the layout and layout-land folders or else dynamically adding rules as follows:
params.addRule(RelativeLayout.Above, ViewId);
to see all key words for rules see: http://developer.android.com/reference/android/widget/RelativeLayout.html
Upvotes: 8
Reputation: 415
I'm not sure the answer here is to get the screen dimensions but rather to use appropriate layouts for portrait and landscape that use the weight property to allocate the screen space between the different views.
I believe you can have a container layout that can host the other fragment layouts.
You can see some simple examples of using fragments inside of a layout xml file.
http://android-er.blogspot.com/2011/12/simple-exercise-of-fragment-inflate.html
and here
http://portabledroid.wordpress.com/2011/04/19/programmatic-and-layout-fragments/
Also, see this link for tips on providing layouts for different screen sizes: http://developer.android.com/guide/topics/resources/providing-resources.html
I hope this points you in the right direction.
Upvotes: 0
Reputation: 4252
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
Upvotes: 11