How to define up and down arrow in custom android keyboard - android

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.

Related

Android - change image of ImageButton when pressed and release

I would like to set image1 for the button, but when it is pressed - change it to image2. After releasing, the button should be again an image1. I tried to do it this way in onClick() method:
button.setImageResource(R.drawable.image1);
if(button.isPressed())
button.setImageResource(R.drawable.image2);
but after first pressing the image of button changed to the image2 and stayed like that.
Could You help me with that problem?
I think this is what you want:
MyCustomTouchListener myCustomTouchListener = new MyCustomTouchListener();
button.setOnTouchListener(myCustomTouchListener);
Now the MyCustomTouchListener :
class MyCustomTouchListener implement OnTouchListener {
public boolean onTouch(View v, MotionEvent event)
{
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
// touch down code
button.setImageResource(R.drawable.image1);
break;
case MotionEvent.ACTION_MOVE:
// touch move code
break;
case MotionEvent.ACTION_UP:
// touch up code
button.setImageResource(R.drawable.image1);
break;
}
return true;
}
}
You can do this easily with a state list drawable, and it requires no additional java code on your part (unless you are creating a StateListDrawable at runtime, but even that is a more suitable approach than implementing custom touch interaction).
Use the following:
int dispImg = 0;
button.setImageResource(R.drawable.image1);
if (button.isPress()) {
if (dispImg == 0) {
button.setImageResource(R.drawable.image2);
dispImg = 1;
}
else if (dispImg == 1) {
button.setImageResource(R.drawable.image1);
dispImg = 0;
}
}
Explanation: dispImg keeps track of the image you're showing. When it is 0, it means that the 1st Image is showing and so, we should switch to the 2nd.
Hope I Helped :D

How to set an OnTouchListener to a string

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...

Undo drag&drop operation

I use android's drag&drop functionallity to move some imageviews into another imageview. This allows to make a selection depending on what imageview I dropped into the destiny imageview.
But now I have to consider that the user could make a mistake when selecting, and need to have some kind of reset button, which would return these imageview's to their original place and cancel the drag&drop.
I've been reading the android doc and looking for this on google but I haven't found nothing, so I don't know if even a functionality like this exists.
I'll post my code for if someone could have and idea on how to get it, but it just is the standard code for this functionallity.
First a listener for the Imageviews that when long click it starts the drag operation:
private final class MyLongTouchListener implements OnLongClickListener {
#Override
public boolean onLongClick(View v) {
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(data, shadowBuilder, v, 0);
v.setVisibility(View.INVISIBLE);
return true;
}
}
And then the Listener for the drop operation in the destiny imageview:
class MyDragListener implements OnDragListener {
#Override
public boolean onDrag(View v, DragEvent event) {
int action = event.getAction();
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
//...
break;
case DragEvent.ACTION_DRAG_ENTERED:
//...
break;
case DragEvent.ACTION_DRAG_EXITED:
//...
break;
case DragEvent.ACTION_DROP:
//...
break;
case DragEvent.ACTION_DRAG_ENDED:
//...
break;
default:
break;
}
return true;
}
}
A still don't know if that functionality exists, but I've found a way to get a similar result.
When you want to do the reset, just do finish() and then launch an intent to restart the activity.
This would leave the things as at the begining, but maybe in some cases won't be useful. For me works.

Touching the screen while onTouch() is already being called

When I touch the screen and move my finger I do something (pullanimation1 and 2) and when I release the screen I do something else (fireanimation1 and 2). Sometimes, the user might touch the screen while pullAnimation or fireAnimation is running, I get errors as the animation then run several times. I would like to make sure the animations won't run more then once when the user touch again the screen.
NB: pullAnimation1 and 2, fireAnimation 1 and 2 are AnimationDrawable
Here is what I've done :
image2.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
boolean bool=false;
boolean bool2=true;
int action = arg1.getAction() & MotionEvent.ACTION_MASK;
switch (action) {
case MotionEvent.ACTION_DOWN:
if (bool2) {
startAnimation(pullAnimation1,pullAnimation2);
bool=true;
}
break;
case MotionEvent.ACTION_MOVE:
if (bool2==true){
Log.w("GAMEACTIVITY","move");
bool=true;
bool2=false;
}
break;
case MotionEvent.ACTION_UP:
startAnimation(fireAnimation1,fireAnimation2);
bool=false;
doPhotoTask();
bool2=false;
break;
}
return bool;
}
});
I think you should be able to use the hasStarted() and hasEnded() methods to determine if your animation is currently going. See the docs for more
Some if statement like this might work:
if((fireAnimation1.hasStarted() == false) || (fireAnimation1.hasEnded == true()){
startAnimation(fireAnimation1, fireAnimation2);
}
I imagine you may also need to use reset() after it is done playing in order or the methods to return proper values next time touch happens.
EDIT:
AnimationDrawable has an isRunning() method, which makes it even easier than View animations.
if(fireAnimation1.isRunning() == false){
startAnimation(fireAnimation1, fireAnimation2);
}

add functionality to normal backspace

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();

Categories

Resources