How to dynamically set textview height in Android - android

In my application I need to set dynamic text to my textview so I want it to get resized dynamically. I have set:
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="normal"
android:text="Frontview"
android:layout_weight="1"
android:gravity="center_vertical"
android:textColor="#0099CC"
android:singleLine="false"
android:maxLines="4"
android:ellipsize="marquee"
/>
My textview height is not going beyond 2 lines and the text is getting cut.

As Konstantin said, this code will probably be ignored after you exceed 4 lines unless you remove android:maxLines="4"
This is how you would set the height in code:
TextView tv = (TextView) findViewById(R.id.TextView02);
int height_in_pixels = tv.getLineCount() * tv.getLineHeight(); //approx height text
tv.setHeight(height_in_pixels);
If you want to use dip units, which allows your app to scale across multiple screen sizes, you would multiply the pixel count by the value returned by getResources().getDisplayMetrics().density;
This depends on your desired behavior, but you might also consider having the TextView size fixed, and allowing the user to scroll the text:
TextView tv = (TextView)findViewById(R.id.TextView02);
tv.setMovementMethod(ScrollingMovementMethod.getInstance());

TextView tv;
----------------------
tv=new TextView(this); // you take TextView id (R.id.textview1)
LinearLayout.LayoutParams Params1 = new LinearLayout.LayoutParams(15,50);
tv.setLayoutParams(Params1); //you take linearlayout and relativelayout.

Remove android:maxLines="4", it restricts height of your view to 4 lines of text.

Related

Text does not adapt to TextView

I need to insert a large text in a TextView, but when pasting the text it does not adapt to the size of the TextView, because it has exceeded the width of the TextView and the text is giant, how to fix this?
Note: I need to correct it using the Android Studio visual interface.
To set optimal size of the text in TextView You can use autoSizeTextType property. To do it Your TextView must have fixed sizes or match_parent. Then You can use autoSizeTextType It looks something like this:
<TextView
android:layout_width="match_parent"
android:layout_height="150dp"
android:text="#string/lorem_ipsum"
app:autoSizeTextType="uniform"
app:autoSizeMinTextSize="12sp"
app:autoSizeMaxTextSize="100sp"
/>
Results:
TextView height : 150dp
TextView height : 300dp
Here is a short video on YouTube with great explanation

How to set a textView width dynamically larger than the width of the screen after animation?

I have a text view with a height which wraps content and a width that matches parent. So that the textView width == the screen Width.
But at one point I want the text to rotate 90 degrees.
Now I want to be able to change the views width so that it is the devices height instead of width.
This would cause that the width would expand.
(Basically like when one does orientation changes, but I can´t just have an orientation change for only one textview so I have to rotate it.)
My problem: I can´t set the textViews width larger than the device width.
Even when I am already done with the animation.
So is it possible to make the textView width larger than the device width?
and if not can anyone please suggest how I could solve my problem because I really need to change the orientation of the textView...
EDIT----EDIT
Is there a way to create a landscape text view and put it in a portait activity? That would completly solve my problems of the last week...
EDIT
My fragment:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/f"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/t"
/>
</RelativeLayout>
I tried different things like
fragment.setRotation(90);
LayoutParams params = fragment.getLayoutParams();
params.height = text.getHeight();
params.width = deviceHeight;
fragment.setLayoutParams(params);
LayoutParams params1 = text.getLayoutParams();
params1.height = text.getHeight();
params1.width = deviceHeight;
text.setLayoutParams(params1);
EDIT________EDIT
Or has anyone ever written something like a verticalTExtView Class?
You can set the attributes of TextView as android:singleLine="true" and android:layout_width="wrap_content" and android:layout_height="wrap_content" // incase if you do not want the height to be increashed so singleLine is making the text to go out of screen width then you will need ScrollView for such behavior.
Hope this helps.

Android textView size is bigger than dynamically created one

I got some issue with dynamically created TextViews. To be more specific:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some text"
android:textColor="#color/black"
android:textSize="30px" />
appears much larger than:
TextView prName = new TextView(this);
prName.setTextColor(getResources().getColor(R.color.black));
prName.setText("Some text");
prName.setTextSize(TypedValue.COMPLEX_UNIT_PX, 30);
How to made them equal? Thanks beforehand
For text you should use scale points (SP) instead of pixel.
For xml:
android:textSize="30sp"
For code:
prName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30);
Set height and width as wrap_content for your textview.
LinearLayout.LayoutParams Params1 = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
prName.setLayoutParams(Params1);
Make sure when you setTextSize for any type of view, you should set it in scalable points (sp) and not in pixels (px) like this:
In xml:
android:textSize="18sp"
In code:
prName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
Using scalable points will let your TextView text size be equal on all devices, while using pixels will let your TextView text size be unequal on devices with different resolutions.
use setTextSize(int unit, float size)
TypedValue.COMPLEX_UNIT_PX //Pixels
TypedValue.COMPLEX_UNIT_SP //Scaled Pixels
TypedValue.COMPLEX_UNIT_DIP //Device Independent Pixels
In here just set
prName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
Remove this line:
prName.setTextSize(TypedValue.COMPLEX_UNIT_PX, 30);
... or use this instead:
prName.setTextSize(30);

Is there a way to specify android:layout_height dynamically during onCreate?

I would like to set
android:layout_height="wrap_content"
to some pixels(int) during my application start. This is a textView and the reason behind this is that i want the height of the textView dynamic based on some inputs from my end which will be computed when the onCreate method is called.
Is this possible? if yes any example would be great.
Forgot to add my textView xml looks like this
<TextView
android:id="#+id/sometext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:fadingEdge="vertical"
android:background="#drawable/dropshadow"
android:scrollbars="vertical"
android:text="Getting data on your slow n/w..."
android:textColor="#ffffff"
/>
Just edit the layout params of the view.
TextView v = (TextView) findViewById(R.id.sometext);
LayoutParams lp = v.getLayoutParams();
lp.height = 50;
v.setLayoutParams(lp);
The view will be 50px high in this example.
Note that it's generally not recommended to use pixel dimensions for layouts due to the many device specs out there. Rather use dp (density independent pixels) instead. You can calculate pixel dimensions from dp values in the following way (50dp to px here):
float dp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50,
getResources().getDisplayMetrics());

Setting the text of TextView that fits the screen Height and Width independently of the text size of TextView

I have a very large string that wont fit in the screen if I shown it as a TextView. Could any one tell me how to select a sub string which exactly fit the screen Height and Width independently of the text size of TextView.
add these attribute to your TextView in the xml
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"

Categories

Resources