Get Button Coordinates

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

Get Button Coordinates

Postby matt46 » Mon Jan 12, 2009 2:13 pm

Hello guys!

I have a little problem and I don't know how to solve this:

I created a Button directly in java, like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Button button1 = new Button(this);
  3.  
  4. bouton1.setText("1");
  5.  
  6.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Then I added it to a view, like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. AbsoluteLayout myview = new AbsoluteLayout(this);
  3.  
  4. myview.addView(button1, new AbsoluteLayout.LayoutParams( 60, 60, 100, 100));
  5.  
  6. setContentView (myview);
  7.  
  8.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


This button appears onscreen, no problem.

But I need to use a function to get button coordinates:

I tried to do it:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. int coordinate = button1.getTop();
  3.  
  4.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


But coordinate is always 0, dans it's the same thing if I try button1.getBottom(), button1.getRight or button1.getLeft.

Do you have any idea?

Regards! :wink:
matt46
Freshman
Freshman
 
Posts: 6
Joined: Sat Nov 29, 2008 8:11 pm

Top

Postby Emmanuel7 » Tue Jan 13, 2009 3:07 am

If you want the absolute position, you can use GetLocationOnScreen.

this is what I'm doing :
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. int[] location = new int[2];
  3.  
  4. MyButton.getLocationOnScreen(location);
  5.  
  6.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


And it works like a charm !

GetTop and getLeft should give you coordinate relative to your parent ( the absolute layout in your case ).
It's still strange that getBottom and getRight return 0.

Do you try that in the OnCreate ?
I think the layout does not really exist at this point, so coordinates are not set yet.
(but testing it when you push the button, for instance, should be OK )
Emmanuel7
Senior Developer
Senior Developer
 
Posts: 164
Joined: Fri Dec 12, 2008 2:17 am
Location: Paris

Postby matt46 » Sun Jan 18, 2009 2:23 pm

Thanks for the reply!

Yes, I use it in the OnCreate method. That's why it doesn't work.

Thanks for the solution!

Regards :)
matt46
Freshman
Freshman
 
Posts: 6
Joined: Sat Nov 29, 2008 8:11 pm

Postby Emmanuel7 » Sun Jan 18, 2009 3:10 pm

Glad to hear that was the solution !

Also, if you need it at the construction of your activity, as you can't do it in the 'OnCreate' method, you should use the 'onFinishInflate' method, that is called after the layout is really created !
Emmanuel7
Senior Developer
Senior Developer
 
Posts: 164
Joined: Fri Dec 12, 2008 2:17 am
Location: Paris

Postby matt46 » Sun Jan 18, 2009 5:04 pm

Thanks for your help!!
matt46
Freshman
Freshman
 
Posts: 6
Joined: Sat Nov 29, 2008 8:11 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 8 guests