Using Different Font Styles in TextView - java

I can display the contents of a file in a TextView by adding the file under the raw folder and reading the file. It works fine. But all of the contents in the file look the same. I want to differentiate titles and the contents by introducing Font styles. Can anybody help?

What I understood from your question is that you want to apply different formatting to different parts of your text in TextView. But As I know, TextView doesn't supports multiple styles (formatting) (yet). Whatever the formatting you apply, will be applied to the whole text in TextView. To use different formatting, you can use the WebView.
Update:
This functionality is available for EditText (I saw it too late), see this link for details; http://developer.android.com/resources/faq/commontasks.html#selectingtext

You can use WebView instead as your text is static you can format it to create a htmlPage and then display it in webview something like this
WebView mView = (WebView) findViewById(R.id.help_text);
mView.loadUrl("file:///android_asset/help.html");
mView.getSettings().setSupportZoom(false);

Related

Modify included layout in Android?

I am using an include tag in an Android layout file to include a relative layout row that I would like to repeat a few times. The problem is that after I include the row I cannot modify the text of a textview inside the layout. Is there anyway I can modify the text of a textview that is part of an include? Mainly I would like to insert 4 of these rows with custom text specified in the xml if possible.
You can do it only at runtime. Even if you do it at runtime, make sure you call findViewById from the parent view as Android doesn't allow you to use the same ID at more than one place in an xml file.
You can either add these 4 includes with a different id and get them from code or do what #Anis said and inflate them at runtime and put there your text.

Set Link Clickable in Java-Android

I am designing an App, where there is TextView, and based on some check condition, I want to make links / phone numbers in the TextView clickable in Java side. Can I do this, I don't want to make it clickable by default which is done by setting in xml file
android:autoLink="all"
What I already tried:
Create regular TextView and don't set autoLink="all",
TextView myView =
(TextView)view.findViewById(R.id.thisIstheTextView);
myView .setLinksClickable(true);
also tried :
myView .setMovementMethod(LinkMovementMethod.getInstance());
None of the above Java code works, I don't want a predesigned xml TextView with autoLink enabled, I want to change the TextView behavior in Java code based on if () conditions. There is no error in my code, but I am not able to achieve what I want. Can you please share your knowledge. Thank you.
Use setAutoLinkMask (int mask).
The possible values that can be combined for mask are those defined for the Linkify class.
Linkify.addLinks(myView, Linkify.ALL);
Fixed the issue.

How to write the "to the power" and some text like as shown in Java or Android?

As in the images shown below, is it possible to write code to write "to the power" in Android or Java?
Something like:
TextView t = (TextView) findViewById(R.id.text);
t.setText(Html.fromHtml("7<sup>2</sup>"));
According to Android documentation on their FAQ, you can always do this using a string resource, but if you do it on the fly you must make sure the TextView is using Spannable storage (always true if it's an EditText for example).
See Selecting, Highlighting, or Styling Portions of Text in the Android Common Tasks.
You can write HTML Code to show something like this.

Android: Adjusting EditText font size to fit text to one line

I am making an app that has two EditText views, txtOne which is used for input, and txtTwo which is used for output. txtOne is editable, and txtTwo is not.
I am looking for a simple way to scale the font size of the text in these two views so that it always fits within the view without wrapping to a new line.
I have found a few implementations of this using custom views extending TextView, but they aren't fit for an EditText view, or use android.graphics.Paint, which is not what I am looking for.
Is there any way to check to see when text in an EditText view is wrapping? If so, then it would be easy enough to do something like:
if(txtOne.isWrappingText()) {
txtOne.setTextSize(txtOne.getTextSize() - 2);
}
Does anyone know of any way to detect this or an alternative solution?
I deleted my post
Basically the suggested code contained a TextWatcher for the EditText, this helped to answer the question. But the code itself was just wrong (I tested it meanwhile).
I suggest to read this question and answers because they adress the very same issue...

Replacing smiles with images in Swing text area

I am designing a chat system..i am making use of JText area to display chat, and JTextField to enter text.
My question is how to recognize smiley's like ":)" and replace it by corresponding image
on text area? i found no method which will append image on text area..Please help.
http://java-sl.com/tip_autoreplace_smiles.html
You'll have to use a read-only JEditorPane to display HTML instead of a JTextArea in that case.
JTextArea was made to display only multiple rows of text, but correct me if I'm wrong. To display Images you could use the JEditorPane control that will allow you to use html, with simple <img /> tags which will point to a image.
Regarding how to recognize smileys you could create a file / list of common patterns that you would like to support and then simple check if the text contains the pattern with the .contains , or even Regular Expressions.
Update
And with the JEditorPane you will also be to do addition things like say scan for emails or links and automatically convert them so the user can click them, always a nice feature to have.

Categories

Resources