Dynamically display a TextView - android

Is it possible to display a TextView dynamically so that the height of the text can be retrieved in the middle of the Application but not at the end that is done using onGlobalLayout() or onSizeChanged().
My requirement is to display the text in the TextView to a certain height limit. I need to check the height of the text in the TextView so that I can achieve my requirement but using any of the onGlobalLayout() or onSizeChanged(), I am getting the height at the end which is of no use to me because I can't check at the end.
Is there anyway to achieve my requirement?

I think this will help you.
Dynamic Views in Layout

Related

How to center a multiline text view with the smallest width necessary?

I would like to have my 2-line text view in android-xml centered horizontally with the smallest width necessary. At the moment it's like the first line is completely filled and the second with the rest of the words.
Does anyone know how to achieve that both lines are filled kind of equally? Below are 2 images, the first showing the default multi-line text view, the second what I would like to achieve. As you can see, the second image requires much less width than the first and is more in the center of the view.
Thanks in advance!
Try setting android:breakStrategy attribute of textView to balanced
android:breakStrategy="balanced"
Add this line to your textView's xml code, this will balance the lines.

Get measure height of a TextView without add to the view hierarchy

I get following requirement.
I need to build a tree with leaves placed left and right, from the screen top to the bottom. I can not put the leaves in a ListView because tow leaves will be in same offset.
I don't know the height of the items.
I need to pre-calculate the height of dynamic content, such as strings with different lengths.
QUESTION:
How can i pre-calculate the height of a sting which will be put in a TextView widget described as follows:
<TextView
android:id="#+id/note_content"
android:layout_width="40sp"
android:layout_height="wrap_content"
android:maxLines="5" />
Create textView, set LayoutParams, set text, call it's measure method and read measured values. BTW you can measure text, not textView (see StaticLayout).
I think you better create your custom layout where you can implement all your "leaves measument" algorithms. Check this lesson. Custom layout is very easy )
Use this for calculate height and width your textview
textView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
public void onGlobalLayout() {
h=textView.getHeight();
w=textView.getWidth();
textView.getViewTreeObserver()
.removeGlobalOnLayoutListener(this);
}
But in xml do not assign textview as a wrap content. Because it will give you value 0.

Resizing the textview size based on the screen width for all devices

I have problem in resizing the text view .
I have a Layout like this with below components
ImageView| TextView | ImageView
placed in relativelayout(horizontal) and each one width is wrap content and height is fill parent.
PROBLEM
I have a text like this "My name","My name is xyzmnop" now, i have fixed the TextView size to 15sp, so what is happening is in larger devices since Textviews width increases the text "My name is xyzmnop" will fit but in smaller devices since the width is small it will spill out to second line. But i want it to be in same line, as I have the flexibility to decrease the size of the text if the text is lengthy.
please help me with this problem.
i have seen this
Android TextView doesn't resize after changing text size
Android EditText Resize Programatically
Resizing TextView does not shrink its height on Android 3.1
EDIT:
I have also added setSingleLine = "true"; in my xml
Say, tv is your TextView.
Just call tv.setMaxLines(1);
TextView has the setMaxLines (int maxlines) method:
Makes the TextView at most this many lines tall. Setting this value overrides any other (maximum) height setting.
I suppose this might help you if you set the maximum number of lines to 1.
Note: there is also the android:maxlines attribute if you need to set it in your XML layout.
i have solved it using ViewTreeObserver.

How to reduce the size of a text view dynamically

I am using a textview to display a text which I am fetching dynamically. But the problem is that the size of the fetched data may vary from 10 to 30 characters. But I am using a textview with a fixed width. I can't do anything on that. Is there any way to reduce the textsize or something dynamically in order to fit it to the fixed size textview?
Check this answer, How to dynamically set textview height android
It gives you idea of how to set height of textview as per number of lines of text.
TextViwe txt = (TextView)findViewById(R.id.txt);
txt.setTextSize(Float.parseFloat(textSize));
I think reducing text size according to length of the data is a bad practice. So one solution is you can move text into next line by following code.
textView.setSingleLine(false);
or
android:singleLine="false"
and set textView height as WRAP_CONTENT

How to move a Textview dynamically on the screen? (framelayout)

I have a app that shows the camera view on the screen on a FrameLayout. The screen is in fixed LandScape mode.
I need to write a textView with dynamically determined coordinates of the screen. The coordinates are determined in percentages, for example :
write the textview on the coorinates x=80% of the screen & y=20% of the screen. write the textview on the coorinates x=35% of the screen & y=55% of the screen.
how to do it? i already have the percentages, i only need to know how to use them to write the textview on the desired position of the frameLayout
code examples are welcome
i tried with this but doesn't works, the textview isn't moved:
TextView poi..... etc etc
poi.setLayoutParams(new LayoutParams((int)(w*(xCoordPercent/100)), h/2));
thanks
MarginLayoutParams params=(MarginLayoutParams )poi.getLayoutParams();
params.leftMargin=80;
//here 100 means 100px,not 80% of the width of the parent view
//you may need a calculation to convert the percentage to pixels.
params.topMargin=50;
poi.setLayoutParams(params);
This may help.

Categories

Resources