Text View Text analysis in Android - android

I have a textview and the text is loaded dynamically.
<TextView
android:id="#+id/txt_song_title"
style="?attr/txtcolorHightlight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignTop="#id/frlyt_img_cover"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:layout_toLeftOf="#id/img_audio_source_icon"
android:ellipsize="end"
android:fontFamily="#string/roboto_light"
android:maxLines="3"
android:text=""
android:textSize="#dimen/font_36" />
The textview has to show the entire text which is set (10 to 100+)characters. Based on text length, the font size should be changed inorder to fit the entire text. How to calculate num of words in the textview to change font size and display entire text without clipping dynamically?

I think you can do it from java code level. TextView class provides methods to implement this.
Following link may be useful.
Auto Scale TextView Text to Fit within Bounds

Try using a library, for example a simple google search gave me this:
https://github.com/grantland/android-autofittextview
I have not tried it but it seems ok :)

Related

Changing a TextView's style and text dynamically

I'm trying to make an app like the one in this mockup:
Supermarket
It's a very simple supermarket app. As you can see, there's a TextView at the bottom of the screen that tells me whether my Cart is empty, or has items in it. If the cart is not empty, the user is shown the total price he/she must pay. You can also notice that said TextView's style and text change according to a variable (in this case, "totalPrice").
How can I do this in Android? I know I can use simple if statements (if totalPrice == 0, then backgroundColor = grey and text = "EmptyCart", for example), but this seems somewhat... hardcoded. Is there a better way to do it? As you can see, the TextView's style and values also change when on the "Subproducts" activity (some Products have Subproducts, which you can see after clicking on them).
Thanks in advance!
I think Databinding is the best way rather than boilerplate code.
create a model class and change background of the view using ternary operation in databinding also manage visibility of price text using this.
To set a textview's text, you use textView.setText("new text"); To set its background, its textView.setBackgroundColor(color) where the color is the hexcode you want as an integer- for example 0xFFFF0000 for reg. Obviously these can be variables as well as hard coded.
It can simply be achieved with two TextViews in a RelativeLayout, and of course, by using basic TextView's methods. As an example layout:
<RelativeLayout
android:layout_height="75dp"
android:layout_width="match_parent"
android:background="#1EC4F3">
<TextView
android:text="PAY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:typeface="sans"
android:textColor="#FFFFFF"
android:textSize="18sp"/>
<TextView
android:layout_height="wrap_content"
android:text="$25.79"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="8dp"
android:typeface="monospace"
android:textSize="14sp"
android:textColor="#FFFFFF"/>
</RelativeLayout>

How to add a spacing between text and underline in android?

I have defined text of a textview as -
<string name="text"><u>this is my text</u></string>
I needed some space between the text and the underline, so I added lineSpacingExtra="2dp" to the textview, but it is not working.
Can anyone tell how to achieve this?
I need to support API 14 till 21. The above test was done on API 21.
I spent a great deal of my time on this question and here are my findings!
Firstly, To increase the spacing between the text and underline in css you need to use styles and unfortunately Android TextView does not support style tag when using Html.fromHtml(). Unfortunately even span tag is not supported (otherwise that could have been used). To see the entire list of tags supported check the HTML Tags Supported By TextView blog.
Now since we know the basic simple implementation wouldn't work, the only other way remaining is to fake it (fool the user!). In your xml layout where you have the TextView add a View below it with the following properties.
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text"
android:textSize="20sp"/>
<View
android:id="#+id/underlineView"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignEnd="#+id/textView"
android:layout_alignLeft="#+id/textView"
android:layout_alignRight="#+id/textView"
android:layout_alignStart="#+id/textView"
android:layout_below="#+id/textView"
android:layout_marginTop="10dp"
android:background="#android:color/holo_red_dark"/>
As you can see the underlineView is emulating the underline. It has its width fixed to the textview above it. You can set its color to whatever you need and importantly you can adjust the spacing using the android:layout_marginTop property. Hope this helps!
My suggestion is to remove the underline from the text string entirely because you can't customize the spacing from there. After that, you have a few options. One option is to use the #drawable feature as discussed in the following link: http://www.quora.com/How-do-I-design-edit-text-view-with-bottom-border-alone-in-Android-and-edit-text-view-with-some-special-symbol-like-below-image
If you want a quick and easy "hack" then go to the layout XML for your activity where your TextView is created. Wrap your TextView in a LinearLayout as follows:
<LinearLayout
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/text"
android:layout_marginBottom="2dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#color/underline" />
</LinearLayout>
The first TextView is where your text ("this is my text") is displayed so you can adjust the "layout_marginBottom" to whatever spacing you need between your text and the underline. The second TextView acts as your underline so to adjust its thickness you can change the "layout_height" value.
The final step to making this work is to go into your "values" folder in your project and create a new XML file named "colors.xml". The entire contents for this example are below:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="underline">#333333</color>
</resources>
Simply change the hex color value in this XML file to customize the underline color to your choice.

