Should we avoid the size definition inside the xml? - android

Is there any reason to avoid having dps defined directly in the xml files?
E.g. is there any reason to prefer:
android:layout_marginLeft="#dimen/left_margin"
over this:
android:layout_marginLeft=16dp
If I understand correctly, this would make sense only for tablets but in that case won't we have a relevant xml in land with the relevant values?
Also if is there anything to be careful in regards to adding something to dimen.xml? E.g. should left_margin be defined in all dimen.xml for all dimension?

There's a good reason for using the first. Lets say that you decide on really big devices you want a bigger margin. With the first, you only have to override the dimens file and override the 1 dimension. In the second, you have to override the entire layout, which causes larger maintenance issues.
(Also you should almost always be using marginStart and marginEnd instead of left and right, so you handle RTL languages correctly).

One good reason would be that maybe you have a whole lot of layouts where marginLeft needs to be the same. You could set them all to 16dp right in the layout files, but what if you need to change that dimension? If you have 16dp defined in every layout, you'll have to change every single instance of it, and you might forget some. If it's defined in dimens you only have to change it once.
Another reason would be themers. Someone might make a theme for your app. It's a lot easier to override extracted values than having to override and copy an entire layout just to change one value.
And as Gabe says, you really should use start and end instead of left and right, whenever possible. Otherwise, your app will look terrible on devices using RTL.

Related

Android UI based on top and left property

I was working with Android UI in Eclipse and found it to be bit hectic. Designing layout using layout managers is bit time consuming. So i was wondering whether it is possible to specify the position of the UI elements based on (x,y) system i.e top and left property which is widely used in Visual Studio IDE for VB,C# etc ?
Positioning element based on top and left property would provide much flexibility.
How would that be flexible? Yes, doing layout correctly takes time, but if you do it right, it will scale properly to any screen size. If you're using X/Y coordinates, you will be hardcoding to a specific screen size, which is an especially bad idea on Android (as there are a multitude of screen sizes available).
If you need x, y positioning, you can use a FrameLayout with foregroundGravity set to top|left, and use layout_marginLeft for the x value, and layout_marginTop for the y value.
You can use AbsoluteLayout and suppress deprecation warnings in your code, but think of how will it look on different screen sizes?
I would advise to use RelativeLayout in your case.
As far as I know, there is no built-in layout that is based on (x, y) coordinates. You might be able to find 3rd party libraries that can do this for you. However, I'm skeptical that they will provide satisfactory results. Remember that Android is deployed on a wide variety of devices which include a range of different screen sizes and resolutions. This means that you can make the UI look pretty on one device using specific coordinates but it won't look very good on other devices.
Personally, I edit my UI layouts directly in the XML files. I find that this provides me better control than using the Eclipse UI editor. You still have to learn how the layout managers themselves work.
Android tries to ensure that your layout components are arranged nicely so that they:
don't overlap with each other
don't go off the screen space
look similar on different screen sizes
etc
It gives you nice XML Attributes to help you arrange your layout. I would recommend you use RelativeLayout for this application, because it allows you to put your layout components in positions RELATIVE to each other.
Some XML attributes you can specify are given here: Android Reference, RelativeLayout.LayoutParams

Why does margin override marginLeft in Android (same with radius, etc)?

This makes no sense to me, coming from CSS. In CSS, if you specify a margin and then margin-left, the left margin will assume the more granular value.
In Android, it is the opposite. Same goes for android:radius, and I'm sure other values.
My question is: why?.. It makes no sense. Is there a single reason for doing it this way?
Edit: prompted by trying to find a solution to yet another Google ADT/Android bug http://code.google.com/p/android/issues/detail?id=7588
I have had the same frustration, but if you think about it, which value should be used? I know in your post you say that CSS uses the "more granular value" but it all boils down to pixels in the end and the result is simply two pixel values that need to be chosen between. The CSS standard chose to do it one way, Android chose the other, I don't think either approach is wrong, they are just different.

Managing Android layouts, there must be a better way

I'm trying to sort the layout for one of my Android apps, but I'm finding layouts a nightmare.
Principally I have to have a portrait and landscape layout for normal, small and large screens. So thats 6 layouts to maintain to start with, let alone having to launch the app on three emulators each time because my UI widets don't load in the built in previewer.
I might be showing my ignorance as a fairly new developer, but there must be a better way!
Are there any tools out there to help with Android layouts?
Thanks
You dont need to have that many layouts. Design only as many as you need, and never use absolute values, aditionally try to make everything look nice using fill_parent and wrap_content for you layout_width & layout_height tags. Android does most of the work it self.
This article contains a lot of usefull info:
Supportng multiple screens
You may find this applicaiton helpful for designing your layouts:
http://www.droiddraw.org/
Also, if you don't specify a layout for each rotation, android will use one - infact it can use one for everything. If you use fixed values it makes it much harder. Try using fill_parent and wrap_content, you android will take care of scaling the view for each screen type and rotation too.
As a tip, don't forget to include:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
above your relative or linear layout, and:
</ScrollView>
at the end - that way if the view doesn't fit on the screen (ie too much content) it will allow the user to scroll.
Eclipse's built in layout "editor" shows a reasonably good example of what a layout looks like. I would assume you're writing your application in Eclipse. If not, I highly recommend it. While not perfect, it's the best environment I've found.
you just need to master the proper use of RelativeLayout's and LinearLayout's. Almost all of my Layouts will start with a Relative and have Linear nested inside.
I generally don't use LinearLayouts without having either height or width set to 0 and using the weight attribute to make everything the same size.

Does use of absolute layout in xml file really makes problem for using on different density emulator

i have used absolute layout in order to display image buttons in my application's main.xml.
how exactly it affects to using my app on different density screens..
Absolute Layout is deprecated and should not be used, it will make a mess of handling various screens.
Best practices for screen layouts
That is right, absolute layout will always make problem, that is why it is not at all recommended to use.
Please refer this Supporting Multiple Screens

Android View Design Issues

I've been playing about with the layout of a view with Android (lets say within the MainActivity) and I'm looking to create this sort of layout using 3 ImageView's (where each block represents an image):
This is rather easy to pull of using LinearLayout's but only if you specify the exact size and position of each ImageView. This then obviously causes problems when looking at different screen sizes.
Using AbsoluteLayout looked like it was going to work at first, but I've read that it's deprecated and it still causes problems with different screen sizes.
Then there is RelativeLayout, which I've tried using with DroidDraw, but it doesn't seem to get me very far when I implement it :(
So, does anyone have an ideas of how to achieve this?
EDIT: I've got close to doing this using dp instead of px but this still gets screwed up when using larger resolution devices! :(
Thanks
Romain Guy does something very similar using RelativeLayout. Android Layout Tricks #1
One solution is that you could use a TableLayout with 2 columns, and then in the second column embed a second TableLayout.
DroidDraw doesn't always show exactly how it will work when it runs 100% of the time I've noticed.
You can do this with a horizontal LinearLayout: add the green first, then add a vertical liner layout for the blue and orange and give each an appropriate weight (like 50 and 50).
You can still use LinearLayout but make the width/height in dip units. Thatvway it should look the same on any supported screen size. Alternativly, you could use the weight attribute instead which is probably a better idea for this case.

Categories

Resources