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!
Related
I must be missing something really stupid.
I have a LinearLayout in my view:
_editTextViews = (LinearLayout)findViewById(R.id.editDesAndLocViews);
I am trying to dynamically change the height of that view by setting the LayoutParams
I have two LinearLayout.LayoutParams
LinearLayout.LayoutParams collapsedParams;
LinearLayout.LayoutParams openParams;
that are set in the constructor like so:
collapsedParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,0);
openParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
But as soon as i try to apply one of those params to my LinearLayout, like so:
_editTextViews.setLayoutParams(collapsedParams);
I get the following FATAL EXCEPTION:
At first i thought my project needed to be cleaned as it seems to think i am trying to force a relative layout to use linear layout params, but everything is LinearLayout. The LinearLayout in question does contain a couple RelativeLayouts. I don't know if / why that would be the problem.
please help
Change your LayoutParamss' type to either ViewGroup.LayoutParams or RelativeLayout.LayoutParams.
The LayoutParams you apply have to match the parent layout of the view. LayoutParams define how a View is layed out in its parent view (layout). Because each layout type takes different parametres for laying out a view (e.g. only LinearLayout has weight, only RelativeLayout can use anchors), they all have their own LayoutParams.
How can I set the RelativeLayout.ALIGN_PARENT_LEFT to LinearLayout in code? I have a RelativeLayout which child is LinearLayout and I need to set the RelativeLayout.ALIGN_PARENT_LEFT in the child LinearLayout instance. The problem is that when I set it, I will lose the child LinearLayout parameters (I will lose the weight). My understanding is that the only way to set the RelativeLayout.ALIGN_PARENT_LEFT to child element is to use RelativeLayout.Layoutparams when setting the child view layout parameters.
Thanks in advance!!
You have to set the width height parms and weight along with your aligh left rule in programtically
layoutparms = new LayoutParams(WindowManager.LayoutParams.FILL_PARENT,
WindowManager.LayoutParams.FILL_PARENT, weightthere);
layoutparms.addRule(RelativeLayout.LEFT_OF, leftisdeview);
yourview.setLayoutParams(layoutparms);
You just have to give android:gravity="left" to the parent linear layout.Remember that it is gravity not layout_gravity.
I need to create a layout dynamically like this through code instead creating through XML.
I am able design either in vertical or horizontal buttons. But I need to create both vertical and horizontal in a same layout.
Please help me to do it in easy way.
Thanks in advance
Its simple, First create the parent linear layout
LinearLayout parentLayout = new LinearLayout(this);
set the parameters and orientation for this parent layout:
LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT)
parentLayout.setOrientation(VERTICAL OR HORIZONTAL);
parentLayout.setLayoutParams(layoutParam);
Thats it , your parent layout has been generated. Now create new layout and start adding it to this layout. For ex.:
LinearLayout firstLayout = new LinearLayout(this);
LinearLayout secondLayout = new LinearLayout(this);
LinearLayout thirdLayout = new LinearLayout(this);
parentLayout.addView(firstLayout);
parentLayout.addView(secondLayout);
parentLayout.addView(thirdLayout);
Also you need to set the parameters for all the layout seperatly. You can optimise the code according to your need.
Hope it works!
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
I'm using a FrameLayout to display (on demand) some text on the screen. I want the text to be in a certain place, so I thought setGravity() would do the job... but no, it seems to have no effect whatsoever on where the text goes (and other objects like ImageView don't even have this method).
So first, what exactly is TextView.setGravity() used for? (Edit: I understand this much better now, thanks! Still not clear on the following part of the question though.)
Second, it seems the only way to update a FrameLayout in this way is to create a new FrameLayout.LayoutParams object with the settings you want, and then use the setLayoutParams() method to apply it. (This seems to automatically update the view so is a requestLayout() call necessary?) And is there a simpler / more straightforward way to achieve this for a FrameLayout... say, without creating a new LayoutParams object as I'm doing now?
Thanks! For reference, below is a (working) code snippet showing how I'm setting up this FrameLayout and TextView.
FrameLayout fl1 = new FrameLayout(this);
FrameLayout.LayoutParams flp1 = new FrameLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
fl1.setId(9001);
fl1.setLayoutParams(flp1);
.....
tv1 = new TextView(this);
FrameLayout.LayoutParams tvp1 = new FrameLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM));
tv1.setId(9006);
tv1.setLayoutParams(tvp1); // This works
tv1.setBackgroundColor(Color.GRAY);
tv1.setTextColor(Color.BLACK);
tv1.setText("Dynamic layouts ftw!");
tv1.setGravity(Gravity.CENTER); // This does NOT work
.....
fl1.addView(tv1);
1) view.setGravity means hows the view should positions if children.
In the case of the textview it refers to the positioning of the text.
When in linearlayouts or viewgroups it refers to its child views.
2) I checked your code. You are already using textview.setGravity method. In that case you dont need to specify gravity parameters in the FrameLayout.LayoutParams constructor.
Other thing I noticed is that you gave the textview the width and height as wrap content which will only take the size of the text. So there is no meaning in giving gravity as the textview has no extra area to position the text to the center. You need to give the width of the textview as fill_parent. That should make your gravity property work.
Btw this is a very good article about Gravities. It explains both about gravity and the layout_gravity attribute.
If you want your textview to wrap the content then you should add your textview to a linearlayout and setgravity to the linear layout.
This should give what your are trying to do
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
FrameLayout fr = new FrameLayout(this);
fr.setLayoutParams(lp);
fr.setBackgroundColor(Color.RED);
LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
LinearLayout l = new LinearLayout(this);
l.setLayoutParams(lp2);
l.setGravity(Gravity.CENTER);
l.setBackgroundColor(Color.BLUE);
ViewGroup.LayoutParams vp = new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
TextView t = new TextView(this);
t.setLayoutParams(vp);
t.setText("Blessan Mathew");
t.setBackgroundColor(Color.CYAN);
l.addView(t);
fr.addView(l);
tv1.setGravity(Gravity.CENTER);
belongs to the gravity of the TEXT inside the TextView.
As documentation states:
Sets the horizontal alignment of the text and the vertical gravity
that will be used when there is extra space in the TextView beyond
what is required for the text itself.