Android TextView Lines Collection - android

I have a TextView that has 10 lines as a property and two variables, Alpha and Beta.
I want Alpha to use the first two lines of the TextView and Beta use the remainder of the lines or 3-10.
How do I code my Java? I cannot find a way to directly address the Lines collection.
This does not work:
TextView.setText.Lines[1]("This is a test\n for Alpha");
TextView.setText.Lines[3]("This is a test\n for Beta\n that has more lines\n than Alpha");
TiA Trey

You can't do that, the lines depend on the width of the TextView. If you want, you can use two TextViews, one with 2 lines and another one with 8.

Related

Is it possible to have TextView with maxLines=2 but if the text has one single long word not break it in 2 lines?

I have a TextView and I set text which I receive from Backend.
the text is either from 1 to 3 words.
Maximum the textview can be 2 lines.
I am using setAutoSizeTextTypeUniformWithConfiguration
and text.breakStrategy = LineBreaker.BREAK_STRATEGY_SIMPLE
And I don't have any success.
wondering is it possible if the text is single word I don't want to split it. I would like to have it in single line with small textSize. if the text is 2 words and long I am fine to show it in 2 lines. The problem is it always breaks the word in 2 lines if it is long while I don't want.
The only solution that comes to my mind is before setting the text to a textview, check whether text has spaces in it.
If it has not that that means it's a single word, so set ptogrammatically maxlines to 1, otherwise set maxlines to 2.
EDIT: I missed text size part. I use this library for autoresizing and it worked so far https://github.com/jivimberg/AutoResizeTextView
it's automatically handled in androidx.appcompat.widget.AppCompatEditText and just set android:maxLines="2"
remove all other properties you set

Styling Text inside Android TextView

In my app, each Target Text has a mix of numbers and strings. I want the numbers to be highlighted in Bold and string to be italicised. For e.g.
You have reached 25% of your goal. 30 more days to achieve target
Is there an easier way to do it than create 5 separate textviews for this - 3 for string and 2 for number. Managing layout of 5 textboxes for each target is becoming a nightmare in my layout. I have 9 such rows leading to potentially 45 textviews in one layout.
You can use <i> and <b> html tags within your string, like this:
<string name="my_string"><i>You have reached</i> <b>25%</b> <i>of your goal.</i> <b>30</b> <i>more days to achieve target</i></string>
You can read more here, under the Styling with HTML markup section: https://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
You can use the following example,
For Kotlin
textView.text = Html.fromHtml("<b>Hello</b> <i>World</i>")
For Java
textView.setText(Html.fromHtml("<b>Hello</b> <i>World</i>"))

How to change the below target in run time in android?

For example, I have a imageView 1 , imageView 2 and imageView3
In the XML I use a relative layout to group them
eg.
imageView1:
below of 2
imageView3:
visiblity: gone
after certain action (in run time)
eg.
imageView1:
below of 3
imageView3:
visiblity: visible
I think something similiar, but can not find the correct syntax
imageView1.setLayoutParams(new LayoutParams(Below));
Thanks for helping
What's your API level? If, by chance, you are using a min SDK of 17, you can just use removeRule(int verb) to remove the below of 2 and then add below of 3.
Otherwise, what I do is I keep two instances of RelativeLayout.LayoutParams for a given view. Try something like this:
belowTwoParams.addRule(RelativeLayout.BELOW, 2.getId());
belowThreeParams.addRule(RelativeLayout.BELOW, 3.getId());
Where belowTwoParams and belowThreeParams are both instances of RelativeLayout.LayoutParams. Keep in mind that your "2" and "3" views need to have valid IDs, set either in the XML or programatically before adding these rules.
Afterwards, you just call imageView1.setLayoutParams(belowTwoParams //or belowThreeParams accordingly);

Android TextView won't show right number of lines in Android 2.3

I have a couple of TextViews in my app, which should have exactly 3 lines. This works on Android 3+, but on Android 2.3, which I'd also like to support, these fields have exactly 2 lines. I've also tried TextView.setLines(3); in code, but that does not seem to help. This is my TextView:
<TextView
d1p1:lines="3"
d1p1:maxLines="3"
d1p1:minLines="3"
d1p1:ellipsize="end"
d1p1:text="Some Text"
d1p1:textAppearance="?android:attr/textAppearanceMedium"
d1p1:layout_width="fill_parent"
d1p1:layout_height="55dp"
d1p1:id="#+id/detailsTextView"
d1p1:layout_below="#+id/locationTextView"
d1p1:textSize="13dp"
d1p1:textColor="#color/app_darkgray"
d1p1:background="#color/app_dark_background"
d1p1:layout_marginLeft="#dimen/sr_horizontal_padding"
d1p1:layout_marginRight="#dimen/sr_horizontal_padding" />
Setting the height to wrap_content also does not work.
After some research it looks like my suspicion is correct. As discussed here (ellipsize multiline textview), Ellipsize causes issues with the min/max lines attributes. Because of this you may want to consider avoiding the Ellipsize otherwise, if that is not an option, you can follow the instructions in the other thread to use a custom TextView class to work around the problem.

2-column alignment in a Textview

I've got a TextView in my app that will, on each line, have a label and then a data value. For instance, some line could look like:
Pressure (atm) 0.983
Acceleration 10.277
Now, I have a handful of data values at once, the labels all of various character lengths. I want to data values themselves to be spaced over a bit from the labels, and all lined up, like so:
Pressure (atm) 0.983
Acceleration 10.277
Is there any way to do this? Thanks!
In my experience this is easier to do by separating value and label into two separate TextViews that you add to a LinearLayout for each row. And in the LinearLayout you can use layout weights to distribute it how you like.
You should add tabulator to the text.
The problem is how to add it in XML, because \t don't work. The solution is add that represents a tabulator.
For example in XML definition on Strings.xml
<string name="hello">Hello World, TesttabsActivity!</string>
In code you can also use the \t option:
TextView hello = (TextView) findViewById(R.id.helloTextView);
hello.setText("Hello\t\t\tWorld");
But this option don't align, only adds spaces (the XML solution also align)
Hope it helps.

Categories

Resources