Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is EasyEditSpan used in Android to allow partial text editing?

I want to allow the user to edit just part of a line of text in my Android app. I see a class called the EasyEditSpan but when I stick it into a TextView nothing happens. I tried making the TextView editable but it still doesn't have any effect. If switch to an EditText then the whole line of text is editable which is also incorrect. Here is my code:

@Override
public void onCreate(Bundle savedInstanceState) {
    TextView testView = (TextView)findViewById(R.id.text_view);
    testView.setText(buildMiddleEditSpannable("Please enter your ", "Name", " here."));
}

public static Spannable buildMiddleEditSpannable(CharSequence beginning, CharSequence middle, CharSequence end) {
    int spanMidStart = beginning.length();
    int spanMidEnd = spanMidStart + middle.length();

    SpannableString span = new SpannableString(new StringBuilder(middle).insert(0, beginning).append(end));
    span.setSpan(new EasyEditSpan(), spanMidStart, spanMidEnd, 0);
    return span;
}
like image 432
satur9nine Avatar asked Jul 13 '12 01:07

satur9nine


People also ask

How do you use editable text on Android?

You can also use android:background="@null" . Edit: The TextView 's editable param does make it editable (with some restrictions). If you set android:editable="true" you can access the TextView via the D-pad, or you could add android:focusableInTouchMode="true" to be able to gain focus on touch.

What is Spannable text in Android?

Spans are powerful markup objects that you can use to style text at a character or paragraph level. By attaching spans to text objects, you can change text in a variety of ways, including adding color, making the text clickable, scaling the text size, and drawing text in a customized way.


1 Answers

After looking at the framework code referring to EasyEditSpan (EasyEditSpan, TextView and TextUtils), it become apparent that even though it says in its description:

Provides an easy way to edit a portion of text.

The currently available functionality is limited to just the second part of the description as follow:

The TextView uses this span to allow the user to delete a chuck of text in one click. the text. TextView removes this span as soon as the text is edited, or the cursor moves.

Here's some quick sample code that demonstrates its use:

public class EasyEditSpanActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final EditText editText = new EditText(this);
        setContentView(editText);

        showToast("Longclick to set EasyEditSpan for the line on cursor");

        editText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
        editText.setSingleLine(false);
        editText.setText("####\n#######\n###\n######\n####".replace("#", "text "));
        editText.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                final Layout layout = editText.getLayout();
                final int line = layout.getLineForOffset(editText.getSelectionStart());
                final int start = layout.getLineStart(line);
                final int end = layout.getLineEnd(line);
                editText.getEditableText().setSpan(new EasyEditSpan(), start, end,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                showToast("Edit line to show EasyEdit window");
                return true;
            }
        });
    }

    private void showToast(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

}

So unfortunately if you need a way to allow the user to edit just part of a line of text in your app, EasyEditSpan does not seem to help much. It is likely you will need to implement some code utilizing ClickableSpan and probably a custom dialog.

like image 189
Joe Avatar answered Sep 20 '22 00:09

Joe