aspose file tools*
The moose likes Android and the fly likes A simple editor with rich text in Android Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "A simple editor with rich text in Android" Watch "A simple editor with rich text in Android" New topic
Author

A simple editor with rich text in Android

vani golakoti
Ranch Hand

Joined: May 27, 2010
Posts: 31
How to implement a editor with font styles, sizes and colors in android. And how save the note and open it after creating the note with all these styles.

Regards,
Bhavani.G
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Hi Bhavani,

I'm unfamiliar with Android, so I'm not sure if it supports the full standard API, but if it does, you might want to take a look at javax.swing.JTextPane.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
No, Android has neither AWT nor Swing. But an EditText view can display text in various styles. Check out the android.text package, particularly Spannable, SpannableString and SpannableStringBuilder, and the related android.text.style package and the android.graphics.Typeface class. That's what I use in the Styled WikiNotes app (see my signature). Also check out the original WikiNotes app on which it is based. While that does not deal with styles, it shows how to edit, display and store text.


Ping & DNS - my free Android networking tools app
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

Moving to our Android forum.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
vani golakoti
Ranch Hand

Joined: May 27, 2010
Posts: 31
Yes, Android does not supports awt and swings. Already I gone through Spannables but once applying the styles how to save the content with styles and retrieve it. Can you please post any proper example to apply styles with spannables and saving.(Every thing should be dynamic)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
The easiest would probably be to keep track of styles in memory in a triple format of (style, index of first character, index of last character) and to store that along with the actual text. Maybe in Properties format.

You still have to decide between various ways of storing data: http://developer.android.com/guide/topics/data/data-storage.html. Shared Preferences is certainly a bad approach, but the other four might be viable, depending on your requirements.
vani golakoti
Ranch Hand

Joined: May 27, 2010
Posts: 31
Hi Ulf Dittmer,

"The easiest would probably be to keep track of styles in memory in a triple format of (style, index of first character, index of last character) and to store that along with the actual text. Maybe in Properties format" . I did not under stand what exactly it is saying. I mean dynamically we have to get the selected text and high light it with styles and once the bold is enabled whatever we are typing it should be bold like that. So other than spannables what is the exact way to do this. Once applying the styles is finished then I can go for saving part. And one more thing is we have so many commercial apps with rich text, how they are achieving this, are they using any libraries?

Thank you.
jetti madhu
Greenhorn

Joined: Feb 22, 2010
Posts: 7
Hi Ulf Dittmer,

The easiest would probably be to keep track of styles in memory in a triple format of (style, index of first character, index of last character) and to store that along with the actual text. Maybe in Properties format.

We can store styles start and end index. In the case of Bold, JETTI , for this I store 2 is the start index and 4 is the last index.
And later he changed that to JETTI , TI as Normal text.. Again we have to store the indexes.. It may work but takes more complicated to look into this approach.. Or else is there any other Idea from your side?

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
So other than spannables what is the exact way to do this.

Spannables *are* the way to do this; what else are you looking for?
vani golakoti
Ranch Hand

Joined: May 27, 2010
Posts: 31
Is it the only way to do this. It is some what difficult while removing the applied style because removespan() method has no parameters for selected text, so it removes the total applied style and further and is difficult to get all the properties.And somebody suggested HTML I don't know what exactly what is the way with HTML's. Let me know if you have any idea, and any libraries for that.

Thank you.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
It is some what difficult while removing the applied style because removespan() method has no parameters for selected text, so it removes the total applied style and further and is difficult to get all the properties.

The way to remove styles is probably to start over with the unstyled text, and then to successively apply all the styles that you have stored (after removing the now-obsolete style from the list, obviously).
vani golakoti
Ranch Hand

Joined: May 27, 2010
Posts: 31
Could you please simplify your explanation for me.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
There isn't much to simplify - you were asking about how to remove a style. So, you start out with a list of, say, 17 styles that are used in the text, each of them in the shape of a triple as I described earlier. Now you remove one style from the list. To display the other styles, you start with the original unstyled text, and apply all remaining 16 styles to it. Done!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A simple editor with rich text in Android