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,133
  • 8
  • 31
  • 51
Joyson
  • 1,573
  • 5
  • 27
  • 47

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,009
  • 18
  • 75
  • 127
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.