can the layout of text in android views be controlled? - android

I am looking to format text line by line in a text view or similar. I want to be able to have line by line justifcation and formatting, almost like a word document (though nothing like as extensive)
I basically need to be able to have a continuous document with some parts right justified, some parts centered. Some bold, some not and some with a coloured background and some white. Also alowing the user to choose font.
Can someone recommend an decent approach to this please.
thanks

TextView/EditText operates on fairly rich text described by the various style span objects here: http://developer.android.com/reference/android/text/style/package-summary.html
For example you can use this to build text containing those various style spans: http://developer.android.com/reference/android/text/SpannableStringBuilder.html
SpannableStringBuilder is-a CharSequence, so it can be set as the text in a TextView. Though more often you probably just want to use TextView.getEditableText() to return the Editable interface to the text inside of the text view which allows you to modify its contents including style spans.
TextView supports most of what you are asking for through style spans, though justification is applied per-paragraph and not per-line. (Per-line is weird... I don't think even Word does such a thing?)

I would recommend using multiple TextViews. Possibly, one for each line if you really need the text that different. You can customize each one however you want fairly easily. If you haven't already, take a look at the documentation: http://developer.android.com/reference/android/widget/TextView.html
Then, you could use a RelativeLayout or a LinearLayout to position them however you need. Just ask if you have any other questions.

Related

How do I apply styles to edit-text while typing in android?

There are 2 options available to apply edit-text style on fly.
One is concat the string with html tags and set text by using Html.fromHtml method.
Second one is create span objects based on the selected style and set the span into spannable object.
Now my doubt is "which is better way to apply or change the edit-text style on typing?".
Can someone explain help me?
Spannable is much more better solution because its faster compared to html process. Also have an easy framework to work with it like SpannableStringBuilder. You can find another libraries to create spannables on the web. Most important thing is in my opinion you can recycle Spannable objects on fly which is memory-friendly.

Is it possible to insert/paste image on a custom EditText?

I wanted to create a custom text view. A little bit advance just as the text editor found here in StackOverflow, I can insert text and even images. I wanted to name this as WordView and create this as a library so I can reuse this across multiple projects.
Unfortunately I am not so sure if the base class allows this. I need a way to insert/paste images directly in the EditText. I am planning to write a custom View, although it seems laborious, I am not quite certain if this is possible at all and if I am going into the right track.
Have anyone tried similar before?
According to the Android Documentation for a TextView You can have a drawable object inside the TextView in the following ways
DrawableBottom
DrawableEnd
DrawableLeft
DrawableRight
DrawableStart
DrawableTop
There are other methods you can use, check out:
1. Programmatically set left drawable in a TextView
2. How to programmatically set drawableLeft on Android button?
3. http://androidsbs.blogspot.co.za/2013/12/androiddrawableleft-set-drawable-to.html
You could possibly implement this using HTML. I'm not sure about the paste functionality but generally it might work.

EditText XML Design

I'm looking to create an EditText that looks like the following & was just wondering if it was possible & if anybody might be able to provide example XML code to create the following.
Note: The cursor doesn't have to be blue, if it can be that's cool, but definitely not required.
This is just an EditText with a custom background (the underline with the curved shape). You would just use a standard EditText, and add your custom drawable (however you make that - either a PNG, 9Patch, ShapeDrawable, etc) as the "background" element. The text label would just be a TextView you put in your layout above the EditText.
However, I would steer you to use the new "TextInputLayout" provided in the Design Support Lib. This has a ton of built in functionality, and as a bonus, your app will be consistent with Material Design standards.

Multiple fonts associated with same TextView

Is there any way to set different fonts for different states in a TextView?
Let's say I want Helvetica Regular for normal state, and Helvetica Bold for pressed?
I know how to link a custom font with a text view, but not sure how to do same for multiple fonts and single TextView? Specifically, is there a way to achieve this behaviour through xml?
Update: I'm not looking into workarounds, like having HTML in TextView, or even replacing TextView with WebView. If it's not possible to achieve, I'd rather have one font
check this link may help you:
http://shuklaxyz.blogspot.com/2012/05/custom-fonts-for-webview-and-textviewv.html
also :
Custom fonts and XML layouts (Android)
also:
Custom TrueType font from xml only
and this :
http://polwarthlimited.com/2013/05/android-typefaces-including-a-custom-font/
hope that help.
Using Html.fromHtml comes in handy, but it is limited to the tags you can use. I personally only have used this to do Bold, Colors, italics.
Like this:
String myWords ="<FONT COLOR = "#992211">This is Red:</FONT><br>";
myTextView.setText(Html.fromHtml(myWords));

Create a nice "About" XML Layout using rich text?

I intend to make a good "About" section in my application that gives the user some basic information about the developer and the app itself.
I didn't find a rich EditText among the Widgets.
I have searched for other questions similar to mine here and I found something like using a WebView or a TextView and giving them HTML attributes in the code.
Is there any nice and professional way to do my About XML Layout?
use multiple TextViews and position them accordingly.
If you need high control and rich text, use a WebView and load a static HTML file. You can achieve a lot with Spans (bold, itallick, links, etc.) in a simple TextView as well but it is generally more work.

Categories

Resources