PDA

View Full Version : Absolut Layout



Gateway_Man
03-10-2011, 11:14 AM
Hi guys,

I have a question about Layouts in Android v2.2.
But first some information about the language I'm using.
Language: Java
IDE: Eclipse
I want to define my ui components in the code. I know thats nothing special and it works great until it comes to the locationsettings.
I want to define the location of my ui controls absolut.
But at these points i have some trouble, because each of the regular android controls I've tested doesn't support properties like setlocation() or things like setx() or sety().
It's really nessesary for me that i can do that in my code and not in that annoying xml "layout" file.

Greets
Gateway

alostpacket
03-10-2011, 11:58 AM
What are you trying to do exactly? A.ndroid is designed around relative positions and not absolute x and y.

It can be tough to get your head around at first but it's well worth it in the end.

Some must-reads:

1) http://developer.android.com/guide/topics/ui/layout-objects.html

2) http://developer.android.com/resources/tutorials/views/index.html

3) http://stackoverflow.com/questions/2025282/difference-of-px-dp-dip-and-sp-in-android

4) http://developer.android.com/guide/practices/screens_support.html


If you must set positions in code, you can use things like

ViewGroup.LayoutParams
http://developer.android.com/reference/android/view/ViewGroup.LayoutParams.html

and its subclasses like:

ViewGroup.MarginLayoutParams
http://developer.android.com/reference/android/view/ViewGroup.MarginLayoutParams.html

Where you can set marginLeft and marginTop (which are sort of like x and y)

Gateway_Man
03-11-2011, 03:14 AM
Hi,

I want to do it like I do it regular with java and swing.
I mean like this for example:




package

Main;

import



javax.swing.*;

public



class example {



/**

*




@param args


*/

//Controls
privatestatic JFrame win;
privatestatic JButton btn_OK;
privatestatic JButton btn_Cancel;
publicstaticvoid main(String[] args) {
// TODO Auto-generated method stub
init();
win.setVisible(true);
}
publicstaticvoid init(){
win = new JFrame("Window 1");
win.setLayout(null);
win.setSize(500, 500);
btn_OK = new JButton("Ok");
btn_OK.setSize(70, 25);
btn_OK.setLocation(win.getWidth() - 90, win.getHeight() - 55);
win.add(btn_OK);
btn_Cancel = new JButton("Cancel");
btn_Cancel.setSize(70, 25);
btn_Cancel.setLocation(win.getWidth() - btn_OK.getWidth() - 100, win.getHeight() - 55);
win.add(btn_Cancel);
}





}




It's much more comfortable for me as to define some words like left and right into a xml file. You know what I mean? I need something where I can calculate with.


Excuse me for my low englisch skills, I don't have to use it very often.


Greets
Gateway

alostpacket
03-21-2011, 12:06 AM
Well A.ndroid doesnt support swing, but you can write layouts in code, it's just not as common.

Read the links I posted and most of the XML attributes you can use to layout objects have an equivalent Java method you can use if you created the object in code.

Boniface
03-22-2011, 02:35 AM
I agree with alostpacket. Your approach is all wrong. You have to go with the flow, and learn to do things the Android way. You are not going to get anywhere by trying to impose a methodology that you have inherited from another platform.

Android layouts are a bit like CSS. Everything is done implicitly. It takes a while to get your head round it, but it is well worth it in the long run.