I have a ScrollView with a LinearLayout and then a bunch of TextViews inside that layout, the goal is to make a CustomListView that will open and close various TextViews along the LinearLayout However, I also want to make an autoscroll feature where when the user double taps the screen the screen will scroll by its self.
I already have the second part done, when ever the user touches the ScrollView I have extended ScrollView and implemented this:
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
Log.d("ScrollView Intercept", "ACTION_DOWN");
return true;
} else if (ev.getAction() == MotionEvent.ACTION_UP) {
Log.d("ScrollView Intercept", "ACTION_UP");
return true;
} else if (ev.getAction() == MotionEvent.ACTION_CANCEL) {
Log.d("ScrollView Intercept", "ACTION_CANCEL");
}
return super.onInterceptTouchEvent(ev);
}
Now the ScrollView has all the touch events.
However, I still want the events for the CustomListView, I have tried to implement a equestDisallowInterceptTouchEvent but it doesnt seem to be working:
#Override
public boolean onTouchEvent(MotionEvent event) {
getParent().getParent().requestDisallowInterceptTouchEvent(true);
Log.d("touch listner","Touch");
return true;
}
PS. I have tried to se ListView in the past, but it was a but confusing so I just extended the TextView class and wanted to use on click and manually make the children Visible and Gone
Related
I have a custom device with a custom button and I need to handle an hardware Button Events/Intents:
every time I press the button it generates a PTT Press Action and I need to open my custom application, is there a way to do this?
If by custom device, you mean custom AOSP. Then make sure, it's button bound to events, this should be done with low level device driver configuration. And it's complicated work. Next, make sure you receive your click in next events.
Try to either use dispatch key event on Activity work:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
if (event.getAction() == KeyEvent.ACTION_UP){
enter();
return true;
}}
return super.dispatchKeyEvent(event);
};
On on key event with Android View which is in focus.
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
/* This is a sample for handling the Enter button */
return true;
}
return false;
}
I'm banging my head with this problem which probably is simple but since I'm new to this topic I somehow haven't been able to figure it out yet.
I've successfully implemented dragging a view with onTouch method. I've also successfully implemented onLongClick and onClick methods. But both of these functionalities were implemented separately.
The problem, like the title says is when I want to join these functionalities. I want the onTouch method to be called when a user long clicks a view and I want a new activity to start when a user clicks a view.
Here is the pseudo code:
public class Website extends AppCompatActivity implements View.OnTouchListener{
TextView longpress;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_website);
longpress = (TextView) findViewById(R.id.longpress);
longpress.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view){
//I don't really know how to do this part
onTouch(View view, Motion Event event);
return true;
}
});
longpress.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//Code for new activity comes here (I know how to do this part)
}
});
}
public boolean onTouch(View view, MotionEvent event) {
switch(event.getAction(){
case MotionEvent.ACTION_DOWN:
//Save initial coordinates of view <-- view.getX(), view.getY()
break;
case MotionEvent.ACTION_MOVE:
//Calculate dX and dY and setX and Y of the view (move view)
break;
case MotionEvent.ACTION_UP:
//If view is certain distance away from initial points do something
break;
}
}
}
Like I said, onTouch works on itself if I don't try to call it from onLongClick method. If I try to call onTouch(View view, MotionEvent event) from onLongClick method the problem occurs because onLongClick only receives one out of two arguments onTouch method should receive (onLongClick only receives view argument but it should also receive event argument).
Maybe I'm trying to do this in a totally wrong way but I have been looking at some documentation e.g. https://developer.android.com/training/gestures/ but still won't get an idea what to do.
(I would like to have a similar functionality to notifications on android phones)
So I've come to a solution which might or might not be a good one but for now it serves my functionality. If someone has a better solution and thinks mine is bad in some way please say so.
Here is the code:
boolean flag;
public boolean onTouch(View view, MotionEvent event){
int action = event.getAction() & MotionEvent.ACTION_MASK;
if(action == MotionEvent.ACTION_DOWN){
//do something on a down press
flag = true;
return true;
}
if(action == MotionEvent.ACTION_UP && flag == true){
//do something if we move finger away from screen we
//didn't move the view first
return true;
}
if(action == MotionEvent.ACTION_UP && flag == false){
//do something if we move finger away from screen and we moved
//the view before we moved the finger away from screen
}
if(action == MotionEvent.ACTION_MOVE){
//do something when moving the view
flag = false;
}
I have searched a lot for a solution but nothing works.
Update: post at bottom
I have a ScrollView and a FrameLayout as child.
Then I set up some EditText programmatically with LayoutParams.
Furthermore, I activated the ime options, so it will go to the next Edittext automatically.
When it now goes to the next one, the layout doesn't scroll up and the keyboard hides the EditText.
Answers like android:windowSoftInputMode="adjustPan / adjustResize"
didn't work.
I also tried to scroll manually with
Spieler1.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
int current_pos = scroll2.getScrollY();
scroll2.smoothScrollTo(0,(int)(current_pos + height/4));
scroll2.post(new Runnable() {
#Override
public void run() {
Spieler2.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
});
return true;
}
return false;
}
});
Strange things happened: It wanted to scroll up, but then suddenly goes back to the original position covering the EditText.
I have no idea, why it doesn't scsroll up, but maybe you have.
Thanks for answering.
I suspect the smoothScrollTo() call causes the behavior you've seen. You might want to use the android:nextFocusDown attribute for your first EditText instead of setting the OnKeyListener.
Im having some issues with the Android onGestureListener, I have 3 Linear Layouts sitting next to each other horizontally in a Linear Layout, the Linear Layout position is set to the middle Layout onCreate, the middle layout also contains a listview inside of it, what I would like to happen is when I swipe left or right the layout moves, this seems to not pick up the gestures when I attempt to swipe left or right on the Linear Layout with the listview inside of it, but if I swipe right or left on the other views that dont have anything in them it picks up the gesture and the view animates accordingly, has anyone had this issue before or have an idea how to fix it? any help will go a long way thanks
#Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return gestureDetector.onTouchEvent(event);
}
SimpleOnGestureListener simpleOnGestureListener = new SimpleOnGestureListener() {
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
float sensitvity = 50;
if ((e1.getX() - e2.getX()) > sensitvity) {
slideLeft();
} else if ((e2.getX() - e1.getX()) > sensitvity) {
slideRight();
}
return true;
}
};
private void slideRight() {
if (swipeDirection > -1) {
if (swipeDirection == 0) {
layoutContainer.animate().translationX(theDistance - 0)
.setDuration(500);
} else {
//go to home
layoutContainer.animate().translationX(0).setDuration(500);
}
swipeDirection--;
}
}
private void slideLeft() {
if (swipeDirection < 1) {
if (swipeDirection == 0) {
layoutContainer.animate().translationX(0 - theDistance)
.setDuration(500);
} else {
layoutContainer.animate().translationX(0).setDuration(500);
}
swipeDirection++;
}
}
The ListView itself has a gesture listener built in already (for scrolling the list), and possibly it also has an onItemClickListener on the individual items in the list. It can be that this interferes with your swipe behavior of the whole layout.
The solution explained here by Pinhassi has worked best for me so far:
Android Swipe on List
probably you will need to expand on the onItemClickListener of your ListView and include the swipe detector mentioned above. Also, putting #Override in front of the line where you declare the onItemClick might be needed to override the listview listeners. That way, you will maintain clickable list items and you can perform swipes on them.
I am setting OnTouchListener to a Button, I can check if motion type is MotionEvent.ACTION_MOVE. On the other hand if it is TextView, I cannot get any moment of MotionEvent.ACTION_MOVE
For example take a look at the code below:
public boolean onTouch(View view, MotionEvent me) {
if (me.getAction() == MotionEvent.ACTION_DOWN) {
Log.e(TAG,"1");
}
if (me.getAction() == MotionEvent.ACTION_UP) {
Log.e(TAG,"2");
} else if (me.getAction() == MotionEvent.ACTION_MOVE) {
Log.e(TAG,"3");
}
return false;
}
if I bind this listener to a Button, I can see "3" in my logs, on the other hand, if I bind this to an ImageView or etc. I cannot see any "3". but "1" and "2" are acting normal for both situation.
So the question is (if I am not mistaken) which items are MotionEvent.ACTION_MOVE sensitive?
Try implementing OnClickListener also..like this..and put onClick empty.. I think it then detects ACTION_MOVE, along with ACTION_UP and ACTION_DOWN..
class MyActivity implements View.OnTouchListerner, View.OnClickListener{
.....
public void onClick(View v) {}
.....
}
Textview doen't hava any MotionEvent ActionMove, http://developer.android.com/reference/android/widget/TextView.html..
Thanks...