IndexOutOfBoundsException in Edit Text for android - android

i have an edit text in my activity.i am entering numbers in it manually but
int mystart = destinationNumber.getSelectionStart();
int myend = destinationNumber.getSelectionEnd();
numberText.getText().replace(Math.min(mystart, myend), Math.max(mystart, myend),
"1", 0, 1);
its entering fine according to the cursor position.
i have a delete button in my acitivity which deletes single character according to cursor postion.
numberText.getText().delete(myend - 1, mystart);
But this logic is not working properly when i select whole text and call delete method it gives me IndexOutOfBoundsException OR i select 4-5 digits and call this delete.
I want the same functionality as android contact dialpad number enter field.Can someone help me figure out what is the correct logic to delete single digit from edittext and multiple selected digits as well.
Thanks

delete receives the start as first parameter and end as second, not the other way around.

Probably the error its taht mystart or myend(probably this) are bigger or smoller than numberText.lenght().
Try to put a Log.d("","" ) with the lenght of the text, mystart and myend and check if you need a myend -1 or something like that.

Related

How to select all text in a text field in monkeyrunner?

Actually I want to delete all text in a text field, and I am using a loop calling device.press('KEYCODE_DEL') to achieve this.
But there are two disadvantages:
Not efficient enough, especially when I don't know how many
characters in the text field so I need set a large enough loop
Need to move the cursor to the end before deleting
So I am trying to accomplish this by two steps:
select all text
press delete button
I found an similar question here which is not solved yet.
And there is an answer for how to select all text, but I think it has the same issues as my loop delete way.
I did several tests, and found a way close to it:
device.press('KEYCODE_MENU', 'MonkeyDevice.DOWN', '')
device.press('KEYCODE_A')
device.press('KEYCODE_MENU', 'MonkeyDevice.UP', '')
I thought these three steps accomplish a MENU+A operation. But it did not work every time. I executed this code for 20 times(in a loop) and found it only took effect for about 5-8 times.
Besides, I found these three steps will move the cursor to the first place most of the time.
Did anyone know why is this operation not reliable? Or any other suggestions to select all text?
Appreciate for any suggestions!
AndroidViewClient's EditText has a method for that:
def setText(self, text):
"""
This function makes sure that any previously entered text is deleted before
setting the value of the field.
"""
if self.text() == text:
return
self.touch()
guardrail = 0
maxSize = len(self.text()) + 1
while maxSize > guardrail:
guardrail += 1
self.device.press('KEYCODE_DEL', adbclient.DOWN_AND_UP)
self.device.press('KEYCODE_FORWARD_DEL', adbclient.DOWN_AND_UP)
self.type(text, alreadyTouched=True)
Using AndroidViewClient its pretty easy. Try this code-
editText= vc.findViewByIdOrRaise(EDITTEXT ID in quotes)
(x,y) = editText.getXY()
editText.device.drag((x,y), (x,y), 2000, 1)
vc.dump()
device.press('KEYCODE_DEL')

Android switch text button?

I am developping an app that has some quotes but I am unable to have next/previous buttons to navigate between the quotes like in the attached image ??
can anybody help ?
http://img.technospot.net/Free-Android-app-Success-Quotes.jpg
If the only that that's changing is the quote, just have the next and previous buttons change the text on click to the next quote in the cycle.
I'm assuming you are genarating randoms numbers to show the quotes randomly. Just save the position of the quotes(are they stored in an array?) and save them in an Integers List every time the user clicks next.
List<Integer> intList = new ArrayList<Integer>();
So if the user wants to go back just get the previous number which was shown by accessing the list with :
intList.get(intList.size()-1);
You should provide more informations next time and be as precise as you can.

How can Dynamic Add and Remove View In android ? And this is also Store even if I closed the Application