How to set line-height textView on Android

I give below attributes that I use textView in my application. ( Green text in middle )
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:gravity="center_horizontal"
android:text="10/9"
android:id="#+id/estimate_fertility_date"
/>
However, there are spaces like the example picture. I want to set these spaces because when App initialize, It seems awful.
I have researched about 1 hour and I found some answers but They are not which I want answers.
https://stackoverflow.com/a/6864017/2834196
It 's not easy to get absolutely no text padding in a normal TextView. One way is to can set a minus margin for the TextView, or just use textview.setIncludeFontPadding(false) to reduce padding to some degree.
Adding negative values to margin does it help you?
<TextView
android:layout_marginTop="-10dp" />

Android - Is it possible to overlay text on text?

I want to create special characters for math, for an android application. And I wonder if it's is possible to overlay one character on top of another, or if there is some fine control on how text is rendered (and how do you go about doing that).
If you need a general method to display math equations, have a look at jqMath. I think you can render it using a WebView.
Many math symbols have Unicode code points. Specifically, the Unicode code point for the symbol of a '+' inside a circle is U+2295. They can be rendered directly if the font supports it. For a list of some math symbols with corresponding Unicode code points check this Wikipedia article.
Also have a look at this question for resources using MathML in Java.
I would extend the View class and then use the drawText method of the Canvas object you receive in the onDraw method. It sounds like you need fine control over coordinates of where text is being painted and extending View would give you just that. Take a look at Canvas.drawText and you can use the x and y coordinates to overlay text as you require.
Try this:
This way you can add Superscript text :
TextView out_unit2 = (TextView) findViewById(R.id.out_unit2);
out_unit2.setText((Html.fromHtml("meter<sup><small>2</small></sup>")));
Subscript text :
TextView out_unit2 = (TextView) findViewById(R.id.out_unit2);
out_unit2.setText((Html.fromHtml("meter<sub><small>2</small></sub>")));
You can use this to add as many to your code.
Hope it helps you.
Thanks.
It could get really messy really quickly, but you would be able to accomplish it with a FrameLayout and several TextViews inside. For example, XML for a "0" overlapped with a "+", superscript "+" and subscript "-":
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="#dimen/title_bar_font_size" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:textSize="#dimen/title_bar_font_size" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:textSize="12sp"
android:layout_marginLeft="12sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:textSize="12sp"
android:layout_marginLeft="12sp"
android:layout_gravity="bottom" />
</FrameLayout>
Resulting in:

How to wrap text in textview in Android

