How to set set location of buttons programatically

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

How to set set location of buttons programatically

Postby rockerboy » Sat Nov 28, 2009 2:03 am

Hi,

How do I set the location of buttons/other views programmatically? Below is my program snippet in the onCreate() method of my activity. Any help please?

//Create two buttons
Button button1 = new Button(this);
Button button2 = new Button(this);

//Create layout params. Here, don't know how to
//set the location (relative location or X ,Y)
LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

button1.setLayoutParams(params);
button1.setText("Click this first");

button2.setLayoutParams(params);
button2.setText("Click this next");

//Create relativelayout
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams layoutparams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT,10,10);

//Add buttons to layout
layout.addView(button1, layoutparams);
layout.addView(button2, layoutparams);
//Set layout as content of activity
setContentView(layout);
rockerboy
Freshman
Freshman
 
Posts: 8
Joined: Sat Nov 14, 2009 3:27 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 7 guests