New to programming, new to Android. have figured out dynamic view creation,deletion and id assignment, but.....
i need to know how to have the backspace work normally unless there is no text in the current edittext, in which case the current view (edittext) would be deleted and the cursor and focus would jump to the edittext before it.
Any help would be very appreciated.
thanks,
Chris
You need to use an onKeyListener for your EditText field:
EditText et = new EditText();
et.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case 82:
case 84:
return false; // menu and search buttons
case 66:
return true; // enter button
case 4:
case 67:
return false; // back and delete buttons
default:
return true;
}
};
});
Hope you were looking for something along these lines.
To put focus to another edit text use:
EditText et = new EditText();
et.requestFocus();
Related
I have created android keyboard and I am creating arrows to move the cursor in the user input, I could do the left and right but I couldn't know how to write the code for the up and down .. here is the code
switch (arrow){
case KEY_LEFT:
CharSequence leftText = getCurrentInputConnection().getTextBeforeCursor(1000, 0);
int leftLen = leftText.length() ;
getCurrentInputConnection().setSelection(leftLen-1, leftLen-1);
break;
case KEY_RIGHT:
CharSequence rightText = getCurrentInputConnection().getTextBeforeCursor(1000, 0);
int rightLen = rightText.length() ;
getCurrentInputConnection().setSelection(rightLen+1, rightLen+1);
break;
case KEY_UP: case KEY_DOWN:
break;
}
can any one help me implementing the down and up ??
If you just want to move cursors
https://developer.android.com/reference/android/inputmethodservice/InputMethodService.html#sendDownUpKeyEvents(int)
Try use sendDownUpKeyEvents method
sendDownUpKeyEvents(KeyEvent.KEYCODE_DPAD_RIGHT);
Will simply do the work.
First of all, I recommend doing something like this to check for key presses
myEditText.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v,
int keyCode,
KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
moveCursorLeft();
return true;
case KeyEvent.KEYCODE_DPAD_RIGHT:
moveCursorRight();
return true;
case KeyEvent.KEYCODE_DPAD_UP:
moveCursorUp();
return true;
case KeyEvent.KEYCODE_DPAD_DOWN:
moveCursorDown();
return true;
}
}
return false;
}
});
I'm going off the assumption that you're programming a text editor of some sort.
The implementation for up and down depends on what you want those buttons to do, and how you've implemented things so far. For example, you could have each line as its own EditText that is kept track of in an ArrayList. Then, each time you add a new line, you can add a new EditText to the ArrayList. And when a user presses up or down, you can just change the index of the ArrayList by 1 in the correct direction.
I imagine that would be a pretty inefficient way of doing it, but it would be easy to define what up and down do. It's difficult to say more about what you should do without seeing more of your code.
I want to highlight text and then drag and drop it to a specific area. I know how basic drag and drop works and how to get the selected area to a String.
But I don't know how to to attach a setOnClickListener on a String? Anyone have any idea or is that a total wrong approach?
The string is in an editView
et =(EditText)findViewById(R.id.et);
int startSelection=et.getSelectionStart();
int endSelection=et.getSelectionEnd();
test = et.getText().toString().substring(startSelection, endSelection);
As the commenters pointed out, you can not set a a touch listener on a String, you have to do it on your EditText. You would do something like this:
et.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
et.setFocusable(true);
et.requestFocus();
// Do what you want
break;
case MotionEvent.ACTION_UP:
// Do what you want
break;
default:
break;
}
return true;
}
});
But still, I do not understand why do you want to use a OnTouchListener and not a OnDragListener, which seems to serve your purpose better...
I want to set my editText to always focus and when I inputted something and press key enter it will clear and regain focus again and can be inputted again.
you can capture the enter key event of your EditText, pull the text from it, and then set the text to "".
Here it is;
EditText et;
ArrayList<String> input = new ArrayList<String>(); // I am not sure how
// you want to save the strings, but this is a good way.
...
#Override
public void onCreate(Bundle sIS){
et = (EditText)findViewById(R.id.et); // fill in your id here
et.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView arg0,
int actionId, KeyEvent event) {
actionId = (actionId & EditorInfo.IME_MASK_ACTION);
switch (actionId){
case EditorInfo.IME_ACTION_DONE:
case EditorInfo.IME_ACTION_GO:
case EditorInfo.IME_ACTION_NEXT:
if (etAddLike.getText().length() > 0){
input.add(et.getText().toString());
etAddLike.setText("");
}
return true;
default:
return false;
}
}
});
...
don't forget to accept answer if it works.
call edittext.requestFocus(); when you want focus on it
I have an Edittext field and want the user to be able to enter the text very quickly. The text that will be entered begins with a capital letter followed by a set of numbers only. So after entering the first letter I want the keyboard view to switch to show numbers only. Here is my code for the edittext listener:
final EditText carPlate=(EditText) findViewById(R.id.carPlateNumber);
carPlate.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(carPlateCharnum==0){
carPlate.setInputType(InputType.TYPE_CLASS_NUMBER);
carPlateCharnum++;
return true;
}
// if keydown and "enter" is pressed
if ((event.getAction()==KeyEvent.ACTION_DOWN)
&& (keyCode==KeyEvent.KEYCODE_ENTER)) {
// display a floating message
DisplayToast(carPlate.getText().toString() + carPlateCharnum);
carPlateCharnum++;
return true;
}
return false;
}
});
The problem is the keyboard view is only switching after I press the enter key. So I need to know where and how to put this part of code:
if(carPlateCharnum==0){
carPlate.setInputType(InputType.TYPE_CLASS_NUMBER);
carPlateCharnum++;
return true;
}
Also I don't want the insertion point to return to the beginning of the EditText field after entering the first letter.
The keyboard has to reinitialize itself to be informed about the changes in the EditText carPlate.
(Detail-Info: TextView.onCreateInputConnection() returns the new InputType to the InputMethodManager).
carPlate.setInputType(InputType.TYPE_CLASS_NUMBER);
((InputMethodManager) YourActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE))
.restartInput(carPlate);
I am trying to achieve the following: my layout has a number of EditTexts. When the user touches one of the EditTexts for the first time, the keyboard should not open but the EditText should just gain focus. This is because I am doing a calculation in the EditText when it gains focus and I want to display the result to the user. Then when the user has seen the result he can touch the EditText again which will selectAll and open the keyboard to enter a new number.
This is the code I am using right now:
myEditText.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(iFocus == R.id.eTmyEditText) {
if(iCount == 3) {
myEditText.selectAll();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(myEditText, InputMethodManager.SHOW_IMPLICIT);
}
if(iCount > 3) return false;
iCount++;
}
else iCount=0;
myEditText.requestFocus();
return true;
}
});
I am using the iCount variable to distinguish the first touch from the second and the ones which will follow. iFocus is set in the focusChangeListener and holds the last EditText which had focus.
The code is working fine sometimes but not always. From time to time the keyboard is not opening or already on the first touch, sometimes the text is not selected, etc.
Is it possible that the TouchEvent bounces somehow? So that the TouchListener is executed more often than I would expect it to be? I also tried to separate the down event and the up event but this does not help.
Has anyone a good idea how what I am trying to do could be implemented in a better way? Thanks
EDIT: this is my final solution:
myEditText.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
myEditText.requestFocus();
if(iCount == 1){
myEditText.postDelayed(new Runnable() {
public void run() {
InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
manager.showSoftInput(myEditText, 0);
myEditText.selectAll();
}
}, 200);
}
iCount++;
break;
default:
break;
}
if(iCount >= 2) return false;
else return true;
}
});
Try this, its working for me when I touch the EditText two Times.
mEditText.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
iCount++;
if(iCount == 2){
mEditText.requestFocus();
InputMethodManager manager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
manager.showSoftInput(mEditText, 0);
iCount = 0;
}
break;
default:
break;
}
return true;
}
});
Try to replace
myEditText.setOnTouchListener(...);
with
myEditText.setOnClickListener(...);
Have a TextView that has the same background and size as the EditText, and switch between them on first tap, and switch them back again when the EditText loses focus.
Or store the current getKeyListener(), setKeyListener(null) and then restore after first click.
Or subclass EditText and override getDefaultEditable() to return false unless it already has focus.