Does any one know how to wrap text in TextView in Android platform. i.e if the text in textview exceed the screen length it should be displayed in the second line.
I have searched and tried the following:
android:scrollHorizontally="false",
android:inputType="textMultiLine",
android:singleLine="false"
But none work..
Can anyone suggest how can I do it.
Constraint Layout
<TextView
android:id="#+id/some_textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="#id/textview_above"
app:layout_constraintRight_toLeftOf="#id/button_to_right"/>
Ensure your layout width is zero
left / right constraints are defined
layout height of wrap_content allows expansion up/down.
Set android:maxLines="2" to prevent vertical expansion (2 is just an e.g.)
Ellipses are prob. a good idea with max lines android:ellipsize="end"
0dp width allows left/right constraints to determine how wide your widget is.
Setting left/right constraints sets the actual width of your widget, within which your text will wrap.
Constraint Layout docs
For me this issue only occurred on Android < 4.0
The combination of parameters I used were:
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false"
The maxLines count seemed to be the random final piece that made my TextView wrap.
For the case where the TextView is inside a TableLayout, the solution is to set android:shrinkColumns="1" on the TableLayout. (Replace 1 with the column number the TextView you want to wrap is in. (0-indexed))
AFAICT, no other attributes are needed on the TextView.
For other cases, see the other answers here.
FWIW, I had initially gotten it to sort of work with
<TextView
android:id="#+id/inventory_text"
android:layout_width="fill_parent"
android:layout_weight="1"
android:width="0dp"
but that resulted in some extra empty space at the bottom of the Dialog it was all in.
Use app:breakStrategy="simple" in AppCompatTextView, it will control over paragraph layout.
It has three constant values
balanced
high_quality
simple
Designing in your TextView xml
<android.support.v7.widget.AppCompatTextView
android:id="#+id/textquestion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:scrollHorizontally="false"
android:text="Your Question Display Hear....Your Question Display Hear....Your Question Display Hear....Your Question Display Hear...."
android:textColor="#android:color/black"
android:textSize="20sp"
android:textStyle="bold"
app:breakStrategy="simple" />
If your current minimum api level is 23 or more then in Coding
yourtextview.setBreakStrategy(Layout.BREAK_STRATEGY_SIMPLE);
For more refrence refer this BreakStrategy
You must use 2 parameters :
android:ellipsize="none" : the text is not cut on textview width
android:scrollHorizontally="false" the text wraps on as many lines as necessary
This should fix your problem: android:layout_weight="1".
By setting android:maxEms to a given value together with android:layout_weight="1" will cause the TextView to wrap once it reaches the given length of the ems.
OK guys the truth is somewhere in the middle cause you have to see the issue from the parent's view and child's. The solution below works ONLY when spinner mode = dialog regardless of Android version (no problem there.. tested it in VD and DesireS with Android =>2.2) :
.Set you spinner's(the parent) mode like :
android:spinnerMode="dialog"
Set the textview's(child custom view) properties to :
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false"
I hope this works for you also.
In Android Studio 2.2.3 under the inputType property there is a property called textMultiLine. Selecting this option sorted out a similar problem for me. I hope that helps.
Just was working on a TextView inside a layout inside a RecyclerView. I had text getting cut off, ex, for Read this message, I saw: Read this. I tried setting android:maxLines="2" on the TextView, but nothing changed. However, android:lines="2" resulted in Read this on first line and message on the 2nd.
Try #Guykun's approach
android:layout_weight="1"
android:ellipsize="none"
android:maxLines="100"
android:scrollHorizontally="false"
Also, make sure that parents width is not set to wrap content. This is the thing that I was missing.
I had the same problem. Following change made it work -
android:layout_width="wrap_content"
The ellipsis, maxLines, or layout_weight - all didn't make any difference.
Note - The parent width is also set as wrap_content.
All you have to do is to set your textview width.
android:layout_width="60dp"
you can change the width to your choice. Just type long sentence to check if it working like this
android:text="i want to be among world class software engineer"
I am using Android 2.2 and my textview will automatically goto the next line if it exceeds the screen.
If you would like to have the text goto the next line before the end of the screen, just add in (just put in your own dp value). This will be useful if you have a picture on the right of the text.
android:layout_marginRight="52dp"
Strange enough - I created my TextView in Code and it wrapped - despite me not setting anything except standard stuff - but see for yourself:
LinearLayout.LayoutParams childParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
childParams.setMargins(5, 5, 5, 5);
Label label = new Label(this);
label.setText("This is a testing label This is a testing label This is a testing label This is a testing labelThis is a testing label This is a testing label");
label.setLayoutParams(childParams);
As you can see from the params definition I am using a LinearLayout. The class Label simply extends TextView - not doing anything there except setting the font size and the font color.
When running it in the emulator (API Level 9) it automatically wraps the text across 3 lines.
Just set layout_with to a definate size, when the text fills the maximum width it will overflow to the next line causing a wrap effect.
<TextView
android:id="#+id/segmentText"
android:layout_marginTop="10dp"
android:layout_below="#+id/segmentHeader"
android:text="You have the option to record in one go or segments(if you swap options
you will loose your current recordings)"
android:layout_width="300dp"
android:layout_height="wrap_content"/>
The trick is with the textView width, try to make it dedicated number like:
<TextView
android:layout_width="300dp"
android:layout_height="wrap_content"/>
I've tried many solutions without any result, I've tried:
android:ellipsize="none"
android:scrollHorizontally="false"
the only one thing triggred the wrap option is the dedicated width
You need to add your TextView in a ScrollView with something like this :
<ScrollView android:id="#+id/SCROLL_VIEW"
android:layout_height="150px"
android:layout_width="fill_parent">
<TextView
android:id="#+id/TEXT_VIEW"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header This text view should act as header" />
</ScrollView>

Categories

Resources