I have this section in my layout file:
<RelativeLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView></TextView>
<TextView></TextView>
</LinearLayout>
</RelativeLayout>
My objective is to set the gravity of the items inside the linearLayout and then to add a margin to the whole LinearLayout programmatically.
This is what I have:
linearLayout_textBackground.setGravity(gravity); //where gravity is the int of the desired gravity
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
linearLayout_textBackground.setLayoutParams(layoutParams);
linearLayout_textBackground.requestLayout();
I would like to set the margin in using layoutParams but when I run the above code, I notice that my gravity value had been reset. However, if I comment out linearLayout_textBackground.setLayoutParams(layoutParams);, my gravity value are set correctly.
Why is the gravity resetting after I setLayoutParams to my layout?
When you do this :
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
You are creating new reference for your layout parameters the one you mention here will change the others not mentioned will be changed to default of android. If you want to change gravity then you need to change it after creating new params like:
Use this:
linearLayout_textBackground.setGravity(gravity);
after this:
linearLayout_textBackground.setLayoutParams(layoutParams);
or define your layout gravity in defining layoutParams.
But the recommended way of setting your gravity is inside your xml as follows:
android:layout_gravity="center"
then you dont need to do this :
linearLayout_textBackground.setGravity(gravity);
Thanks to #M.Saad Lakhan's answer, I noticed that I was creating a new reference of the parameters. So what I ended up doing is getting the actual params of the layout:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)
linearLayout_textBackground.getLayoutParams();
This fixed the problem.
Related
Can I set the position of the widget with programming in android instead the xml code. For example:
And like these. Are there methods instead the above xml code to use?
Yes you can set all the circled attributes via code aswell. For example this code aligns the button1 to the left of its parent (RelativeLayout) and positions button2 left of button1.
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
button1.setLayoutParams(params);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF, button1.getId());
button2.setLayoutParams(params);
Keep in mind that
layout_alignTop
layout_alignEnd
layout_alignParentBottom
layout_alignParentLeft
layout_alignParentStart
layout_toRightOf
layout_above
layout_below
layout_centerInParent
layout_centerHorizontal
will work only if the view you are setting the attributes to, is inside a RelativeLayout.
Original answer,
Yes you can, for example
Button button1 = new Button(R.id.buttonn);
Button button2 = new Button(R.id.buttonn2);
button1.setX(button2.getX);
There are more functions like setX , if you need something spesificly just ask for it :)
I have a RelativeLayout #+id/main_layout.
Inside the #+id/main_layout, I have a TextView #+id/txt1 and a RelativeLayout #+id/store.
For some reason I have to set android:layout_below="#+id/txt1" to the inner RelativeLayout #+id/store programmatically. How to do that?
Can you try adding it like this:
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
relativeParams.addRule(RelativeLayout.BELOW, idOfTheViewBelow);
Of course, use parameters (fill parent etc.) which you need!
Edit: Of course, you don't need to create the RelativeLayout, just instantiate your own, and apply the rules you need!
Hi I am developing android application in which I am creating relative layout programmatic and tried to set margin for that and Added it into linear layout which have orientation linear.
So here is my code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/screen_background"
tools:context=".ChooseChannelsFragment" >
<LinearLayout
android:id="#+id/main_outer_llt"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
</LinearLayout>
</RelativeLayout>
and inside fragment I am adding relative layout like this
RelativeLayout relativeLayout = new RelativeLayout(getActivity());
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(200, 80);
relativeParams.setMargins(20, 20, 20, 20);
relativeLayout.setLayoutParams(relativeParams);
relativeLayout.setBackgroundColor(getResources().getColor(R.color.green_color));
linearLayout.addView(relativeLayout);
It create layout with given color and size but not accepting margins. Am I doing something wrong? How to do this? Need Help. Thank you.
The LayoutParams type you use on a view should actually be from its parent.
So, if you're adding a RelativeLayout to a LinearLayout, the LayoutParams you set to your RelativeLayout should actually be a LinearLayout.LayourParams, and not a RelativeLayout.LayoutParams.
As others suggested, layout_margin# is the space between the parent's # edge and your view.
# replaces "Left", "Right", "Top" or "Bottom"
Getting/setting margins worked for me with:
ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) mView.getLayoutParams();
params.topMargin += 20;
mView.requestLayout(); // important
Of course, my View was indeed a ViewGroup and the parent was a ViewGroup as well. In most cases, you should cast your layout params to the parent's View class LayoutParams (in this case it's ViewGroup and RelativeLayout)
In this case, the father is LinearLayout. So you should use:
TableLayout.LayoutParams layoutParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(20, 20, 20, 20);
I am trying to add TextView into a view which extends LinearLayout. When I set its width of layoutparams to fill_parent or wrap_content, the text inside TextView will display vertically such like the following.
e.g.
T
E
X
T
However, when I set the width to some fixed number, it will display normally or as I expected.
e.g. TEXT
My question is why this happens and how to solve it, i.e. how I should set programmatically so that TextView can write text horizontally without setting a fixed width to it such as setting fill_parent or wrap_content?
Here is setting of XML code of the parent ilGallery which extends LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<com.illib.ilgallery.view.ILGallery
android:id="#+id/galleryLayout"
android:layout_height="fill_parent"
android:layout_width="fill_parent"/>
</LinearLayout>
The following is the code how i initialize the children inside it:
ilViewPager = new ILViewPager(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
ilViewPager.setLayoutParams(params);
ilgallery = this;
//initialize default header and footer view
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
headerView = new TextView(context);
headerView.setLayoutParams(params2);
((TextView)headerView).setTextSize(TypedValue.COMPLEX_UNIT_PX,40);
((TextView)headerView).setText("hello");
footerView = new TextView(context);
footerView.setLayoutParams(params2);
((TextView)footerView).setTextSize(TypedValue.COMPLEX_UNIT_PX,40);
((TextView)footerView).setText("hello2");
There appears to be only a single item inside your LinearLayout. Please set the orientation of the LinearLayout to "horizontal", like this:
android:orientation="horizontal"
Also, whenever possible instead of creating new LayoutParams objects from scratch, you should get the LayoutParams from the current layout and modify that, like this:
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)ilViewPager.getLayoutParams();
lp.<change-any-values>;
ilViewPager.setLayoutParams(lp);
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.
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
Within a relative layout you have to use other layout params:
Layout params within relative layout