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);
}
}
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 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 read something about supporting multiple screen sizes in Android.
For that most of the answers recommended to create different layouts
(layout-normal, layout-small, and so on). But that would mean that
I have to define all my layouts multiple times. I don't think that
this is a smart solution.
Is there no way to create just one layout and to automatically scale
it for any display size?
There are several parameters in layouts in android that adapt themselves to the size of the screen (fill_parent, center...), however I have not seen in android's layout parameters for everything... for example 1/3 of width.
Finally what I am doing is the following:
One layout per architecture of the interface. For example, in tablets I change the architecture, integrating sometimes 2 layouts in one.
Include, programmatically, changes of sizes. For example, an button I want to be 1/3 of the screen I need to programmatically obtain the width of the screen and change the button size.
Always include several sizes of icons that android automatically selects depending on resolution.
But there is a significant part of the job that needs to be done "by hand".
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 a simple game part of application where i need to create a game using lots of images of different different shapes, imagine ice breaker game where i need to break ice using hammer.
So when i think of setting layout with different types of ice images and that too which support all different densities,i get stuck and not able to understand how i set my layout, whether with Relative / Linear Layout so that it give me same result in all size of android devices except tablets.
I hope you understood the problem, support of all density, around 500-600 images with different shapes, which layout to use and one more thing .apk size is also big matter for me as there are other lots of images other than for this game.
Please read the Screen Supports for the better understanding.
Use configuration qualifiers as explain in that Document for the Different Layout. And Also put the XML file with in that layout drawable directory.
that help me. So it will be also helpful to you.
Be free to comment if you have any dought.
Edited
You can set the layout for the different screen support as like below:
See the Image Below: