How do i start a new line in String? [duplicate] - android

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
how do i create a multiline string in EditText?
i want to display a string in a multiline EditText. Currently, the string displays as....
Receive: 0 coin(s) Total
Cost: $100
I want want it to display as...
Receive: 0 coin(s)
Total Cost: $10
This is my code, I have tried to use "\n", but to no avail...
result.setText("Receive:"+ " "+ df.format(rawounces)+"coin(s)" + "\n"
+ "Total Cost:$" + df.format(transactionandpremium*rawounces));
Thank you in advance.

You can use the Html attribute to set text as you want as ::
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
String str="Receive: 0 coin(s) <br> Total Cost: $10";
editText.setText(Html.formHtml(str));

Related

How to Show double quotation with HTML code as text in Android (TextView) ?

I want to Show HTML code as Text in Tutorial App
String.xml
<string name="p1"><![CDATA[<html>\n<title> Font tag Example </title>\n<body>\n<font face="arial" size="1" color="blue"> WELCOME </font> <br>\n <font size="2" color="cyan"> WELCOME </font> <br>\n <pre>\n This text uses pre tag and preserves nextline and spaces\n </pre>\n </body>\n </html>]]></string>
prog1.xml
<TextView
android:id="#+id/p1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:textSize="18sp"
android:text="#string/p1" />
Show but without " ", Need Show with " " double quotation
require Show like this
to show double quote("") inside a textview use any of these
"
\"
here is the official android website
https://developer.android.com/guide/topics/resources/string-resource
Try it in your string.xml <string name="ffdfb">\"ffdfb\"</string>
or
<string name="ffdfb">"ffdfb"</string>
else try it in your xml android:text='"fvdfv"'

Override TextView ellipsize char('...') with ("... Read more")

I want to replace the char ('...') with a string ("...Read more").
Any easy way to do that by extending TextView and overriding a property?
I am expanding the TextView on click by increasing lines from 2 to 500 and vice versa. The TextView is in a ScrollView.
This code is XML File.
<com.google.android.material.textview.MaterialTextView
android:id="#+id/textDescriptions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="What is Android ? \nBefore learning all topics of android, it is required to know what is android.Android is a software package and linux based operating system for mobile devices such as tablet computers and smartphones." />
This code is Java file.
String readMore = "... Read more";
String shortDescription = textDescriptions.getText().toString().substring(0, 100);
String text = "<font color=#aaaaaa>"+shortDescription+"</font> <font color=#ff669900>"+readMore+"</font>";
textDescriptions.setText(Html.fromHtml(text));

Android TextView in ScrollView - Different color for each new line

I am creating sort of terminal-like view, that would display received and transmitted messages over Bluetooth,e.g. received messages in blue, transmitted in red.
So far, i managed to put textView inside scrollView and by using .append() methdod add lines to scroll view.
Layout looks like this:
<ScrollView
android:id="#+id/scrollingView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/disconnectButton"
android:layout_alignParentStart="true">
<TextView
android:text="Received and sent txt!!!\n"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollTextView"
android:minLines="11"
android:maxLines="11"/>
</ScrollView>
And code for adding text to scrollView:
scrollView = (ScrollView) findViewById(R.id.scrollingView);
scrollTextView = (TextView) findViewById(R.id.scrollTextView);
scrollTextView.setGravity(Gravity.BOTTOM); scrollTextView.setTextColor(getResources().getColor(R.color.receivedBTColor));
scrollTextView.append("$"+dataInPrint+"\n");
scrollView.fullScroll(View.FOCUS_DOWN);
The problem is that each of the lines should be able to be in different colors. setTextColor() method sets color for whole textView, which is totaly not what i want since i need to temporarily save the lines which go upwards until scrollView overflows. I looked at the example of using Spannable class, but it is quite messy.
Could anyone suggest a way to make a scrollable colorful text? Something like this?
Example from Bluetooth terminal app
Thanks a lot!
You can do a little tricky editing utilizing HTML and Android's Html.fromHtml()...
String dataInPrint = "" //Whatever the output message is.
String redB = "<font color="red">";
String blueB = "<font color="blue">";
String colorEnd = "</font>";
if(//message is send){
yourtextview.append(Html.fromHtml(redB + dataInPrint + colorEnd));
}
else{//message is recieve
yourtextview.append(Html.fromHtml(blueB + dataInPrint + colorEnd));
}

Setting right-to-left direction for Hebrew in an android app

I write an application in Hebrew but there is a problem with the direction of the words:
This is the code:
textView2.setText("יצרן: " + company + "\n");
textView2.append("דגם: " + degem + "\n");
textView2.append("מנוע: " + manoa + "\n");
textView2.append("ק'מ/ליטר: "+ watt + "\n");
textView2.append("נסיעה: " + timeString + " " + timeMedida+"\n");
I got:
יצרן:ABARTH
instead of
ABARTH:יצרן
and
דגם:005
instead of
דגם:500
the xml:
<TextView
android:id="#+id/txt_1_delek"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="50sp"
android:layout_marginRight="100sp"
android:layout_marginLeft="100sp"
android:text="myTextView"/>
In case someone stumbles upon this old question and is looking for an answer... Amir's link points toward the right direction.
What you need is to change the text's base direction (and not the alignment, so as explained in the link "gravity.RIGHT" wont help there). Inserting the "left mark" unicode character \u200F at the beginning of your string will do the trick.
BTW just adding it as a hardcoded character didn't work for me, apparently because of encoding issues. I prefered to avoid dealing with it in the code, so i simply extract the character as a string from strings.xml, let android do the conversion as it wishes, and append the string (in your case "יצרן: " + company + "\n") in realtime.

Add line break in Twitter (Android/Java)

I want to tweet some text to Twitter using line breaks.
I've tried \n or \r like in my code below, but Twitter is ignoring those.
tweet.setText(" Text1 " + "\n" + "Text2");
Thanks in advance!
try <br /> or [br /].
<br /> is the standard html tag, sometimes websites and forums use [br /].

Categories

Resources