Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android custom layout

I spent sometime yesterday struggling with android layouts (... or better custom layouts). For general purposes forms the out of the box layouts that androids offers are more than enough. But when it comes to more complex use cases (let's take the example of the carousel with overlapping of images that are laid on an elliptical path) I quickly realized the limitation and complexity of android APIs. This was my approach:

  1. Using out of the box layouts is not an option (to many unmanageable issues to handle) , however the need of graphical objects that can be added to a container (let's say ImageView for instance) is necessary in order to easily perform translation, rotations, etc... through built in animation.

  2. The AbsoluteLayout seems the most reasonable layout available for the purpose. It allows to add derived view objects and write your own algorithm to position them. But... it is deprecated. That's a shame! Who wants to use deprecated apis ?

  3. The next most reasonable thing to do is to write your own layout sub-classing the mystic ViewGroup.. and here is where the all thing fall apart (at least it did for me). It is complicated, misleading and tricky to do it as much as it is to write your own custom layout for swing (I actually did it years ago, and I remember to be simpler).

  4. Then (driven by desperation) sub-classing a View, overriding the onDraw method and drawing directly on a Canvas seems the only straightforward solution (although this reminds me the days of java2D... long hours spent to achieve the high 1% of your project), but then you are giving up the handy built in animation APIs that you wish to apply to the graphics drawn on the canvas.

The conclusion is that, unless I have missed something, the current android architecture is a bit far from the programming paradigm offered by some other UI technologies such as flash or javafx (I can easily implement the above described carousel in javafx and flash). The reason is that, in javafx for instance, the Scene object (comparable to the View concept of android) offers by default an absolute layout. You can then apply other out of the box layouts if you want to. But for scenarios where a custom layout is necessary you don't have to do anything.

I am a little bit surprise that this option is not available in android, after all is a quite common need that is addressed by other ui technologies. I still like to think there is a 5th option in android that I haven't found. Any expert out there that could provide some information on the topic?

Thanks.

like image 405
sacchetto Avatar asked Jul 16 '10 19:07

sacchetto


People also ask

What is custom layout in Android?

Android provides a series of different layouts to suit your apps needs. One of the quickest and easiest ways to display information to users is via the ListView component. This component creates a simple scrollable region that can display unique sets of information.

Can you create custom views How?

Creating custom views. By extending the View class or one of its subclasses you can create your custom view. For drawing view use the onDraw() method. In this method you receive a Canvas object which allows you to perform drawing operations on it, e.g. draw lines, circle, text or bitmaps.

What does custom view mean?

A well-designed custom view is much like any other well-designed class. It encapsulates a specific set of functionality with an easy to use interface, it uses CPU and memory efficiently, and so on. In addition to being a well-designed class, though, a custom view should: Conform to Android standards.


1 Answers

I think that Android Layout are great for most use cases for interacting with users, BUT (a big But) the Layout mechanism is very delicate and is tricky to extend correctly. For small tweeks it's fine, but if you want to do something out of the ordinary (such as an animated carousel), you probably do free graphics anyway, so you don't need the extra complexity of extending Layout. It's just more constraints to live with, with no justifiable added value.

Most apps have some standard stuff (ActionBar, buttons, preferences, dialogs, etc), and one or more "unique" elements. So my suggestion is to use standard layout for the standard stuff, but for your unique elements use one of these:

1) SurfaceView - very easy to use. See the official Lunar Lander code example. To see SurfaceView in action, you can also take a look at my app video, which uses SurfaceView for the animated element, and everything is laid out with RelativeLayout.

2) opengl - It's the most powerful way to do graphics on Android. It runs on the GPU, so CPU can be free to do other things. And it's 3D (or 2D, if you just use one plane). I'm using libgdx, and it's great, and not difficult after a short learning.

Both SurfaceView and opengl (GLSurfaceView) are views, so they can be added to a layout as any other view

like image 164
Amir Uval Avatar answered Sep 23 '22 21:09

Amir Uval