I’m pretty new to android, Java and sqlite. For my first program I’m creating a program that will take user input and place in predefined text.
Example: “text” string1 “more text” string2 “even more text” etc
My text will be one color and strings will display in another color.
I’m using sqlite to seperate my data and code and this is where I hit a wall. Trying to find help on how I will be able to combine my above text into one row/column in my database table.
Using only one above example i could get this up and running. But there will be 50+ of above example for release making a database a must especially when I want to add more after release.
Most probably you’ve read up on
SpannableStringBuilder, which allows you to add color to the text in your TextView’s content. Check out the code below:The code above will work in most cases, however what you want is to have different alternating colors on a single TextView. Then you should do the following:
The above code will now work, but you’ll notice that if you add another text
your_another_textand want to use the previousfcsinstance for a second time, the previously coloredyour_textwill now lose its formatting (color). This time you’ll need to create another ForegroundColorSpan fcs3 to format the third part of the SpannableStringBuilder. The key here is to use a character style in asetSpanmethod only once. See below:This method is good if you know you have a fixed number of String elements in your SpannableStringBuilder. If you have wish to have a TextView of dynamic length and number of elements, you need to do this in a different approach. What worked for me was to convert each string element into a SpannableString, use
setSpan, andappendit to the TextView. This is useful if you’re using a loop to build your TextView.