I'm aware that I can set different layouts for different screen sizes, but I would like to pull out the information of which one it has chosen. For example in the activity layout file for large tablets, I would like to add a view that scrolls through different statistical stuff, but on smaller phones this won't look good, and I'd rather have nothing there, as this will be a menu (i.e. fragments are overkill and will distract from the actual purpose)
So I would like to write some code which says something like:
if(xlarge) {
(manipulate stuff)
}
Is it possible to pull out the right constants from somewhere?
In your code, use findViewById to get a reference to the View that exists only in your large screen layout. If that reference == null then you know you are working with your small screen layout otherwise you know you've gotten hold of the large screen version.
If you really don't want to use Fragments for this, check out these links:
https://developer.android.com/reference/android/view/Display.html
https://developer.android.com/reference/android/util/DisplayMetrics.html
You could use the device's size like this:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
if (heightPixels > MIN_HEIGHT && widthPixels > MIN_WIDTH) {
manipulate stuff...
}
EDIT:
Just saw your comment to the original question. To get the screen size category, this question seems to answer it:
How to determine device screen size category (small, normal, large, xlarge) using code?
Related
I am new to Android. I am building my first application.
I have been trying to make it support multiple screens. I have gone through the developers section in android, but, I am not clear at these things.
What is the resolution, I should use for the different drawable folders?
(for example an hdpi screen may have 500*600 screen or 1280*1920 screen.)
I have been using layout folders as mentioned in the developers page, like small, normal and the rest, but android keeps on selecting normal layouts for screens like nexus 6p(for which I am guessing large would be appropriate).
Is there any way I could make it look better??
You have to write layout once but have to define dimensions dimens.xml for different different resolutions in app>res>values.
When you inflate your layout or setting your content view you can try making method on your application if you have base activities or base fragments
//At base activity/fragment
protected boolean isLargeLayout() {
return getResources().getBoolean(R.bool.large_layout);
}
//Then at your real activity/fragment just call
if(isLargeLayout()) {
//Set your layout knowing it's large
}else{
//Normal layout
}
Or use straight something like
if(getResources().getBoolean(R.bool.large_layout)) {
}
Or use dimens.xml with different dps (I do not prefer this method. I like android to choose which is large screen or not)
I'm working on an app that retrieves news articles from the web and displays them.
The article contains different parts, text parts, images etc. so what I'm doing is parsing the article, creating Views (TextView, ImageView etc) accordingingly and adding them to a LinearLayout.
Especially on a tablet in landscape mode, this doesnt look good, so first thing which comes to mind is makes it 2 columns.
My question is: is there a library or a good piece of code out there that can do some if the work that is entailed with that, like measuring the height if the views I have added, choosing a good place within the content from where to switch to the second column, take care that the right column doesn't get deeper than the left one etc?
Pic for explanation.
Sorry, I misread your question, you're building the layout programatically.
As seen in this answer you can get the window dimensions like this:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
You can then figure out a threshold that looks good to start using multiple columns and make your views percentages of those sizes you measure.
There is not such a library since it is just supported by android system, but not in the your way.
We use fragments and flexible layouts for different screen sizes, it simple and powerful.
Try to read the following page for details.
http://developer.android.com/guide/practices/tablets-and-handsets.html
So I'm trying to write a game for android, and I have a couple of questions regarding 'best practices' for android dev. The game I'm writing would have some dice on the top part of the screen, which the user should be able to drag around, and on the bottom half of the screen, I need to show a list of different numbers, updating as the dice are dropped into a new location. So, what's the best way to tackle this? I've coded up some sample code (which works) using a single view, and drawing the dice bitmaps and the numbers, but everything is so resolution-dependent that it bugs me. Would I gain anything by switching to an xml-defined view, and adding a dice view and a number-list view, and drawing those separately? Is there a standard or best practice that I should be following?
Thanks
It is a good practice to define a Layout in xml file so that your app runs on different screen sizes..
For more on why you should be using xml files, please refer to Android Design Guidelines.
When using xml file use dp for sizes and sp for text sizes, also use wrap_content and match_parent where ever possible.
You can also make density independent pixels in your code. Please see this..
public float typedDimension(int a){
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, (float) a,
getResources().getDisplayMetrics());
}
Firstly, SurfaceView is a good choice for game development - it will give you much better performance than a standard view.
Secondly, a nice technique I have seen used is to write to a standard size off-screen surface and then scale that bitmap to the size of the screen. When you have screens of different size, everything will still be in the correct place. Note you should scale down rather than scale up, so you might want to use different sized images (for mdpi, hdpi, xhdpi) to avoid using too much memory on lower devices.
When making apps for Android 3.x, you could basically just check if the device was in landscape to see if it could fit multiple fragments side by side. But this is only because you were basically guaranteed that the device is a tablet.
With the introduction of the compatibility library and android 4.0, I can't think of what I would consider as a 'good' way to determine if I can get away with side by side fragments or should revert to standard one fragment per 'screen.'
In the NewsReader example app, Google has a special values file for every common resolution, each with a Boolean value for whether the resolution should support the 2 fragment layout, but I think this way is poorly conceived. The only way I can think of is to check the size of the screen (to guess if it is a tablet or at least big enough to not ruin the layout), and then check the orientation.
So if anyone out there has an idea how to easily and efficiently check this, please let me know!
You can actually see the screen size type (small, normal, large, etc) by using the following:
getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK
That'll return an int that you can check against any of the following:
Configuration.SCREENLAYOUT_SIZE_SMALL
Configuration.SCREENLAYOUT_SIZE_NORMAL
Configuration.SCREENLAYOUT_SIZE_LARGE
Configuration.SCREENLAYOUT_SIZE_UNDEFINED
Then you can check for orientation, using
getResources().getConfiguration().orientation
Which will match any of the following possibilities
Configuration.ORIENTATION_PORTRAIT
Configuration.ORIENTATION_LANDSCAPE
Configuration.ORIENTATION_SQUARE
Configuration.ORIENTATION_UNDEFINED
With this you can essentially simulate the multiple views by adding your fragments as space allows based on screen size and orientation.
I create 2 layouts, one with one container (ViewGroup) for a fragment, and the second layout has 2 containers side by side. I use Android's folder structure for resources to specify that the scenarios where I want the multi-fragment (2 container) layout. Then in code, check for the existence of the second container to determine if you are multi-panel or not.
isMultipanel = false;
ViewGroup container2 = findViewById(R.id.container_2);
if (container2 != null) {
isMultipanel = true;
}
Try not to think of it as tablet or phone, but rather wide enough or not wide enough for 2 panels.
If I wanted multi-panel on normal size devices (<= 5") and large (5" - 7") only when they are in landscape, but always on xlarge (> 7"), I would put the multi-panel layout file in:
layout-land
layout-large-land
layout-xlarge
Then you don't have to do size checks in code since Android is already handling this for you.
I have a screen that I need to remove some items on smaller screens because they will not fit.
I search for items in the layout to populate with live data in code, such as populating names etc. There are 5 items that need to be populated in my large layout but only 2 in my small layout as three I removed because they won't fit. How in my Activity can I tell that I am in "Small mode" and not search the layout for the three id's that I want to skip and not populate?
Do I need to just search for them and catch the errors and ignore or is there a better way?
Thanks
It looks like in this circumstance the widgets will return null when I attempt to find them if the layout chosen doesn't support that widget. So I can just detect if its null and if so skip populating the widget.
You will want to utilize a combo of the resource directories (high, medium, low) and follow the guidelines on how to support multiple screen sizes from the android documentation. Also you can get the screen dimensions if you need to anything specific in the code. See these links.
Get screen dimensions in pixels
http://developer.android.com/guide/practices/screens_support.html