I’m trying to get some code which will for example change
“^^text^^”
Into something like this
“text“
So I need it to remove the ^^ tokens and add a Spannable Text on the “text” string making it bold.
Right now I have this code.
public void format()
{
TextView textView = (TextView) findViewById(R.id.test);
CharSequence text = null;
if(textView != null)
{
text = textView.getText();
}
String token = "^^";
if(text != null)
{
int length = text.length();
int start = text.toString().indexOf(token) + length;
int end = text.toString().indexOf(token, start);
if (start > -1 && end > -1)
{
SpannableStringBuilder ssb = new SpannableStringBuilder(text);
ssb.setSpan(new ForegroundColorSpan(0x000000), start, end, 0);
ssb.delete(end, end + length);
ssb.delete(start - length, start);
text = ssb;
System.out.println("format");
}
System.out.println("works");
}
textView.setText(text);
System.out.println("running");
}
This code gets called in the onCreate which looks like this.
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
format();
setContentView(R.layout.activity_tutorial_basic_file_test);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
This is the XML layout file I’m using.
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="@string/empty" />
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginTop="2dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/test_text"
android:textColor="#FFFFFF" />
</LinearLayout>
</ScrollView>
And finally the string I’m using in a resource file.
<string name="test_text">Test string ^^ for ^^ formatter code</string>
What am I doing wrong or how should I do this?
You have several problems here:
In
onCreateyou callformat()before setting the content layout. First set layout, then operate on views that are declared inside it, i.e.:In format you mistakenly define
lengthas length of thetext. Looking at the code, it should definitely be length oftoken:Remember that providing color values as
intvalues often means you should explicitly set alpha value. If you don’t the alpha is set to0(i.e. transparent). You use0x000000(transparent black) inForegroundColorSpan, but it’s more common to set0xff000000(opaque black). If that’s the case, change the line to:You should set the
TextViewcontent to theSpannableStringBuilderdirectly. This is becauseTextViewautomatically sees that it is aSpannableand applies your desired formatting. Whereas setting it to a string representation of theSpannableStringBuilderdoesn’t apply formatting at all. So you should replace the line:with:
and delete the latter:
The final code is as follows: