How to set Button "Layout below" properties by code in android inside an Relativelayout
i have defined relative layout by using findviewby id.
use addRule() of LayoutParams to set layout below
RelativeLayout.LayoutParams lineParams = new RelativeLayout.LayoutParams(
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT,
android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
lineParams.addRule(RelativeLayout.BELOW, myview.getId());
Add this button to RelativeLayout which is defined
Relativelayout rl= (RelativeLayout) findViewById(R.id.Relativelayout01);
rl.addView(b,lineParams);
Related
I don't know how I can set the TextView on the right side in my RelativeLayout. Adding gravity to the TextView didn't work. Thanks for helping me.
RelativeLayout relativeLayout = new RelativeLayout(context);
relativeLayout.setGravity(Gravity.CENTER_HORIZONTAL); // center in linearLayout_power_stations_bar
textView = new TextView(context);
textView.setText(String.valueOf(nuke_count));
textView.setTextColor(activity.getResources().getColor(R.color.game_powerStationsBar_textOverIcon));
textView.setTextSize(16);
relativeLayout.addView(textView); // TextView over image
relativeLayout.addView(imageView); // adding image
linearLayout_power_stations_bar.addView(relativeLayout); // adding to other layout
You can set layout parameters to TextView and also set alignment of parent right.
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
textViewsetLayoutParams(params);
Now You can Use this code :)
I have an issue with the creation of a new RelativeLayout above an already existing TextView in my app.
My main layout is called contentLayout and inside it I have a TextView called addRoomButton :
RelativeLayout contentLayout = (RelativeLayout) findViewById(R.id.contentLayout);
TextView addRoomButton = (TextView) findViewById(R.id.addRoomText);
I just want to add a new RelativeLayout above my TextView so I wrote this :
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setId(R.id.roomRelativeLayout);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
100
);
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rlp.addRule(RelativeLayout.ABOVE, addRoomButton.getId());
contentLayout.addView(relativeLayout, rlp);
The RelativeLayout is well created on the top of my contentLayout, but my TextView is not visible. I think it's just behind relativeLayout but I'm not sure...
Is there any solutons to put the TextView below the new RelativeLayout without creating LinearLayout or other Layouts?
Why wouldn't you do it by XML?
layout_below=""
try out your idea with xml. put relativelayout with relativelayout(A) in it and button(B). set A to layout_above B and see there is no success. Set B bellow A and you get the effect you want. So you need to getLayoutParams() of your button and adjust it to be below your newly created layout.
I've RelativeLayout created in layout/activity.xml
And i want to add some elements there programmatically by following way:
RelativeLayout rlayout = (RelativeLayout) findViewById(R.id.relativeLayout1);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rlayout.addView(CustomView,p);
And it works, but elements which were added doesn't fill all view, but i need it.
and also i want to add such elements in square (width=height), how can I do it?
To fill all view Use LayoutParams.MATCH_PARENT instead of LayoutParams.WRAP_CONTENT.
To make layout as square just create int width,height = 300; and then :
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(width, height);
or pass LayoutParams.WRAP_CONTENT into RelativeLayout.LayoutParams and change height and width of your custom view.
To make view square :
Button customView = new Button(this);
customView.setLayoutParams(new RelativeLayout.LayoutParams(200, 200));
RelativeLayout rlayout = (RelativeLayout) findViewById(R.id.relativeLayout1);
rlayout.addView(customView);
to make fill on all view you can use this:
Button customView = new Button(this);
customView.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
RelativeLayout rlayout = (RelativeLayout) findViewById(R.id.relativeLayout1);
rlayout.addView(customView);
Also you can add rules for your layout items like this :
p.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
Best wishes.
I have to add a custom layout in a relative layout on a button click.
Again on another click add that same layout with another values above the previous inflated layout.
and it will continue like this.
I don't want to use list view.
I can add dynamically my custom layout but how to place it above the previous added.
on click of ADD button a new row will be added to my relative layout in xml, like ROW 1, ROW 2, and this will continue with ROW 3, ROW 4 etc.
you can use the addRule method of RelativeLayout.LayoutParams class. For example:
RelativeLayout.Layoutparams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
button.setLayoutParams(params);
Doing this, you can set programmatically all params you would set in your layout xml file.
on button click event add this code:
LinearLayout linearLayout=(LinearLayout) findViewById(R.id.layout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout=new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params);
Button button1=new Button(getApplicationContext());
button1.setLayoutParams(params1);
button1.setText("button");
layout.addView(button1);
linearLayout.addView(layout);
Define the layout having id #+id/layout and orientation Vertical in XML file.You will find the button layout being added on every click event.
I created LinearLayout and Button via class not via XML. I know how to inflate layout view and button view dont know how to set margins of them. I need to set margins of my button and my linearlayout. If I use XML, it is very simple: <android:marginLeft="10px">.
But, what should I do if I want to set margin by class not by XML?
In this we have linear layout in main.xml named lyt1 and we add edittext at runtime and set
left margin value
please use bleow code :
lyt = (LinearLayout)findViewById(R.id.lyt1);
EditText txt = new EditText(WvActivity.this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
lp.leftMargin = 10;
txt.setLayoutParams(lp);
lyt.addView(txt);
lyt.invalidate();
Use:
LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(left, top, right, bottom);