I've got a view that's a child of a RelativeLayout, and I'm trying to modify the layout parameters in the Java. I get the LayoutParams as follows:
RelativeLayout.LayoutParams dlp = (LayoutParams) dimBackground.getLayoutParams();
dlp.addRule(RelativeLayout.BELOW, currentTextView.getId());
//dlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
dimBackground.setLayoutParams(dlp);
The line dlp.addRule(RelativeLayout.BELOW, currentTextView.getId()); doesn't change the position of the dimBackground, but if I uncomment the line below, then dimBackground will get aligned the bottom of the parent view.
Any idea why the first rule wouldn't be working?
Edit:
As suggested, I tried in the xml, still with no luck. Below is my xml:
<RelativeLayout
android:id="#+id/dim_background_bottom"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_below="#+id/product_subtotal_row"
android:background="#color/gray_tint"
android:visibility="visible" />
(Since you didn't post too much details about your layout... it's difficult to give you a definitive answer).
I guess that your currentTextView and your dimBackground don't have the same parent relativeLayout.
As mentionned in the RelativeLayout doc :
A Layout where the positions of the children can be described in relation to each other or to the parent.
So please double check that currentTextView and dimBackground are both child of the same RelativeLayout
Related
Ive got a FrameLayout which im dynamically adding TextViews to. Currently, the TextViews near the edge arent wrapping at the end of the parent FrameLayout and are going off screen. The FrameLayout is using wrap_content for both the width and height:
<FrameLayout
android:layout_margin="10dp"
android:id="#+id/wrappingFrameLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00ff00"
>
The TextViews are just being created, then translated to a certain spot using .setTranslationX() and .setTranslationY().
I've tried getting it to wrap using some answers found elsewhere on SO, such as setting LayoutParams to WRAP_CONTENT on the TextView:
newTextView.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT));
I've also tried newTextView.setSingleLine(false); but that didnt work either.
Any ideas?
EDIT: So I noticed that none of the dynamically added views respond to any methods on them except things like color/textsize. I tried calling methods like setMinLines(3) and .setWidth(100) and they had no effect.
You can try using FrameLayout.MarginLayoutParams instead of .setTranslationX() and .setTranslationY() to position the TextViews.
Something like:
FrameLayout.MarginLayoutParams layoutParams = (FrameLayout.MarginLayoutParams)textView.getLayoutParams();
layoutParams.leftMargin = xPosition;
layoutParams.topMargin = yPosition;
textView.setLayoutParams(layoutParams);
(I was having the opposite problem. The TextViews were wrapping but I wanted them to be cut-off. I was using the LayoutParams and margins. Changing to setTranslation fixed it for me.)
I have the android xml layout using DroidDraw :
link : https://docs.google.com/document/d/1L3QJVwI9Znmeu1yANJXhDcZGFR70587d4oznu0hrZCw/edit?usp=sharing0
I need to bottom align the last Textview (Yellow one as indicated in the print screen).
i.e.
I need this :
Try using
android:alignParentBottom="true"
If I see correctly that the parent ViewGroup of your TextView is a RelativeLayout
Set the parent relative layout property to:
android:layout_gravity = "bottom | left"
In Relative layout use:
android:alignParentBottom="true"
and In Linear layout use:
android:layout_weight="1.0"
android:gravity="bottom|right"
This post did the trick.
stackOverflow link
I needed to implement layout_weight on my layouts. Basically defining weights for header, content and footer
I have an XML definition for a view that I am adding to a larger container view with addChild. It's based on a LinearLayout and looks basically like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="52dip"
android:background="#drawable/box_bg"
android:clickable="true"
android:onClick="onTreeBoxClick"
android:orientation="horizontal" >
<ImageView android:id="#+id/box_photo"
android:layout_width="45dip"
android:layout_height="45dip"
...
/>
<RelativeLayout
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="wrap_content"
>
(Remainder omitted -- probably not relevant since it's basically working as designed)
When I create these views, I have found the following behaviors that seem odd to me:
Right after I inflate the view, getLayoutParameters() returns null.
After I call addChild() to add it to its parent, getLayoutParameters() returns a valid object.
Examining the LayoutParameters, I find both width and height set to -2 (WRAP_CONTENT), which is clearly not what I specified in the XML file.
When I look at the layout parameters of the enclosed ImageView, it reads out at the specified values.
Can anyone explain what is going on here? Why isn't my specified height being noticed?
This isn't really affecting me since the parent view is a custom view wherein I force the final dimensions of the children with MeasureSpec etc., but I'd like to understand this anyway!
You didn't provide some details which are important.
1) Right after I inflate the view, getLayoutParameters() returns null.
I would assume that you used this:
inflater.inflate(R.layout.content, null);
in this case the LayoutInflater can't make(at all) proper LayoutParams for the root Linearlayout because it doesn't know who is going to be its parent(so it can create the right type of LayoutParams). If you would use this:
inflater.inflate(R.layout.content, someOtherLayout, false/true);
then the root LinearLayout will have proper LayoutParams because it will see the type of someOtherLayout and create the LayoutParams from this information. You may want to provide a snippet of code to get a better answer if this is not what you currently do.
2) After I call addChild() to add it to its parent,
getLayoutParameters() returns a valid object.
I assume that you speak about the addView() method. The addView() method will check the LayoutParams of the view which is trying to add and if those LayoutParams are null then it will automatically assign that view a LayoutParams object returned by its generateDefaultLayoutParams() method.
3) Examining the LayoutParameters, I find both width and height set to
-2 (WRAP_CONTENT), which is clearly not what I specified in the XML file.
As I said at 2, the generated LayoutParams are coming from the generateDefaultLayoutParams() method which will return a LayoutParams instance as the parent was designed to do. For example, a LinearLayout with orientation HORIZONTAL(the default one) will return a LayoutParams instance with width/height set to WRAP_CONTENT.
4) When I look at the layout parameters of the enclosed ImageView, it
reads out at the specified values.
Because the LayoutInflater took care of this, as the ImageView it's in the interior of the layout and has a known parent from which the LayoutInflater can make the proper LayoutParams.
I there a way to wrap_content on a specific element inside of a parent element? For instance, I have something like the following layout:
<RelativeLayout width:match height:wrap>
<ImageView width:match height:wrap scale:fitXY />
<LinearLayout width:wrap height:wrap>
</RelativeLayout>
The parent wrap constraint is very loose, but I want it to specifically use the matching width, but always match the height of the image view.
The problem here arises when I place this view in another RelativeLayout where each view is aligned above or below another in order to fill a potentially changing superview. LinearLayout didn't really seem to stretch things to fill, so I switched to Relative, but when I did, the view described above stretched vertically when I want it to still match the height of the image view.
Is there a good solution to this problem?
You could try putting the following (pseudocode) in the onResume() method:
if(myRelativeLayout.height > myImageView.height)
myRelativeLayout.setHeight(myImageView.height);
You need to make sure to call myRelativeLayout.measure() before you do this, so the system knows what the size of the Views will be.
Just an idea for you to try, let me know if it works :)
When I use a RelativeLayout with either fill_parent or wrap_content as height and an element which specifies: android:layout_alignParentBottom="true" it is ignored and it is aligned at the top. Setting the height of the RelativeLayout to an explicit value makes it work. Any clues?
This seems to be a bug in Android itself, see http://code.google.com/p/android/issues/detail?id=1394.
I worked around it by wrapping my RelativeLayout in a FrameLayout and putting my bottom aligned view as a children of the FrameLayout with android:layout_gravity="bottom". This hinders you from referencing it from within the RelativeLayout so you'll have to work around that (for example using margins).
If anyone has a better workaround, please share.
When you inflate the layout, use inflate(R.layout.whatever, parent, false), where parent is the ListView. If you don't do that (e.g., you pass null for the parent), RelativeLayout gets strange in list rows.
My hack for this andriod bug:
ViewGroup.LayoutParams lp=(ViewGroup.LayoutParams)view.getLayoutParams();
lp.height=view.getContentHeight();//hack for android bug about ViewGroup.LayoutParams.WRAP_CONTENT and android:layout_alignParentBottom="true" on landscape orientation
view.requestLayout();
act.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
I was able to get the proper alignment by specifying the problematic TextView with:
android:id="#+id/must_be_bottom_left"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_below="#id/xxx"
where xxx was the id of a TextView that has android:layout_below="#id/yyy"
and yyy is a TextView that is always above both xxx and must_be_bottom_left.
The contents of my list items can vary so that sometimes the "xxx" TextView is View.GONE, but even then the layout works as expected.
I don't know how fragile or merely seredipidous this work-around is. I am using Android 1.6 and I haven't tested it for forward compatability.