In my Application I want to Add and Remove View (like Button, or Textview, Checkbox ) by Coding (Programming ).
In Details:
I have One EditText and One Add Button. User Enter Anything in EditText and Press the Add Button then this one is added in bellow LinearLayout, and whether User click on his/her added Button it will going to next LinearLayout.
I get sucess upto this.
but when user click the button in second LinearLayout then it will come back on first Linearlayout. I am getting error Here, i don't know where I made a Mistake.
And I also facing Problem about how can I Store this all. Like User Add 5 Button and closed the application and whenever he/she came back to application I need whatever he/she previously added.
Here is what i done.
http://dynamicandroidview.blogspot.com/2011/12/how-to-add-view-in-android-by-coding.html
Try to create a database table with minimum 2 columns in your case it will be id and buttonText.
Now when user clicks on the add button it will save text to the database and will create the button dynamically below any buttons which are already created before or as a new button.
Now in your onCreate method get the count of text thats stored in database.Some thing like the following code:
DB getData = DB.getInstance();
getData.open(this);
ArrayList<TextHolder> getList = new ArrayList<TextHolder>();
getList = getData.getAllTextFromGeT();
getData.close();
x = genList.size();
Here x will be the number/count of elements that are already stored in the database.Now you can another int say i and using this i and x in the for loop you can create buttons dynamically.
Inside the loop you can do something like the following to get text for all the buttons that are being created:
TextHolder firstOne = getList.get(i);
String text = firstOne.getText();
You will also need class with getters and setters method in order to convert DB elements into objects.Like in the above code getText() is our getter method which is getting elements from database and returning it here.
here text will be the text of the button.
So every-time users starts the application he will see all the buttons that he created when he ran the application before and newly added button will appear on the spot and also will be stored in the database for future retrieval.
Remember we are just storing text of the button and assigning it unique id which helps us to create the buttons.Hope this helps

How to write Toast notification code for a simple math exercise?

I need to configure a button with a code that takes the first two digits of a number (when clicked) (from a textbox) and adds a number to it; and then takes the 3rd and 4th digits and does somewhat the same to it and displays the result value in a toast message. The user inputs the original number to which the additions and subtractions must be done.
So far I have only done: Toast.makeText(TimerCodeActivity.this, String.valueOf(
After that I am just lost
Try something like the following:
EditText theTextBox = (EditText)findViewById(R.id.idOfTheEditText);
int digits = Integer.parseInt(theTextBox.getText().toString().subString(0,2)); // Take the first two digits
int newnumber = digits + 4; // Add a number? Or are you trying to add like a string?
Toast.makeText(TimerCodeActivity.this, String.valueOf(newnumber), Toast.LENGTH_LONG).show();

append backspace in edittext

I have a simple activity. Only one edittext and one button.After writing some text in the edittext if I press the button I want to delete the last character of the text.
I have tried like this:
String backSpace = txtMsg.getText().toString();
if(!backSpace.equals(""));
String mystring=backSpace.substring(0, txtMsg.length()-1));
txtMsg.setText("");
txtMsg.append(mystring);
It works fine but I want to manually append the backspace character at last position and finally at any position by moving the cursor(by txtMsg.setSelection());
Like we append any character to the end of the text:
txtMsg.append("C");
I want to know what will be in place of C for appending the backspace?Please Help
Thanks in advance.
I am not sure if you still need an answer to this or not, but I recently had the same need as you and I used the delete methond on the Editable android type:
http://developer.android.com/reference/android/text/Editable.html#delete(int,%20int)
I had a password EditText and did the following in the click handler for the 'delete' button:
// delete
if (m_userPass.getText().length() > 0) {
m_userPass.getText().delete(m_userPass.getText().length() - 1,
m_userPass.getText().length());
}
The delete method takes two arguments, the start and end position of the text to delete. In my example, I am just making the delete button delete the last character (regardless of the cursor). If you wanted to get fancier, you could put in logic to figure out the start and end position based on the cursor position.
The start position has to come before the end position (I accidentally made that mistake at first). And the start position has to be greater than or equal to zero.
there is a simple way of doing what you want via:
KeyEvent event = new KeyEvent(0, 0, 0, KeyEvent.KEYCODE_DEL, 0, 0, 0, 0, KeyEvent.KEYCODE_ENDCALL);
input.dispatchKeyEvent(event);
You can try this, it worked for me:
if(!(text.length()==0))
text.setText(text.getText().delete(text.length() - 1, text.length()));

Categories

Resources