0

Hello Everyone I am making an app where the user can italicize spannable text on a button click.

// Captures Contextual Menu Clicks//
public void onContextualMenuItemClicked(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.bold) {
        // do some stuff
    }

    if (id == R.id.italic) {

        int startSelection = noteContent.getSelectionStart();
        int endSelection = noteContent.getSelectionEnd();
        Spannable spannable = noteContent.getText();

        spannable.setSpan(new StyleSpan(Typeface.ITALIC) , startSelection , endSelection , 0);



        StyleSpanRemover spanRemover = new StyleSpanRemover();

        spanRemover.RemoveStyle(spannable,startSelection,endSelection,Typeface.ITALIC);
    }

this code makes it italic

        int startSelection = noteContent.getSelectionStart();
        int endSelection = noteContent.getSelectionEnd();
        Spannable spannable = noteContent.getText();

        spannable.setSpan(new StyleSpan(Typeface.ITALIC) , startSelection , endSelection , 0);

This makes it un-italic

        StyleSpanRemover spanRemover = new StyleSpanRemover();

        spanRemover.RemoveStyle(spannable,startSelection,endSelection,Typeface.ITALIC

I want the user to click the same button to italicize and un-italicize. What if statment can I use to get what TypeFace the spannable selected words are? That way it knows if to italicize or not

UPDATE: this is what I have now

     int startSelection = noteContent.getSelectionStart();
        int endSelection = noteContent.getSelectionEnd();
        Spannable spannable = noteContent.getText();

        StyleSpan[] spans = spannable.getSpans(startSelection, endSelection, StyleSpan.class);

        if (spans.length == 0) {

            spannable.setSpan(new StyleSpan(Typeface.ITALIC), startSelection, endSelection , 0);

        } else {

            spannable.setSpan(new StyleSpan(Typeface.NORMAL), startSelection, endSelection , 0);
        }

even though the code runs the code

  spannable.setSpan(new StyleSpan(Typeface.NORMAL), startSelection, endSelection , 0);

and sets it back to normal. but it wont set spans.length back to zero so I cant italicize it again how to i set spans.length to zero.

Jordan
  • 407
  • 5
  • 20

1 Answers1

0

You can use getSpans(startSelection, endSelection, StyleSpan.class) to see what spans are present in the selection range. Based on that, you can decide whether (and how) to italicize the text. Note the user might highlight a portion that contains both normal text and italicized text.

EDIT

StyleSpan[] spans = spannable.getSpans(startSelection, endSelection, StyleSpan.class);
if (spans.length == 0) {
    // no spans, italicize text here
} else {
    // TODO
}
Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • how do I use this in an if statement? Thanks Jordan :) – Jordan Sep 11 '15 at 00:01
  • If there are no spans in the range, then it must not be italicized. Otherwise, something is italicized. – Karakuri Sep 11 '15 at 00:08
  • I get the premises of that, yes. I just can't figure out how to put it into code, if (getSpans(startSelection, endSelection, StyleSpan.class) = ?) { } – Jordan Sep 11 '15 at 00:12