Placing An Image In The Center Of A Dialog.

The place to discuss anything and everything about running your LiveCode on Android

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
Googie85
Posts: 154
Joined: Tue Aug 05, 2014 10:07 am

Placing An Image In The Center Of A Dialog.

Post by Googie85 » Sun Dec 22, 2019 8:15 am

Hi Guys!!

I am trying to put a small image into the center of my dialog and am having some troubles. I have a fair idea that this is a simple progress, but having no luck. My code is as follows:

Code: Select all

on mouseUp

   put the width of this stack into width1
   put the height of this stack into height1

   divide width1 by 2
   divide height1 by 2

   set the center of image "Icon.png" to width1
end mouseUp
Any help would be greatly appreciated!!

Many Thanks,

Googie.

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2588
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Placing An Image In The Center Of A Dialog.

Post by jmburnod » Sun Dec 22, 2019 10:10 am

Hi,
1. "loc" instead "center" is the magic word in this case
2. Loc property needs two integer item x,y
3. Fortunately, LC converts a no integer to an integer in this case but that is a good idea to use trunc() or round() functions to convert it by script (if width or height of your stack is not divisible by 2 you get a non integer for x or y)

Code: Select all

on PlaceMyIMGToCEnter
   put the width of this stack into width1
   put the height of this stack into height1
   put trunc(width1/2) into width1
   put trunc(height1/2) into height1
   set the loc of image "Icon.png" to width1,height1
end PlaceMyIMGToCEnter
Best regards
Jean-Marc
Last edited by jmburnod on Sun Dec 22, 2019 12:42 pm, edited 1 time in total.

bogs
Posts: 5262
Joined: Sat Feb 25, 2017 10:45 pm

Re: Placing An Image In The Center Of A Dialog.

Post by bogs » Sun Dec 22, 2019 11:32 am

Hey there Googie85, a few things to note -

If you go to the Dictionary, you'll find there is no 'center' property, so that is problem 1 as Jean-Marc indicated.

In your original code snippet, you set up 2 variables, then did math on them but did not store the result. Your code -

Code: Select all

   put the width of this stack into width1
   put the height of this stack into height1

   divide width1 by 2 -- this is divided, but not stored...
   divide height1 by 2 -- this is divided, but not stored...
You also had :
set the center of image "Icon.png" to width1
Width 1 would be the width of the stack for the reason noted above, and so only 1 coordinate, so even if 'the center' was a property, you would have given it incomplete information for it to actually work.

Jean-Marc's example stored the result back into width1 and width2.

Lastly, I would point out that you really don't need to go through all of that to center an image if your using 'loc', this would work just as well -
<I am assuming your dialog box is a custom one you made up, but that shouldn't matter. I made a stack with 2 buttons, and an image, and used this for the code >

Code: Select all

# button "Center"
on mouseUp
   set the loc of image "info" to the loc of this card
   --I used "this card" because for this example, there is only 1 stack and card, 
   -- however, you could just as easily use the name or long id of the card...
end mouseUp
	
# button "Reset"
on mouseUp
   set the loc of image "info" to 20,20
end mouseUp
And here is the stack -
centerMe.zip
A centering example...
(1.1 KiB) Downloaded 63 times
Image

Post Reply

Return to “Android Deployment”