How to set RelativeLayout layout params in code not in xml?
Just a basic example:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);Button button1;button1.setLayoutParams(params);params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);params.addRule(RelativeLayout.RIGHT_OF, button1.getId());Button button2;button2.setLayoutParams(params);
As you can see, this is what you have to do:
- Create a
RelativeLayout.LayoutParams
object. - Use
addRule(int)
oraddRule(int, int)
to set the rules. The first method is used to add rules that don't require values. - Set the parameters to the view (in this case, to each button).
RelativeLayout layout = new RelativeLayout(this); RelativeLayout.LayoutParams labelLayoutParams = new RelativeLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); layout.setLayoutParams(labelLayoutParams); // If you want to add some controls in this Relative Layout labelLayoutParams = new RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); labelLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT); ImageView mImage = new ImageView(this); mImage.setBackgroundResource(R.drawable.popupnew_bg); layout.addView(mImage,labelLayoutParams); setContentView(layout);
Something like this..
RelativeLayout linearLayout = (RelativeLayout) findViewById(R.id.widget43); // ListView listView = (ListView) findViewById(R.id.ListView01); LayoutInflater inflater = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE); // View footer = inflater.inflate(R.layout.footer, null); View footer = LayoutInflater.from(this).inflate(R.layout.footer, null); final RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT); layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);footer.setLayoutParams(layoutParams);