Am using an array list that holds 5 images which change after an interval of 5seconds on the viewpager2 using handler and runnable,the images change perfectly well from the 1st image to the 5th image and restarts the process again using slide transition, the problem i have is changing the animation as it restarts running through the item images it looks terrible because it has to slide back to the first time as it transits more faster through the items backwards , i want to create it in a disappear(last item) and reappear (1st item) fast transition just like when popup ads change images and restart the slide transition again to the last item in a fragment, below is the piece of code.
viewpager3.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
#Override
public void onPageSelected(int position) {
super.onPageSelected(position);
sliderHandler.removeCallbacks(sliderRunnable);
sliderHandler.postDelayed(sliderRunnable, 5000);
}
});
sliderRunnable =new Runnable() {
#Override
public void run() {
if(viewpager3.getCurrentItem()<image2List.size()-1)
{ viewpager3.setCurrentItem(viewpager3.getCurrentItem()+1);
}else {
viewpager3.setPageTransformer();
viewpager3.setCurrentItem(0);
}
}
};
Not sure why you are using Viewpager2 for this and have to automate the changing of views yourself and you would really also need to implement an infinite View pager to get a more seamless transition (though at a small risk if running out of pages)
You could try setting viewpager3.setCurrentItem(0,false); to jump to the start without any animation.
But much better would be to use the more appropriate AdapterViewFlipper class.
This has automatic changing of the item based on a timer built in to it (no runnables needed).
It recyclers and is dynamic like viewpager2, the only downside is that page(view) is not as nicely encapsulated in a Fragment but as you are just showing images then that should not be a problem.
Example of how to use https://abhiandroid.com/ui/adapterviewflipper
You can add in and out animations to slide in and out to get a similar look to the Viewpager2 animations
If you look at another examples of the similar class for more static content it shows the slide in/out animations https://abhiandroid.com/ui/viewflipper
Related
I have an app where I animate some views on click.
However while the animation is going on it's still possible to initiate other on click events, which in the end cause multiple problems with the animations. I would need to make it so that no new animation starts while a certain one is going on.
Let's say the animation shuffles clickable views on screen. How do I make it so that the user can't initiate a new click command for x amount of time, where x could be the duration of the animation, or some other solution with similar results.
I'm just not even sure what I'm looking for when it comes to terminology. What areas should I look into to manipulate user inputs like this? Any good source on the topic would also be welcome.
You can always disable the click event with a flag and enable it again when the animation has finished. It isn't a perfect solution, but it does the trick quite well in most cases.
For example, if you are using an Animator for your animation, you can do something like this:
private boolean isClickEnabled = true;
...
#Override
public synchronized void onClick(View v) {
if (isClickEnabled) {
// We disable the click, which will be enabled again when the animation ends
isClickEnabled = false;
...
animator.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
// We enable the click again now that the animation has finished
isClickEnabled = true;
}
});
animator.start();
}
}
The same applies for when you use the Animation class, but with the AnimationListener instead.
Please note that, instead of saving a local boolean, you could just disable and enable the click on the view using view.setClickable(true/false) – it really depends on what you need and on your implementation, but the basic idea remains the same.
if you want to block the touch for some time try to Implement Runnable or runonuithread and than whatever view you want to block use mathod called view.setClickable(false).
So I have 3 different buttons on my layout. Now as I was creating my layout for phones etc. I decided that having 3 buttons on that page was a waste of space. Those buttons already have a ton of code logic behind them, to set their visibility, replace strings depending on situation etc.
My question is, is there a simple way to change them to a menu, is it possible to simply copy the layout XML I had for them on the main page, and paste it inside a menu?
That menu would open from a simple button, and then present all 3 buttons inside of the menu. So they aren't taking up space the entire time. Will this break the code I already have? Imagine I have the following code. Will it still
bLogin.setText(getResources().getString(R.string.Exit));
bLogin.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
And so on. I'm concerned with the menus behavior, will it close when I click one of the options, will the visibility attributes mess up the men, etc.
You could add a fourth button (let's call it menuButton) that is used to show/hide the other three buttons. You can then add the OnClickListener to the menuButton and set the visibility of the other three buttons from VISIBLE to GONE and the other way around.
So:
Button menuButton = (Button)findViewById(R.id.menuButton);
menuButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if (buttonsVisible) {
otherButton1.setVisibility(View.GONE);
...
} else {
...
}
}
});
You could use menu or action bar, which would simplify things for you (you wouldn't have to worry about showing/hiding your buttons, etc). Check the docs. Also, if you're on Android 3.0+, it is recommended to migrate to action bar:
On Android 3.0 and higher, items from the options menu are presented by the action bar as a combination of on-screen action items and overflow options. Beginning with Android 3.0, the Menu button is deprecated (some devices don't have one), so you should migrate toward using the action bar to provide access to actions and other options.
Of course, some work is required, but in the end, you will have a much more flexible solution (eg. if you will later want to add another action...)
I'm trying to make an interactive book, where every page contains 2 images and 2 buttons.
One image is animated on user touch (that's working fine)
The buttons are "Back" and "Next", that's working fine too, but my problem is I have a sequence of 10 classes name one after another:
Class01
Class02
Class03...
(Every class has a different animation)
So in class01, next button is always calling the next activity
public void Next(View v) {
Intent next = new Intent(this, next_page);
startActivity(next);
finish();
and closing the current one, which is Force Closing the app quite regularly.
I'm new to android and I think my logic is pretty useless.
How can I implement this sequence?
you can make it using View Flipper to make it more interactive and add animations.
In your case, finish(); isn't necessary, remove it. And you will have:
Next button
public void Next(View v) {
Intent next = new Intent(this, next_page);
startActivity(next);
}
Back button
public void Back(View v) {
this.finish();
}
Don't forget, users can press "Back Button" on their device. This may interest you:
public void onBackPressed() {
// do something if the button back is pressed.
super.onBackPressed();
}
I think #MohammedSaleem and #mvnpavan are right when they say you should to use ViewFlipper, more adapted in your case. You will declare in your Manifest just one Activity for all your layouts. See this answer (https://stackoverflow.com/a/3545954/2668136) which says:
ViewFlipper can be used if you want to periodically change the views.
Say like an automated flipping book of some sort.
To make a ViewFlipper, you must to read this tutorial:
Creating Image Slideshow Using ViewFlipper
And to see a simple example in this SO answer:
How To Use ViewFlipper With Three Layouts?
Finally, the Google Reference Documentation:
Public Class ViewFlipper
Hope this help.
I have a ListView wich use recycled views. I'm trying to prevent a view from recycle.
So I use setHasTransientState:
android.support.v4.view.ViewCompatJB.setHasTransientState(View view, boolean hasTransientState)
It works very well on Jellybean version but it doesn't do anything on Api < 16.
Is there a way to make it work or there is different approach for pre Jellybean ?
I found out how to set a RecyclerListener like #Daniel Chow suggested.
listView.setRecyclerListener(new RecyclerListener() {
#Override
public void onMovedToScrapHeap(View view) {
// Stop animation on this view
}
});
For pre Jellybean, I think you can just use setRecyclerListener on ListView and when RecyclerListener#onMovedToScrapHeap(View view) is called, clear the animation on the view who has been recycled and directly do the final job which was supposed to be done when animation ends.
The code inside onMovedToScrapHeap(View view) depends on how you implement the animation, e.g. you can call View#clearAnimation() if you previously used View#startAnimation to start animation.
Use android.support.v4.view.ViewCompat.setHasTransientState(View view, boolean hasTransientState) instead of android.support.v4.view.ViewCompatJB.setHasTransientState(View view, boolean hasTransientState)
Besides the animation problem Daniel talked about, another issue where knowing when your view is recycled has to do with memory management. If you are putting large, memory intensive bitmaps in your list items, it may be possible that you don't want your view recycled if its not likely to be re-used by other items. This hook gives you a chance to clear the bitmap you may have assigned to an ImageView. Hopefully, this is a rare problem.
I am having a bit of difficulty implementing the android-pulltorefresh widget by Johan Nilsson found at https://github.com/johannilsson/android-pulltorefresh
The problem I am having is after putting the custom listview into my application everything is fine it, but it asks to Tap to Refresh the list view but I need it to be set to pull down to refresh.
The code I am using below is pretty much from the github page and a screenshot of the app can be found below do demonstrate my issue:
PullToRefreshListView lv = (PullToRefreshListView)findViewById(R.id.listView);
lv.setOnRefreshListener(new OnRefreshListener() {
public void onRefresh() {
// Do work to refresh the list here.
GetData getData = new GetData();
getData.execute();
}
I need the Tap to refresh header gone and only to be shown once the listview has been dragged down. I get the feeling I just need to change some sort of flag but I can't find where this would be.
Unfortunately there is no way to work around this. The entire control is built around the idea that the "Pull to Refresh" header is a normal listview item that gets hidden by scrolling the list upward. Unfortunately, when you have a very short list, the list cannot be scrolled upward to hide the first item cause there are not enough items in the list -- so the fallback is to show the first item (the header) as well and have it display "Tap to Refresh".
EDIT: One kludge you may be able to do is insert dummy blank items so the list has enough items to hide the top header list item.
use this code
Hope it works for you.