1

How can we add buttons at dynamic positions in layout or using canvas, not in table layout?

Can anyone please help me on this?

Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
user1285293
  • 108
  • 1
  • 9
  • Implement onTouch event constructor . [1]: http://stackoverflow.com/questions/3683727/layout-with-dynamic-position – Mudassir Hasan Sep 21 '12 at 05:18
  • Hi user1285293 have you checked my answer? Is it what your are looking for? If not tell me what you need and I try to provide a better answer. If the answer is correct you can mark it as it. – Bruno Bieri Sep 22 '12 at 16:52
  • It would be nice if you accept the answer or tell me what you need. – Bruno Bieri Sep 24 '12 at 05:47
  • Hey user1285293 can you tell me if the answer I provided fulfull your requirements? If not tell me what you need. If so you can mark the answer as correct. – Bruno Bieri Sep 25 '12 at 05:20
  • 1
    Hi user1285293 please tell me if the answer works for you. If so you can accept the answer otherwise tell me what oyou need. – Bruno Bieri Oct 04 '12 at 05:23
  • Can you please say me if something is missing in my answer. If the answer is OK you can mark it as correct otherwise I try to correct it. – Bruno Bieri Oct 06 '12 at 06:17

1 Answers1

7

Use RelativeLayout to position your controls where you like them. Have a look at this link: Dynamic TextView in Relative layout

and here How to create a RelativeLayout programmatically with two buttons one on top of the other?

If you like to achieve it within XML only. Look here: http://www.mkyong.com/android/android-relativelayout-example/

Here an example how you could use the RelativeLayout to position your controls dynamically:

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Creating a new RelativeLayout
        RelativeLayout mainRelativeLayout = new RelativeLayout(this);

        // Defining the RelativeLayout layout parameters with Fill_Parent
        RelativeLayout.LayoutParams relativeLayoutParameters = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);

        // Creating a new Left Button
        Button buttonLeft = new Button(this);
        buttonLeft.setText("Left");

        // Creating a new Left Button with Margin
        Button buttonLeftWithMargin = new Button(this);
        buttonLeftWithMargin.setText("Left with Margin");

        // Creating a new Center Button
        Button buttonCenterParent = new Button(this);
        buttonCenterParent.setText("Center");

        // Creating a new Bottom Button
        Button buttonBottom = new Button(this);
        buttonBottom.setText("Bottom");

        // Add a Layout to the Buttons
        AddButtonLayout(buttonLeft, RelativeLayout.ALIGN_PARENT_LEFT);
        AddButtonLayout(buttonCenterParent, RelativeLayout.CENTER_IN_PARENT);
        AddButtonLayout(buttonBottom, RelativeLayout.ALIGN_PARENT_BOTTOM);

        // Add a Layout to the Button with Margin
        AddButtonLayout(buttonLeftWithMargin, RelativeLayout.ALIGN_PARENT_LEFT, 30, 80, 0, 0);

        // Add the Buttons to the View
        mainRelativeLayout.addView(buttonLeft);
        mainRelativeLayout.addView(buttonCenterParent);
        mainRelativeLayout.addView(buttonBottom);
        mainRelativeLayout.addView(buttonLeftWithMargin);

        // Setting the RelativeLayout as our content view
        setContentView(mainRelativeLayout, relativeLayoutParameters);
    }

    private void AddButtonLayout(Button button, int centerInParent, int marginLeft, int marginTop, int marginRight, int marginBottom) {
        // Defining the layout parameters of the Button
        RelativeLayout.LayoutParams buttonLayoutParameters = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        // Add Margin to the LayoutParameters
        buttonLayoutParameters.setMargins(marginLeft, marginTop, marginRight, marginBottom);

        // Add Rule to Layout
        buttonLayoutParameters.addRule(centerInParent);

        // Setting the parameters on the Button
        button.setLayoutParams(buttonLayoutParameters);     
    }

    private void AddButtonLayout(Button button, int centerInParent) {
        // Just call the other AddButtonLayout Method with Margin 0
        AddButtonLayout(button, centerInParent, 0 ,0 ,0 ,0);
    }
}

And you should get something like this:

enter image description here

Community
  • 1
  • 1
Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
  • I think you got downvote because the link does thing in xml not in activity dynamic. So give demo of creating and placing view dynamic and you might get accepted + upvoted :) – MKJParekh Sep 21 '12 at 05:17
  • 1
    Okay. I gave your +1.. But still you need to do one more thing..Show some methods that the OP can use to place it anywhere in layout like top..bottom..center..padding ..margin... Whenever you get time you can edit the answer..and I believe that this can be a NICE ANSWER..! – MKJParekh Sep 21 '12 at 06:15
  • Thanks for pushing me to give a better answer – Bruno Bieri Sep 21 '12 at 06:52