setting Layout positions for view components in code?

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

setting Layout positions for view components in code?

Postby Andy » Thu Jan 15, 2009 1:10 pm

Hi ,

is there any way to set the positions (X,Y) parameters for the views added using code,

Just a simple example java code for layout and a single View(Textview)

LinearLayout lLayout = new LinearLayout(getApplicationContext());
lLayout.setBackgroundColor(color.white);
lLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
lLayout.setOrientation(LinearLayout.VERTICAL);

TextView Welcome = new TextView(getApplicationContext());
Welcome.setText("Welcome to TBank");
Welcome.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10f);
Welcome.setHighlightColor(color.background_light);
Welcome.setLayoutParams(new LayoutParams(LayoutParams., LayoutParams.WRAP_CONTENT));

lLayout.addView(Welcome);

I know in case of XML we can use android:layout_x & y attribute .....(may be only for
Absolute Layout).

can anyone tell the code replacement for this positioning using X,Y co-ordinates( for any type of Layout)

Regards,
Andy
A n D r O i D
Andy
Junior Developer
Junior Developer
 
Posts: 13
Joined: Fri Dec 19, 2008 2:53 pm

Top

Postby c4r1o5 » Tue Jan 27, 2009 8:47 pm

I think that the only way is doing something like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Welcome.setLayoutParams(new AbsoluteLayout.LayoutParams(
  3.  
  4. AbsoluteLayout.LayoutParams.WRAP_CONTENT,
  5.  
  6. AbsoluteLayout.LayoutParams.WRAP_CONTENT,
  7.  
  8. <yourNewX>,
  9.  
  10. <yourNewY>));
  11.  
  12.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4



But you must use absoluteLayout instead LinearLayout (not sure).
c4r1o5
Freshman
Freshman
 
Posts: 3
Joined: Tue Jan 27, 2009 8:07 pm

Postby hebbalhrk » Tue Feb 03, 2009 9:28 am

first create layout parameters
LayoutParams layoutParams = new LayoutParams(100,150,20,50);

Assign the params to the view
welcome.setLayoutParams(layoutParams);

To set new position for the view just say:
layoutParams.x = new x value..;
layoutParams.y = new y value..;
hebbalhrk
Developer
Developer
 
Posts: 37
Joined: Wed Jun 18, 2008 10:35 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 3 guests