TextView inside FrameLayout being cut-off and not wrapping - android

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.)

Related

Why are these buttons mis-aligned in a TableLayout?

I'm using a table layout to arrange some buttons. As long as I use the same font for all the labels they are properly aligned in each row.
For some buttons I'd like to use icons from a custom ttf font.
When I use such an icon, the button is placed slightly higher, like so:
(This image is scaled up to make the the problem more evident.)
I took measurements - the buttons appear to be of same height, regardless of the used font.
Why are the buttons not aligned properly?
Does anyone have a suggestion to get them aligned?
Thanks.
Following CommonsWare's advice (thanks for the quick replies!), I tried this:
final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.CENTER_VERTICAL;
row.setLayoutParams(layoutParams);
This did not work. Will try the base alignment comment next.
Add the following attribute to your TableRow:
android:baselineAligned="false"
By default, the button labels' base lines are vertically aligned which causes the offset you experience.

Adding Textviews Dynamically goes out of focus to a Linear Layout

I am trying to achieve a dynamic list of textviews like in the image below :-
Here is my code :-
LayerDrawable dashboardResShape_community= (LayerDrawable) getResources().getDrawable(R.drawable.upcomingtask_tags_shape);
// The background effect is by the layer list drawable from the above code
LinearLayout tags_view2=(LinearLayout)findViewById(R.id.tags_view);
LayoutParams lp = new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.setMargins(10, 2, 2, 2);
TextView[] tx = new TextView[15];
for(int i=0; i<15; i++) {
tx[i] = new TextView(getActivity());
tx[i].setPadding(8, 4, 8, 4);
tx[i].setBackground(dashboardResShape_community);
tx[i].setLayoutParams(lp);
tx[i].setText("Tag"+i);
tags_view2.addView(tx[i]);
}
and in my xml there is only a linear layout :-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tags_view"
android:orientation="horizontal" >
</LinearLayout>
This is what i achieve :-
When i am adding 15 textviews, only 8 are shown like below, the rest should come in the next line but they are not.
If i add more textviews, it goes out of screen but i want to add the textview in the second line when the first line is full. What i am doing wrong here?
Its LinearLayout's limitation.
If you want the explained behavior than
You have to make your own Layout/View refer this link or
Impliment LinearLayout Horizontal orientation with wrapping children like this
you cannot get more text views on next line after linear layout is filled( screen width ), you already the made linear layout orientation as horizontal. Better solution add one more linear layout or use relative (do some child count coding and set parameters). The best solution i prefer for u is table layout. Easier to code code and handle
What you can do is add as many textviews as will fit on the screen to your linearlayout, but then when a textview would go off the screen, you could add another linearlayout below the one that you already had, and then add on to that. You could keep doing that and you would end up with no textviews goind off the screen. You could also try using a gridview.
Here is what this layout looks like:
http://developer.android.com/guide/topics/ui/layout/gridview.html
And here is the documentation:
http://developer.android.com/reference/android/widget/GridView.html

How do I make two relativelayouts show as top and bottom on a linearlayout versus side by side, programmatically?

I have a linearlayout as a container for two relativelayouts. Both relativelayouts appear on the screen but they are side by side. I want them to be top and bottom. It looks as if the linearlayout initialization defaults to Horizontal. I have tried using setorientation to Vertical but the screen blanks out.
The following code is an example of what I am trying to do:
LinearLayout layoutContainer = new LinearLayout(this);
layoutContainer.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
//layoutContainer.setOrientation(LinearLayout.VERTICAL);
// Arguments here: width, height, weight
LinearLayout.LayoutParams childLp = new LinearLayout.LayoutParams(0,
LayoutParams.FILL_PARENT, 1);
layoutTop = new RelativeLayout(this);
layoutContainer.addView(layoutTop, childLp);
layoutBot = new RelativeLayout(this);
layoutContainer.addView(layoutBot, childLp);
layoutTop.setBackgroundColor(GREEN);
layoutBot.setBackgroundColor(Color.BLUE);
setContentView(layoutContainer);
It looks as if the linearlayout initialization defaults to Horizontal.
That is exactly right.
I have tried using setorientation to Vertical but the screen blanks out.
You do need to set orientation to vertical to get this effect. As for it "blanks out", I see several things wrong such as setting the width to "0". There is no width so it won't show anything. I think you would want something like LinearLayout.WRAP_CONTENT. Also, you are using LinearLayout params for your RelativeLayout which may or may not make a difference in this case.
If there isn't a necessary reason to create the layout in Java it is much easier to do this in the xml.
I think you are complicating your layouts by trying to programmatically manipulate them. Set the orientation to Vertical and do the following:
<LinearLayout
------
------
android:orientation="vertical">
Your first Relative layout
<RelativeLayout
android:id="#+id/rel1"
android:layout_alignParentTop="true"
----------------------
---------------------
/>
Your next Relative layout
<RelativeLayout
android:id="#+id/rel2"
android:layout_below="#id/rel1"
----------------------
---------------------
/>

RelativeLayout params AddRule doesn't consistently work?

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

android:layout_alignParentBottom is ignored when used without explicit layout height as a row in ListView

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.

Categories

Resources