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)
Related
I am getting very confused on how to support all different Android screen sizes. I have already checked this answer, this site, the official documentations and many other answers but still I am confused so please don't mark this as Duplicate.
I already created layout-sw120dp, layout-sw160dp, layout-sw240dp, layout-sw320dp, layout-sw480dp (I only need to support up to 6 inches so I don't create layout-sw600dp).
What do I have to do is just have one layout and just copy it in each different folder so Android will do the selection on its own or I also need to change values in each layout?
What do I have to do is just have one layout and just copy it in each different folder or I also need to change values in each layout?
You start by deleting the layout-sw120dp, layout-sw160dp, layout-sw240dp, layout-sw320dp, and layout-sw480dp directories that you created. Or, at least, ignore them for now.
You then start implementing your UI, putting layout resources in res/layout/.
Then, for those layouts that need to be different in certain scenarios, you then create a directory representing the size when you want a different layout. Copy the layout from res/layout/ into the new directory, and then modify that copy to reflect whatever changes you want.
In other words, one copy of every layout is in res/layout/, and you override where needed with additional, modified copies in specific directories where you need the UI to change.
If you want to use the same layout for each and every screen density, you don't need to create different folders. Use just one simply called "layout" and the system will interpret it for every density. However, this could lead to strange layouts on certain physical devices depending on their screen size and density...
Another point you have to be aware of, if your application supports orientation changes, is that you have to design layouts for portrait and lanscape orientations. This is done by duplicating a folder used for a density and add "-port" or "-land" to inform the systen which one must be used according to the actual orientation of the device your app is currently running on.
If you want to precisely define your app look and feel, you have to customize your layout for each density. And if you use bitmaps, you will have to customize them either (for example, your app icon should be defined with different sizes to keep a good looking for each screen density). Just as for the layout, you have to create "drawable-..." folders to contain the corresponding versions of your bitmaps.
This is an answer that's been an issue from old ages and for which you'll see lot many answers but which is not a one fit all type still. What I did come up with though when faced with the same issue was to use PercentRelativeLayout. This is a relatively new feature that was started from Android support version 23.0 (android sdk manager), and one of the big game changers according to me, since it allowed
developers to specify their blocks relative to the layout size Percentage-wise. And since it is in terms of percent, this fits all screen sizes with varying dimensions, but while maintaining the ratio.
Ofcourse this method involves some trial and error and a lot of experimenting, but I found this method to be the easiest to implement and which took out the headache of maintaining the dimensions for various screen sizes.
Few tutorials to get you started :
https://www.tutorialspoint.com/android/android_relative_layout.htm
https://guides.codepath.com/android/Constructing-View-Layouts
I know you can have different UI elements by declaring different layout folders like layout-large or layout-xlarge. However, I don't want to have to update two separate files every single time I make a change to my apps interface.
Is there any other way to have a button that only has visibility="gone" on small screens and normal visibility on large screens?
I separated the button into 2 XML files - one in layout-large that has visibility="visible" and one in layout that has visibility="gone", and then include the button in my layout/home.xml file. It worked.
You can separate it into two XML layout files but then you have to maintain both. You can abstract it out by having one layout file and then give the button a style that's defined for both tablets and not.
I would set it to GONE and change it in code. That way you don't have two layout files.
However, I would not use Blumer's method of getConfiguration().screenLayout. Buckets are no longer the best way to handle different screen sizes. Dianne Hackborn explains why in a post, but it boils down to:
Based on developers’ experience so far, we’re not convinced that this
limited set of screen-size buckets gives developers everything they
need in adapting to the increasing variety of Android-device shapes
and sizes. The primary problem is that the borders between the buckets
may not always correspond to either devices available to consumers or
to the particular needs of apps.
Instead, you should use their new numeric values--roughly sw600dp for 7" tablets and sw720dp for 10":
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
if (getResources().getBoolean(R.bool.sw600dp)) {
((Buton)findViewById(R.id.mybutton)).setVisibility(VISIBLE);
}
}
I am new to android and i am trying to create a single layout for different screen size.Is it possible or am i want to create different layout for different screens
Of course you can only provide the layout in res/layout, but it might look ugly on other screen sizes, even though (or because) Android tries to scale it.
It is not possible to define layouts for different reolutions in a single file. It's just possible to design the layout to be dynamic so Android can adjust it to the given resolution as good as possible.
I am developing an application for whole android devices. But resolation of screens are different and that is the biggest problem how it looks. So, I want to make resizing controls and also I used absolutelayout but It is still same.. I give value to controls as dp ..
How can I solve this problem ?
You don't resize the screen of an android device - you make your app instead work with the various screen sizes.
The relevant docs are here.
You cannot hardcode the dimensions of your layout and expect it to work on every screen size. And there is no method which automatically does it unless you write it.
You might want to change your approach, use Relative Layout or Linear Layout instead and use values like fill_parent and wrap_content while designing your layout.
Another approach Android developers follow is use different resource files for different screen sizes and Android loads them automatically at runtime.
Refer to this for more info on how to work with different screen sizes effectively.
I am creating a simple game in android so I can create something and learn how to program in Android (I'm a noob).
Right now in my layout editor (i think thats what its called, basically the place where you can create your layout xml files) there are many sizes on the top left... which one should i target? do i need to make a separate layout for each one of them?
Thanks!
R
The screen size selection is only intended to give you an impression of what the layout looks like on various screen sizes and densities. A good place to get started is Common Layout Objects and Supporting Multiple Screens.
When developing for Android, you should not target a specific screen size, but instead make layout elements fit proportionally. An exception may be x-large displays such as tablets, for which a great read is Distributing to Specific Screens. An example of getting elements to position nicely is this question.