I one activity I am seting different layouts depends on user choice.
For example I have setContentView(R.layout.main),
after that when user choose something I am setting new like setContentView(R.layout.first) when next time clicks I am setting setContentView(R.layout.second).
I need to change content in same activity. How to set animation ( something like when I really changing between activities ) when I changes content from main to first and from first to second ?
Let us take this with example.
Suppose we are changing view on press of button,
private OnTouchListener touch = new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
loadOutViewAnimation();//
loadInViewAnimation();
}
}
}
public void loadOutViewAnimation(){
//considering layout is your root layout
layout.setAnimation(animation);
}
public void loadInViewAnimation(){
setContentView(R.layout.first);
//by using findview by id here you will get root layout.
layout.setAnimation(animation);
}
You can do this by using ViewFlipper.....
See this example http://www.androidpeople.com/android-viewflipper-example
I hope this will help you to solve your problem.
You can used methods from AnimationUtils class makeInAnimation(context, boolean) and makeOutAnimation(context, boolean) to create Animation object. Config it with setStartTime and setDuration methods. Now you can call setAnimation on your view and it will be appearing or/and disappearing with your animation.
Related
I've been learning android for almost a month, and I want to make a simple game with a custom class that extends from View, and it's included on main_activity.xml. In Main Activity.class I create an instance of the View, and control the movement of the sprite on the GameView with buttons, so each button has a method that control sprite's movement like this:
public void move_up(View v){
gameview.sprite.move(Dir.UP);}
The problem is that it only works when the button is released, and it's executed one time. I want the method to be executed while the botton is pressed but I can't figure out how to do this.
I'm assuming by the name of your method that you're adding the onClick property to the xml layout file in order to handle the event. While this may seem convenient at first it will not give you what you need.
Instead, you should implement the OnTouchListener which gives you information about what touch event is currently happening, in your case you'd want to handle the ACTION_DOWN action:
findViewById(R.id.btn_up).setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
gameview.sprite.move(Dir.UP); //Or whatever
return true; //In case you wan to handle ACTION_UP
}
return false;
}
});
Although you could make your Activity implement the listener and handle multiple buttons there. This is achieved simply by telling your activity to implement OnTouchListener, imeplementing the method onTouch as in the code above but instead as a method of your activity.
And the resulting set, would be simplified to findViewById(R.id.btn_up).setOnTouchListener(this); where R.id.btn_up is the id you've defined in the xml file.
This would make it start moving when they press, instead of when they release, if what you want is to make it move until they release (which would make sense), do something like:
if(event.getAction() == MotionEvent.ACTION_DOWN){
gameview.sprite.move(Dir.UP); //Or whatever
return true; //In case you wan to handle ACTION_UP
}else
if(event.getAction() == MotionEvent.ACTION_UP){
gameview.sprite.stop(); //Or whatever you call the stop method
return false;
}
I have a problem I am struggling with a while now.
I have a Layout with a Button and a container in it.
<FrameLayout ... >
<Button
android:id="#+id/button"
...
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/overlayContainer"/>
</FrameLayout>
My goal is that as I long-press the button, I attach a custom view MyCustomViewto the container and keep the finger pressed.
All the following (ACTION_MOVE, ACTION_UP) events should then ideally be dispatched to and evaluated by MyCustomView.
MyCustomView works like a circular flyout menu: it overlays, dims the background, and shows some options. You then slide your pressed finger to the option, lift it up, and it triggers a result.
mButton.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// attach custom view to overlayContainer
// simplified code for demonstration
overlayContainer.addView(new MyCustomView());
return true;
}
});
Right now I don't see any option to "steal" the ACTION_DOWN-Event (which is required to start the event flow to a view) from the Button as I'm above it.
Nor does it work to manually generate and dispatch a ACTION_DOWN-Event in MyCustomView as I attach it.
While researching I found this post here, it basically is the same requirement, but for iOS (also does not provide an elegant solution, other that an click capturing overlay view) ): How to preserve touch event after new view is added by long press
Note that I want to avoid some kind of global overlay over the main view, I would like the solution to be as pluggable and portable as possible.
Thanks for any suggestions.
To answer my own question after the hint in the comments:
I solved it using a bare stripped version of TouchDelegate (had to extend it, since it unfortunetaly is no interface - setTouchDelegate only accepts TouchDelegate (sub)classes. Not 100% clean, but works great.
public class CustomTouchDelegate extends TouchDelegate {
private View mDelegateView;
public CustomTouchDelegate(View delegateView) {
super(new Rect(), delegateView);
mDelegateView = delegateView;
}
public boolean onTouchEvent(MotionEvent event) {
return mDelegateView.dispatchTouchEvent(event);
}
}
Then in my onLongClick method:
mButton.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// attach custom view to overlayContainer, simplified for demonstration
MyCustomView myMenuView = new MyCustomView()
mButton.setTouchDelegate(new CustomTouchDelegate(myMenuView));
// What's left out here is to mButton.setTouchDelegate = null,
// as soon as the temporary Overlay View is removed
overlayContainer.addView(myMenuView);
return true;
}
});
This way, all my ACTION_MOVE events from the Button are delegated to MyCustomView (and may or may not need some translation of the coordinates) - et voilĂ .
Thanks to pskink for the hint.
i am using listview in android with ratingbar as well as check box, here is my problem,if i am setting onitemclick listner to the listview, it is not working?
Please any one can help me..
Thanks in advance.
In your xml file.............
In checkbox..........
android:focusable="false"
Yes, I see the exact same thing. If you remove the checkbox and rating bar, then OnItemClick works, but with these widgets in your view, Android thinks the user wants to interact with them.
So instead you have to handle the user click in the view itself (rather than the listview).
OnTouchListener pressItemListener =new OnTouchListener()
{
#Override
public boolean onTouch(View arg0, MotionEvent arg1)
{
HomeActivity ha = (HomeActivity) getContext();
ha.handleLocationTouchEvent(position, arg1);
return false;
}
}
newView.setOnTouchListener(pressItemListener);
In the above example, HomeActivity is my parent activity. So I handle the user touch event in the custom view and then pass it off to the parent activity where you can do with it what you want. You might also want to handle onLongTouch. Hope this helps.
I have a two ImageViews.
1.ImageView zoomImageMoveable : should zoom in,out and moveable.
2.ImageView zoomImageFixed : show zoom this is guide line.
I want run different functions from one touch.
{
FrameLayout myFrame = new FrameLayout(this.getContext());
myFrame.addView(zoomImageMoveable);
myFrame.addView(zoomImageFixed);
}
Case 1. I tried to attach a listener to each of the view, as they are overlapping only one works.
Case 2. I also tried to wrap the two view with a FrameLayout and added an OnTouchListener to that layout to have it forward the calls to each of the assigned methods. Yet this method also didn't work.
myFrame.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
zoomImageMoveable.onTouchEvent(event);
zoomImageFixed.onTouchEvent(event);
return false;
}
});
How should I approach this issue?
Okey, so I've implemented a button on a Sliding drawer in a android application I'm building. The only problem is that when I press the button the whole sliding drawer is pressed and it slides up.
I know I can disable 'press to slide up' in the XML, but that does not seem to work as the sliding drawer still is pressed just without the slide up.
If I call the slidingDrawer.lock(); function the button actually works but then the sliding drawer can't slide up or even be pressed up.
Any one have a simple solution to this problem?
If I understand well you have added buttons on your SlidingDrawer handle and you want them to work like buttons when the user press them with keeping a standard SlidingDrawer behaviour when the handle is pressed/dragged?
I just solved a similar problem.
My Handle was looking something like that:
It's composed of two buttons and a center TextView which will be the real handle (reacting as a standard SlidingDrawer handle).
To make the buttons work independently of the SlidingDrawer I changed a bit of source code in the onInterceptTouchEvent method of the standard SlidingDrawer.java class (copy paste the source file from the android code source):
public boolean onInterceptTouchEvent(MotionEvent event) {
//...
final Rect frame = mFrame;
final View handle = mHandle;
// original behaviour
//mHandle.getDrawingRect(frame);
// New code
View trackHandle = mTrackHandle;
// set the rect frame to the mTrackHandle view borders instead of the hole handle view
// getParent() => The right and left are valid, but we need to get the parent top and bottom to have absolute values (in screen)
frame.set(trackHandle.getLeft(), ((ViewGroup) trackHandle.getParent()).getTop(), trackHandle.getRight(), ((ViewGroup) trackHandle.getParent()).getBottom());
if (!mTracking && !frame.contains((int) x, (int) y)) {
return false;
}
//...
}
I also added a setter for the mTrackHandle attribute to set, during the activity creation, the real hanlde to use:
protected void onCreate(Bundle savedInstanceState) {
//...
mSlidingDrawer.setTrackHandle((View) findViewById(R.id.menu_handle_TextView_Title));
//...
}
After that you can set standard listener on your two buttons. They will work like a charm.
in response to Joakim Engstrom:
Yes that's possible!
to do that you have to override onInterceptTouchEvent as follow.
#Override
public boolean onInterceptTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
rect = new Rect(handle.getLeft(), ((View) handle.getParent()).getTop(),
handle.getRight(), ((View) handle.getParent()).getBottom());
if (!rect.contains((int) event.getX(), (int) event.getY())) {
if (event.getAction() == MotionEvent.ACTION_UP)
this.lock();
else
this.unlock();
return false;
} else {
this.unlock();
return super.onInterceptTouchEvent(event);
}
}
you have also to add a setter to set handle to actual handle view during activity creation.
may be this code can help you
https://github.com/xPutnikx/SlidingDrawerWithButtons