1

I'm creating an app to track a score. The score is displayed in a TextView and when the user clicks the TextView it increments. This works perfectly! However, I would like to setup an onLongClickListener() to edit the text. So, when the user does a LongClick then I would like a digits only editor to pop up on the screen and when the user clicks OK or Done it will update the TextView value to the user inputted amount.

Can someone tell me how I can accomplish this please? This is my first real app so I'm a little puzzled as to how to accomplish this and all the searching on Google/Stackoverflow isn't helping.

Jim Thornton
  • 123
  • 1
  • 9
  • IMHO using onLongClickListener is probably not a good idea because there isn't any hint for user to touch and hold for long time and expect any outcome. Generally , users will never go for long clicks. You can always put a separate button besides your textview which is easily understandable to the user. – Purush Pawar Dec 21 '12 at 05:07

3 Answers3

2

Since the TextView is read only, what I've done in the past is have an EditText and a TextView in the same place. Done this way, you'll have to have code to show and hide the two views when you want to enter and exit "edit mode". So, in your onLongClick event, you'll hide the TextView and show the EditText. And on the Enter key (use a key listener on the EditText) and when the EditText loses focus (there's a focus listener as well), you'll do the opposite.

The other piece is how to limit the input to numeric input. Check this out: https://stackoverflow.com/a/7300516/53501

As far as what to do when the EditText loses focus, I would prompt the user to confirm or cancel the change. I'd personally use an AlertDialog for this (via AlertDialog.Builder).

Having read the comment that this is your first app, please comment if you'd like clarification on any of these things.

Community
  • 1
  • 1
Rich
  • 36,270
  • 31
  • 115
  • 154
0

Firstly create the view, which you would like to display (see here how to do that). Construct the dialog and inside the onLongClickListener - show the dialog. Also - assign an onClickListener for a button inside your dialog, which will apply the input to your TextView.

ılǝ
  • 3,440
  • 2
  • 33
  • 47
0

you can do by this way...

 text1.setOnLongClickListener(new OnLongClickListener()
 {  
        @Override
        public boolean onLongClick(View arg0)
        {
            text.setText("Your Text");
            return false;
        }
 });
Mehul Ranpara
  • 4,245
  • 2
  • 26
  • 39