Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

programmatically assign position to a button in android

Tags:

android

button

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

like image 829
Joyson Avatar asked May 19 '26 16:05

Joyson


1 Answers

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);
like image 152
Dipak Keshariya Avatar answered May 22 '26 08:05

Dipak Keshariya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!