3

Tell me how to programmatically assign a position to a button in android using linear layout. By default it takes the xtreme left position of the screen. Also i want to do it using Linear layout please do not suggest me using Relative layout. Following is my code

buttons_and_edittext=new LinearLayout(HelloAugmentedWorldActivity.this);
buttons_and_edittext = (LinearLayout)findViewById(R.id.linearLayout1);
buttons_and_edittextParameters = new LinearLayout.LayoutParams(120, 150);

button3 = new Button(this);
button3.setText("log");

buttons_and_edittext.addView(button3,      
buttons_and_edittextParameters);

any help will be appreciated thanks

MBMJ
  • 5,323
  • 8
  • 32
  • 51
Joyson
  • 1,643
  • 5
  • 28
  • 48
  • refer the previous question http://stackoverflow.com/questions/2481455/set-margins-in-a-linearlayout-programmatically – Aerrow Jul 04 '12 at 09:41
  • Thank u very much. I only had to use buttons_and_edittextParameters.setMargins(30, 20, 30, 0); – Joyson Jul 04 '12 at 09:53
  • cau u suggest me a site wer i can get good tutorials on using Relative layout i sum what feel uneasy to use relative layout as i am new to android. – Joyson Jul 04 '12 at 09:55

2 Answers2

3

Please refer below link for add buttons into relative layout at fix position.

Android User Interface Design: Relative Layout

And Use Below Code also for that.

Button but1 = new Button(this);  
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);  
but1.setLayoutParams(params2);  
but1.setText("Press Here!");  
// give the button an id that we know  
but1.setId(1001);
layout1.addView(but1);
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
0

After a long time I got a perfect solution to set button and image position programmatically in android studio with the help of relative layout params by following these steps - check out this link.