scooby
scooby

Reputation: 603

Relative layout change through java code

I went through several links yet i am not able to find if it is possible to set other attributes of relative layout, like android:gravity,android:orientation through java code. I want to change the positions of the some of the views through java code and not through xml.

e.g, i want to set these attributes through java code for a particular view. android:gravity="center_vertical" android:orientation="vertical"

how can i do so? please help.

Upvotes: 0

Views: 2253

Answers (2)

silwar
silwar

Reputation: 6518

as Daan said you can use Layout params for relative layout in android

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.alignWithParent = true;
params.bottomMargin = 5;
params.height = 50;
params.leftMargin = 50;

But as you said you want to set following attributes for particular view, you cannot assign these attributes to RelativeLayout as these are belongs to LinearLayout and other views

android:gravity="center_vertical" android:orientation="vertical"

For setting gravity for any view please follow the code

tv = (TextView)findViewById(R.id.tv01);
tv.setGravity(Gravity.CENTER_VERTICAL);

You cannot set Orientation of any view.. instead you can set orientation to LinearLayout

like this

LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);

Hope this will help you

Upvotes: 1

Daan Geurts
Daan Geurts

Reputation: 449

Within a relative layout you have to use other layout params: Layout params within relative layout

Upvotes: 0

Related Questions