I installed the ADT plugin for Android for Eclipse, and I don’t like it.
I would like use my own Grid Layout the way I want and design windows by code (the same way as JFrame in Java, because I already know how to do this) not by click and drag.
How to do this in Android?
While I don’t think it’s a great idea to write the UI code in java, here’s how you would do it.
First, you’ll want to look at the
android.view.*and theandroid.widget.*packages in the Android Reference docs.Everything the the Xml corresponds to a
View(or Subclass). So, if you seeLinearLayoutin the Xml, then there will be a LinearLayout java component as well.So, in your
Activity'sonCreate()method, where normally you’d just set the content resource xml view, you can instantiate a Widget and set it directly.You might also need to the set the
LayoutPararmsas well, since most widgets need to know if they should wrap content or fill the parent.Many of the Layout widgets are analogous to Swing Containers, ie, they accept children, so you can call addChild() with another view to create a widget hierarchy.
This should be enough to get you started.
As a final note, I’d like to reiterate that while you can build the complete UI in Java code, the Xml layout does offer some other benefits. For example the Xml layouts allow you to support multiple screen sizes, or different layouts (landscape vs portrait, etc), which is much harder to do (but not impossible) in Java. Also, you might be tempted to think that because the Views are in Xml, then they will be “slow” to be created on the device. But, Android optimizes the Xml resources, and in fact, I don’t think they are even Xml at the time they are compiled into your App. So the resource Xml files are very efficient, and writing direct Java code for the Views probably won’t get your much extra in terms of performance.