0

I'm thinking of making a small android game like Mole Catch but if i want the mole to jump around randomly on the screen as a button i would need to make the button change location in the java code and not in the XML and im not realy sure how i would change this location.. Like if i create the button in xml and then the buttons position will still be set in the xml file not in java.

So is there some way i can instead of setting position of button in XML then set the position in Java for this kind of game / application?

4 Answers 4

1

You could use the method offsetLeftAndRight and offsetTopAndBottom

Sign up to request clarification or add additional context in comments.

Comments

1

Hi TheComppBoy in your case my suggestion is don't create your button in xml file. Make it dynamic and add it into the any layout(possibly in relative layout which is define in xml file) .

and as your mole is jump change the button gravity as required.

it may help you.

Comments

1

Use AbsoluteLayout (It is less flexible and harder to maintain.) and specify exact location (x and y).

Comments

1

Use AbsoluteLayout with layout_x, layout_y to set initial position and set layoutParams to update position.


public AbsoluteLayout.LayoutParams (int width, int height, int x, int y)

Creates a new set of layout parameters with the specified width, height and location. Parameters

width   the width, either MATCH_PARENT, WRAP_CONTENT or a fixed size in pixels
height  the height, either MATCH_PARENT, WRAP_CONTENT or a fixed size in pixels
x   the X location of the child
y   the Y location of the child

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.