Please Could You give me a bit of help, I have two activities the main activity (A) and the second activity (B) I made the layout background of activity(B) transparently, so now I can see the components of Activity(A) through Activity(B) until now everything is great, but now I want to access to activity(A) components through Activity(B) that have a transparent background, I used the "setOnTouchListener" to get the touch position but I have no idea how to set this position that I get to the Activity(A) and make it act as I touch it directly
my code
relativeLayout.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN){
float x = (int) motionEvent.getX();
float y = (int) motionEvent.getY();
MainActivity m = new MainActivity();
m.simulateTouch(motionEvent);
}
return true;
}
});
Thank you in advance
I don't think that is possible, because when you are in second activity the first activity is paused and even though you can see the elements of first activity you wont be able to access them.
I suggest you to find another way for doing this.
Related
Disclaimer: I have already tried following similar threads (namely How can I use OnClickListener in parent View and onTouchEvent in Child View? and How can i get both OnClick and OnTouch Listeners), yet my code still fails to work.
I have a (parent) activity which contains a (child) view. Activity has all the network code, while view contains a bitmap displaying game state. View executes the game moves and draws them, while activity sends and receives the moves.
As such, I need to listen for player's actions on the bitmap, handle them there and immediately after it is handled in the view, I need to send the made move to the server. So the best way I've found to handle all that so far is by using onClickListener along with onTouchListener. But I've spent 4 hours and can't get this to work, so any help would be appreciated.
My parent onClick:
gameView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
//gameView.performClick();
sendMove(theGame.getLastMove());
}
});
My child view:
#Override
public boolean onTouchEvent(MotionEvent event)
{
if(canIMove) {
float x = event.getX();
float y = event.getY();
int recWidth = this.getWidth() / 7;
int currentWidth = 0;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (x > currentWidth && x < currentWidth + recWidth)
{
//Bitmap touched
theGame.Move();
ReloadGameBoard();
}
return true;
}
}
return false;
}
It matters not whether I try to performClick() in onClick or in onTouch, it doesn't also matter whether I return true or false in onTouch, it doesn't matter whether I put it in ACTION_DOWN nor ACTION_UP, the result is always the same: child View onTouch gets executed and parent onClick does not.
And please, if you decide to downvote this, at least tell me why.
I was trying to make a TextView change when I swipe across the screen. I used GestureDector.OnGestureListener, and just recoded the onFling() methode as follows:
#Override
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent2, float v, float v2) {
if (motionEvent.getRawY() < motionEvent2.getRawY()) {
((TextView) findViewById(R.id.textView)).setText("Next");
} else if (moveTaskToBack(motionEvent.getRawY() > motionEvent2.getRawY())) {
((TextView) findViewById(R.id.textView)).setText("Previous");
}
return true;
}
But in my case, for a weird reason, when I flick left the app just stops (like I pressed the home button).
What could be causing this?
EDIT: if I reopen the app and press the return button (physical), it show "previous", therefore I could can conclude that the swipe works but something happening after setting the TextView is the culprit.
findViewById(R.id.textView)
This will throw an error if the textView R.id.textView is not on the current content view.
Sometimes, with overrides, you will need to do this:
return super.onFling(motionEvent, motionEvent2, v, v2);
Well, this is awkward. In the else if statement, for some reason, auto-complete probably added the function call:
moveTaskToBack(boolean b)
Which moves the stack to the back of the activity stack.
http://developer.android.com/reference/android/app/Activity.html#moveTaskToBack(boolean)
I'm working on an Android app and I want to add some eyecandy to the UI. I have an activity (let's call it MainActivity) which has some text fields, buttons and a gallery on it.
What I would like to achieve is: When the user touches somewhere on this activity, there should be some visual effect at the point where he touched (e.g. something like sparkles etc.).
So the essential parts of my question are:
a) How can I determine where the user touched
b) how can I draw my effects on to the screen (as an 'overlay' to the activity).
Thanks in advance for every helpful answer.
(I've checked out this answer but it doesn't seem to be applicable to my situation. )
First, you have to add onTouchListener to your root Layout, then you can get coordinates, where the user has touched the screen.
example :
float x,y;
rl = (RelativeLayout) findViewById(R.id.root_layout);
rl.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
float y = event.getY();
return super.onTouchEvent();
}
});
You should override the onTouchEvent method of your activity. You can then obtain the coordinates of the touch event:
#Override
public boolean onTouchEvent(MotionEvent event) {
float touchX = event.getX();
float touchY = event.getY();
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// touch down
break;
case MotionEvent.ACTION_MOVE:
// movement
break;
case MotionEvent.ACTION_UP:
// touch up
break;
default:
// default
}
return super.onTouchEvent(event);
}
To answer further on your question, to have a overlay, look at the APIDemos -> Graphics -> OpenGL ES -> Translucent GLSurfaceView. This will help you to create overlay on your activity. There may be some other example in API demos which will get you some help. Android's API Demos are good set of examples to address some known issues.
If you are working on ICS then I recommend you to look at the Developer Options in Settings application. If you are not working with ICS, I believe you can look at the APIDEMOS for touchevents. They draw lines on touches. The code in that, can get you started.
I have a view in a linearlayout. When the view is longpressed the view will be removed from this linearlayout and placed, on the same position of the screen, to a relative layout. On this way a can move the view over the screen with my finger.
it almost works:
i got the longpress event working (remove the view and place the view to the relativelayout). after that i add an ontoucheventlistener so my view stays with my finger, but only for a second. the last time the touchevent is fired i got "MotionEvent.ACTION_CANCEL". When i remove my finger and place my finger again to the view i can go feature with my movement, then it will keep until i remove my finger.
I think that my problem is that the view it removed for a short moment, at that time i get a "MotionEvent.ACTION_CANCEL", however, there are still some unhandled events, they will be fired first. Thats why i got for about 1 second still ontouchevents. (this is just a thought).
someone a idee how i can keep the ontouchevent, or let the ontouchevent fired without replacing my finger?
Edited
My thought is not correct. When i do a longpress the view stays with my finger, however i lost the view as soon as i move about 50 to 100 pixels to any direction.
Edited 2
the longpress code of the view inside the linearlayout
view.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
_linearLayout.removeView(v);
moveView(v);
return true;
}
});
moveView will be called by the longpress
private void moveView(View v) {
_relativeLayout.addView(v);
v.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_MOVE:
int x = (int) event.getRawX();
int y = (int) event.getRawY();
v.layout(x, y, x + v.getWidth(), y + v.getHeight());
break;
case MotionEvent.ACTION_UP:
_relativeLayout.removeView(v);
v = null;
break;
case MotionEvent.ACTION_CANCEL:
//it comes here when i move my finger more then 100 pixels
break;
}
return true;
}
});
}
of corse, this is the relevant part of the code and not the original code
Have a look at the Experience - Android Drag and Drop List post. There is also a source code that does something very similar to what are you trying to get. Eric, the author of that post, uses a separate temporary ImageView to hold a dragged view image, taken with getDrawingCache(). He also hides the original list item by setVisibility(View.INVISIBLE).
In the DragNDrop project (link above) you can replace drag(0,y) calls by drag(x,y) in DragNDrop.java to see a dragging in all directions.
Just throwing some possibilities in because I can't see the code but MotionEvent.ACTION_CANCEL could be being called as the parent layout is switched and the view is getting redrawn.
You could try overriding the onTouchEvent and at the point the ACTION_CANCEL event is sent try get hold of an equivalent view in the new layout and operate on that but I still think the better idea is to redesign so it all takes place in the same layout.
I have and android app that has 4 tabs, the first 3 take input from the user and selecting the 4th tab performs some calculations and displays results.
This works fine with the tabs implemented so that I just switch views within the same activity as I can easily access all of the inputed data on the 4th tab.
What I would like to do is switch activities when the tabs are changed. My tab layouts were getting out of control and it is easier having them in separate fies, same with the code.
I would like to save the inputed data from each tab to singleton so I can access it from other activities but onTabChangedListener does not seem to be the way go as the tab has changed, new activity started and view gone already.
How can I perform an action like calling a method that saves user data from the current view when a tab is changed but BEFORE it does it.
what about using onPause() or onStop() methods of the inner activities? you could save your data there.
You could set an OnTouchListener to catch the event and retrieve the index like this:
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
float x = event.getX();
int width = v.getWidth();
int index = (int) (x / (width / tabsCount)); // The amount of tabs.
...
}
}