Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform Redo Undo operation in EditText

Tags:

android

I want to know is there any method or any link or tutorial to perform redo undo operation in Android edittext. If any one knows than please let me know.

like image 211
Aamirkhan Avatar asked Jan 03 '12 07:01

Aamirkhan


People also ask

How do you implement redo and undo?

If “UNDO” string is encountered, pop the top element from Undo stack and push it to Redo stack. If “REDO” string is encountered, pop the top element of Redo stack and push it into the Undo stack.

How do you redo in text editor?

To reverse your last Undo, press CTRL+Y. You can reverse more than one action that has been undone. You can use Redo command only after Undo command. To perform a function on all the content in the text editor, you need to select it all.

How does the undo mechanism work in an editor?

Undo is an interaction technique which is implemented in many computer programs. It erases the last change done to the document, reverting it to an older state. In some more advanced programs, such as graphic processing, undo will negate the last command done to the file being edited.

How do I redo in Android Studio?

It is simple and default: ( CTRL + Z ) This is for undo. Just press ( CTRL + SHIFT + Z ) for redo.


1 Answers

Quick note on the Antti-Brax/Divers(Kidinov) solution. It works great, except if you try to use it with a TextView post-API 23, you'll run into problems, because guess-what, Google actually added a hidden UndoManager (android.content.UndoManager) and didn't document it or make it obvious it was there. But if you have a hard/bluetooth keyboard in Marshmallow or Nougat and hit ^Z or SHIFT-^Z, you'll get undo/redo.

The problem comes if you're already using Antti-Brax's class with an EditText, and you also hook it to ^Z and shift-^Z, you'll run into problems with anyone using a hard keyboard. Namely the ^Z will trigger BOTH the native and Antti-Brax's undo, leading to two undos simultaneously, which isn't good. And after a few of them, you'll probably get a Spannable out of bounds crash.

A possible solution I found is to subclass the TextView/TextEdit/whatever and intercept the undo/redo calls from the TextView so they don't run as follows:

    @Override
    public boolean onTextContextMenuItem(int id) {
        int ID_UNDO, ID_REDO;

        try {
            ID_UNDO = android.R.id.undo;
            ID_REDO = android.R.id.redo;
        } catch (Resources.NotFoundException e) {
            ID_UNDO = 16908338; // 0x1020032
            ID_REDO = 16908339; // 0x1020033
        }
        return !((id == ID_UNDO) || (id == ID_REDO)) && super.onTextContextMenuItem(id);
    }

Those magic id numbers were found here, and are used only as a backup if the android.R.id.undo values aren't found. (it also might be reasonable to assume that if the values aren't there the feature isn't there, but anyway...)

This is not the best solution because both undo trackers are still there and both are running in the background. But at least you won't trigger both of them simultaneously with ^Z. It's the best I could think to do until this gets officially documented and the getUndoManager() methods of TextView is no longer hidden...

Why they made a feature you can't turn off (or even know if it was there or not) "live" in released Android I can't say.

I just opened an issue on Android's issue tracker if anyone wants to follow this.

like image 100
fattire Avatar answered Sep 23 '22 20:09

fattire