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.
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.
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.
you can do by this way...
text1.setOnLongClickListener(new OnLongClickListener()
{
#Override
public boolean onLongClick(View arg0)
{
text.setText("Your Text");
return false;
}
});
Related
I am using a Button to check text entered into an EditText. After the button click event I don't want to allow any more text to be entered.
I previously used setEnabled(false) but the problem with this is the software keyboard is then closed, which means the user has to reopen it which is very inconvenient (the process in a loop is essentially: text entry --> button1 (stop text entry) --> button 2 --> text entry etc...). I tried to avoid this by adding in the code:
setEnabled(false);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
But, I notice this creates a short flash sometimes as the keyboard is quickly minimized and then restored again.
There must be another way to stop additional text alteration without losing the focus of the EditText?
Given the situation you are describing, the only thing I can think of would be to store the value and restore it at the end of your operation (as opposed to disabling the text box). It's not a very correct approach, but it should work...
You could use an InputFilter :
int currentTextSize = editText.getText.toString().length();
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(currentTextSize);
editText.setFilters(FilterArray);
and to remove it :
editText.setFilters(new InputFilter[] {});
You could of course declare those filters as static in your class if they are to be used often... This might not be the best way to achieve this but it's the first I thought of ;)
I managed to implement a solution to this that I was happy with, although its not a particularly elegant solution. For anyone who might stumble across this in the future:
I created the following EditText in my xml which provides an EditText to temporarily receive focus and means the keyboard remains open. Note the width and height are set to 0px so it is a hidden object.
<EditText
android:id="#+id/switcher"
android:layout_width="0px"
android:maxLength="1"
android:layout_height="0px"
android:textSize="10sp"/>
Then in my button click event I added the following code:
EditText attempt= (EditText) findViewById(R.id.attempt);
EditText switcher= (EditText) findViewById(R.id.switcher);
switcher.requestFocus();
attempt.setFocusable(false);
This moves the focus to hidden EditText and then stops the attempt text object from being able to be edited again. It can then be removed on another action with:
attempt.setFocusable(true);
attempt.setFocusableInTouchMode(true);
attempt.requestFocus();
Perhaps switcher.setFocusable should be changed between the two (but in the opposite way)
I am making a simple shopping cart application where the user can select items (via buttons) and their running total will be displayed in a text view.
I am fine with having the text view being updated on a single click but I am struggling to figure out how to write my code if the same button is pressed more than once. For example a button writes a value of £3 into the TextView, if this button was clicked again I want the TextView to increase to £6 and so on.
Further to this I want to be able to have more than value added to the TextView from different buttons. I imagine this is more of a Java question as opposed to Android but seeing as I'm a bit of a newbie to both all advice is welcome!
Into your class if you have price value add an incerementPriceValue and make get/set functions for the last one.
If user press the button
setIncrementValue(getIncrementValue()+priceValue);
setText(getIncrementValue+simbolstring);
Create a int variable count and on the click increase the count value and set that value on the textView
I have two EditText fields, and one button.
i want to do the following:
if the EditText1 && EditText2 == empty
then
button is disabled
else
button is disabled
the button should be enabled only if the user write something in both fields,
in simple words:
if the user write something in both fields, the button will be enabeld,
if he delete what he write then disable the button again.
i will be regretful if any one answer me.
android 2.2.
You want TextWatcher. Use this to determine when text is changed in both. You could either do an 'if else' every time text is changed or in a while loop. Hope this helps. And I think you will be grateful not regretful for help :)
I have three EditText boxes in an activity, for two of which normal input methods (hard keys, default soft keyboard) are ok. But for one of the EditText boxes I want to send soft input only from a custom keyboard view. So in effect I want the default soft keyboard never to be shown for this EditText. I've tried adding onTouchListeners and onFocusChange listeners for the EditText with partial success like this:
public boolean onTouch(View v, MotionEvent event) {
v.requestFocus();
imm.toggleSoftInput(0, 0);
return true;
}
public void onFocusChange(View v, boolean hasFocus) {
InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm.isActive(v)) {
imm.toggleSoftInput(0,0);
}
}
But I have not achieved a definitive solution because
1)the default soft keyboard always briefly flashes visible before the listener hides it
2)on some occasions, such as moving focus to the EditText with hard keyboard arrow keys sometimes sets the default soft keyboard visible
and so on.
So I would love to find a simple way to tell Android never to show the default soft keyboard for this specific EditText. I would not like to extend EditText and start to override stuff, since the EditText functionality is perfect for me - I just want the default soft keyboard not to be shown.
I've spent days now trying to figure this out. Some topics (including some here) found via google have half-way attempts at this problem, but so far I haven't found a single totally functional solution.
EDIT:
I'm really starting to get annoyed. I decided I could try not to use EditText but whatever other view that will get the job done. It turns out it is freakin hard to get rid of that soft keyboard. It even shows up when I use the hard keys to move focus from an EditText to a Button! Why on earth should the soft keyboard be shown on every freakin View that happens to have focus? Even when I explicitly say inputType="none"? How do I turn that * soft keyboard OFF? Below is the xml for the Button - let's use that as an example:
<Button
android:id="#+id/OkButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="none"
android:paddingRight="5mm"
android:paddingLeft="5mm"
android:layout_below="#id/Volume"
android:layout_alignParentLeft="true"
android:text="OK"/>
EDIT2:
I have how achieved a solution that seems to work. First I get a hold of the InputMethodManager:
this.imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
and the I set OnClickListener, OnTouchListener and OnFocusChange listener all call the following method when I want the EditText to be focused and my custom KeyboardView visible, while hiding the default soft input:
private boolean makeActive(View v) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
EditText e = (EditText) v;
int iType = e.getInputType();
e.setInputType(InputType.TYPE_NULL);
e.requestFocus();
showKb();
e.setInputType(iType);
return true;
}
Simple
editText.setShowSoftInputOnFocus(false);
Some people suggested that the following might work on older versions of Android, but the behaviour is unexpected.
edittextPlay.setTextIsSelectable(true);
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(searchBox.getWindowToken(), 0);
where SearchBox is your textbox or better yet instead of SearchBox get your current displayed window.
Or try:
InputMethodManager imm = (InputMethodManager)getBaseContext()
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
where context is getApplicationContext();
I have an EditText that recieves input and prints in on the screen. I need to find a way wherein after click submit or enter, the phone will focus on the EditText
I tried
txtJoke.setFocusable(true);
txtJoke.setFocusableInTouchMode(true);
txtJoke.requestFocus();
and it doesn't work..
requestfocus works during onCreate..
I'm thinking of manually triggering a click an edittext..
You can use ...
Button newButton.setOnFocusChangeListener(new View.onFocusChangeListener) {....}
to specify where will be the focus go next.