Reputation: 11999
I have a parent linear layout.
I need to put three buttons at the bottom of screen horizontally aligned not from XML but through Java code.
Button 1 should be at left of screen
Button 2 should be at bottom of screen
Button 3 should be at right of screen
This is the layout need to be designed:
What I understand is I have to add a relative layout to my parent layout. And some rules for the button.
This is what I tried
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
l=(LinearLayout)findViewById(R.id.mainl);
rl=new RelativeLayout(this);
b1=new Button(this);
b2=new Button(this);
b3=new Button(this);
b1.setText("Button 1");
b2.setText("Button 2");
b3.setText("Button 3");
rl.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rl.addView(b1);
rl.addView(b2);
rl.addView(b3);
l.addView(rl);
}
And also the buttons are not coming at bottom. They are coming at top of screen.
Activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainl"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
Upvotes: 2
Views: 320
Reputation: 1575
I have created layout programmatically use this code:
public class MainActivity extends Activity {
private LinearLayout layout;
private RelativeLayout lay;
private Button btn1, btn2, btn3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
layout = (LinearLayout)findViewById(R.id.ff);
lay = new RelativeLayout(this);
RelativeLayout.LayoutParams layout_params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
layout_params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lay.setLayoutParams(layout_params);
//lay.setLayoutParams(layout_params);
btn1 = new Button(this);
btn1.setText("ButtonA");
btn2 = new Button(this);
btn2.setText("ButtonB");
btn3 = new Button(this);
btn3.setText("ButtonC");
RelativeLayout.LayoutParams button_params1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
button_params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
button_params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
btn1.setLayoutParams(button_params1);
RelativeLayout.LayoutParams button_params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
button_params2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
button_params2.addRule(RelativeLayout.CENTER_HORIZONTAL);
btn2.setLayoutParams(button_params2);
RelativeLayout.LayoutParams button_params3 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
button_params3.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
button_params3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
btn3.setLayoutParams(button_params3);
lay.addView(btn1);
lay.addView(btn2);
lay.addView(btn3);
layout.addView(lay);
}
}
Upvotes: 0
Reputation: 2188
Try this one: XML file :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
android:orientation="vertical"
tools:context=".MainActivity" >
</LinearLayout>
And in JAVA file :
LinearLayout main = (LinearLayout) findViewById(R.id.main);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
android.widget.RelativeLayout.LayoutParams.MATCH_PARENT,
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout ll = new RelativeLayout(getBaseContext());
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
params3.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
Button btn1 = new Button(getBaseContext());
Button btn2 = new Button(getBaseContext());
Button btn3 = new Button(getBaseContext());
btn1.setText("One");
btn2.setText("Two");
btn3.setText("Three");
ll.addView(btn1, params1);
ll.addView(btn2, params2);
ll.addView(btn3, params3);
main.addView(ll, params);
Hope this will help you.
Upvotes: 0
Reputation: 2005
Try this
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.BOTTOM;
rl.setLayoutParams(layoutParams);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(
(int) LayoutParams.WRAP_CONTENT,
(int) LayoutParams.WRAP_CONTENT);
params3.addRule(RelativeLayout.CENTER_HORIZONTAL);
rl.addView(b1, params1);
rl.addView(b2, params3);
rl.addView(b3, params2);
l.addView(rl);
Upvotes: 0
Reputation: 6533
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
rl.addView(b1, params);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
rl.addView(b3, params1);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams
( (int) LayoutParams.WRAP_CONTENT, (int) LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.CENTER_HORIZONTAL);
rl.addView(b2, params2);
LinearLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams) txt1.getLayoutParams();
layoutParams.addRule(LinearLayout.BOTTOM, 1);
rl.setLayoutParams(layoutParams);
Upvotes: 1
Reputation: 128428
For adding button to RelativeLayout run time, you have to pass params values.
Try for placing button b1
on left:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
rl.addView(b1, params);
Upvotes: 1