I am developing an application on Android.
I have an issue with a Fragment, the code can be found below.
The idea is to have an Image View display a list of Picture in an infinite loop. In order to realize this, I have created a new Thread, so as not to block the UI Thread. With a while (0 < 5) statement I create an infinite loop. Then I run an if...else statement to check on which Picture we are to determine the next picture to go to.
A Handler is used to take care of the 10 seconds delay between switching pictures. And finally another runnable takes care of the posting to the UI Thread.
This seems like a very complicated way of getting things done, anyone used the simpler code?
On top of that, somewhere in my code, there is an error. I cannot spot it, anyone?
Here is my code.
public class SecAct_Foto_Fragment extends Fragment {
int counter = 0;
View rootView;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.sec_act_photo_layout, container, false);
return rootView;
}
Thread myThread = new Thread(new Runnable() {
#Override
public void run() {
while (0 < 5) {
//so far it loops only once
//you start with run_rocks and but_left
final ImageView pic_view = (ImageView) rootView.findViewById(R.id.foto_groot);
final ImageView three_but = (ImageView) rootView.findViewById(R.id.knoppen);
//create a runnable for the picture view
pic_view.post(new Runnable() {
#Override
public void run() {
//every 10 seconds, switch picture and button fragment
if (counter == 0) {
final Handler handler0 = new Handler();
handler0.postDelayed(new Runnable() {
#Override
public void run() {
pic_view.post(new Runnable() {
#Override
public void run() {
pic_view.setImageResource(R.drawable.run_mount);
}
});
counter = 1;
}
}, 10000L);
} else if (counter == 1) {
final Handler handler1 = new Handler();
handler1.postDelayed(new Runnable() {
#Override
public void run() {
pic_view.post(new Runnable() {
#Override
public void run() {
pic_view.setImageResource(R.drawable.run_away);
}
});
counter = 2;
}
}, 10000L);
} else {
final Handler handler2 = new Handler();
handler2.postDelayed(new Runnable() {
#Override
public void run() {
pic_view.post(new Runnable() {
#Override
public void run() {
pic_view.setImageResource(R.drawable.run_rocks);
}
});
counter = 0;
}
}, 10000L);
}
}
});
myThread.start();
}
}
});
}
private class AsyncQueryRun extends AsyncTask {
#Override
protected Object doInBackground(Object[] objects) {
for (...){
////do what you want
runOnUiThread(new Runnable() {
#Override
public void run() {
///do what you want to be handled by UI thread
}});
SystemClock.sleep(60); ////wait as long as you want in mili sec.
}
}
#Override
protected void onPostExecute(Object o) {
}
}
You can use Handler in following way :
final ImageView pic_view = (ImageView) rootView.findViewById(R.id.foto_groot);
private int animationCounter = 1;
private Handler imageSwitcherHandler;
imageSwitcherHandler = new Handler(Looper.getMainLooper());
imageSwitcherHandler.post(new Runnable() {
#Override
public void run() {
switch (animationCounter++) {
case 1:
pic_view.setImageResource(R.drawable.run_mount);
break;
case 2:
pic_view.setImageResource(R.drawable.run_mount2);
break;
case 3:
pic_view.setImageResource(R.drawable.run_mount3);
break;
}
animationCounter %= 4;
if(animationCounter == 0 ) animationCounter = 1;
imageSwitcherHandler.postDelayed(this, 3000);
}
});
I have decided to try the solution of #NehaK and work with the ImageSwitcher View.
Added the following code in XML..
<ImageSwitcher
android:id="#+id/foto_groot_imageswitch"
android:layout_width="match_parent"
android:layout_height="220dp"
app:srcCompat="#drawable/run_rocks"
/>
Then used it in my Fragment..
public class SecAct_Foto_Fragment extends Fragment {
int counter = 0;
View rootView;
private ImageSwitcher pic_image_switch;
private Handler pic_image_switch_handler;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.sec_act_photo_layout, container, false);
/*Animation anim_in = AnimationUtils.loadAnimation(getActivity(), R.anim.enter_from_left);
pic_image_switch.setInAnimation(anim_in);*/
//pic_image_switch = new ImageSwitcher(getActivity());
pic_image_switch = (ImageSwitcher) rootView.findViewById(R.id.foto_groot_imageswitch);
pic_image_switch.setFactory(new ViewSwitcher.ViewFactory() {
#Override
public View makeView() {
ImageView imageView = new ImageView(getActivity());
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
return imageView;
}
});
pic_image_switch_handler = new Handler(Looper.getMainLooper());
pic_image_switch_handler.post(new Runnable() {
#Override
public void run() {
switch (counter) {
case 0:
pic_image_switch.setImageResource(R.drawable.run_mount);
break;
case 1:
pic_image_switch.setImageResource(R.drawable.run_away);
break;
case 2:
pic_image_switch.setImageResource(R.drawable.run_rocks);
break;
}
counter += 1;
if (counter == 3) {
counter = 0;
}
pic_image_switch.postDelayed(this, 1000);
}
});
return rootView;
}
}
Related
Here my transition and reverse transition happens only once not 10 times, Can u tell me where I'm wrong ??
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(int i=0;i<10;i++)
{
Resources res = getApplicationContext().getResources();
Drawable background[] = new Drawable[2];
background[0] = res.getDrawable(R.drawable.shapes);
background[1] = res.getDrawable(R.drawable.large_shape);
final TransitionDrawable transition = new TransitionDrawable(background);
ImageView imgview = findViewById(R.id.transitimage);
imgview.setImageDrawable(transition);
Runnable r1 = new Runnable() {
#Override
public void run() {
transition.startTransition(2000);
}
};
Runnable r2 = new Runnable() {
#Override
public void run() {
transition.reverseTransition(2000);
}
};
Handler h = new Handler();
h.postDelayed(r1, 0);
h.postDelayed(r2, 2000);
}
}
}
public void repeat() {
new CountDownTimer(2000, 1000) {
public void onTick(long millisUntilFinished) {
if (i>9) { // i is a field,its initial value equals 0
cancel(); // stop timer
}
if (flag) {
transition.startTransition(2000);
} else {
transition.reverseTransition(2000);
}
flag = !flag;
i += 1;
}
public void onFinish() {
repeat();
}
}.start();
}
Call above function in some place.
I have a recycler view. On a button click I want to remove all the items from the recyclerview but the items must be removed with animation.
I am able to remove all the items at once but I don't know how to remove them with animation. Thanks
It's old, but wish this helps someone else as it's already not answered yet; I have done it by deleting a single item at a time by simulating a swipe animation on this item, and post a delay before deleting the next item, and so on to the way down to the last item of the RecyclerView
Step No.1:
In your activity that holds the clear all button and the RecyclerView instance: Create a method of single item delete
private void deleteItem(View rowView, final int position) {
Animation anim = AnimationUtils.loadAnimation(requireContext(),
android.R.anim.slide_out_right);
anim.setDuration(300);
rowView.startAnimation(anim);
new Handler().postDelayed(new Runnable() {
public void run() {
if (myDataSource.size() == 0) {
addEmptyView(); // adding empty view instead of the RecyclerView
return;
}
myDataSource.remove(position); //Remove the current content from the array
myRVAdapter.notifyDataSetChanged(); //Refresh list
}
}, anim.getDuration());
}
Step No.2:
Create the method that will delete all RecyclerView list items >> call it in your button click callback.
boolean mStopHandler = false;
private void deleteAllItems() {
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
#Override
public void run() {
if (myDataSource.size() == 0) {
mStopHandler = true;
}
if (!mStopHandler) {
View v = myRecyclerView.findViewHolderForAdapterPosition(0).itemView;
deleteItem(v, 0);
} else {
handler.removeCallbacksAndMessages(null);
}
handler.postDelayed(this, 250);
}
};
requireActivity().runOnUiThread(runnable);
}
Also it's important to handle configuration change in manifest, activity section, as if the configuration changes while clearing your recycler view list, an exception will be raised
<activity
android:name=".activities.MainActivity"
android:configChanges="orientation|screenSize|keyboard"
android:label="#string/app_name">
...
</activity>
This is a pretty good library and what's better is the documentation for it. You can even insert durations for transitions and animations.
Also, remember that if you are using default animation, after calling myDataSet.remove(pos) using adapter.notifyDataSetChanged() while there is an animation ongoing will cause the animation to stop.
Extend BaseItemAnimator class of recyclerview-animators library:
MyAdapter adapter = new MyAdapter(null);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setAdapter(adapter);
recyclerView.setItemAnimator(new MyScaleInLeftAnimator());
findViewById(R.id.button).setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View view) {
int count = adapter.getItemCount();
adapter.clear();
adapter.notifyItemRangeRemoved(0, count);
}
}
);
...
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder{
private ArrayList<String> mItems;
...
public void clear() {
if (mItems != null) {
mItems.clear();
}
}
}
...
public class MyScaleInLeftAnimator extends BaseItemAnimator {
private long lastRemoval;
private int removeCount;
public MyScaleInLeftAnimator() {
lastRemoval = 0;
removeCount = 0;
}
public MyScaleInLeftAnimator(Interpolator interpolator) {
mInterpolator = interpolator;
lastRemoval = 0;
removeCount = 0;
}
#Override protected void preAnimateRemoveImpl(RecyclerView.ViewHolder holder) {
ViewCompat.setPivotX(holder.itemView, 0);
}
#Override protected void animateRemoveImpl(final RecyclerView.ViewHolder holder) {
long time = System.currentTimeMillis();
long d = time - lastRemoval;
if (d < 100) {
removeCount++;
} else {
removeCount = 0;
}
lastRemoval = time;
ViewCompat.animate(holder.itemView)
.scaleX(0)
.scaleY(0)
.setDuration(getRemoveDuration())
.setInterpolator(mInterpolator)
.setListener(new DefaultRemoveVpaListener(holder))
.setStartDelay(removeCount * 100)
.start();
}
#Override protected void preAnimateAddImpl(RecyclerView.ViewHolder holder) {
ViewCompat.setPivotX(holder.itemView, 0);
ViewCompat.setScaleX(holder.itemView, 0);
ViewCompat.setScaleY(holder.itemView, 0);
}
#Override protected void animateAddImpl(final RecyclerView.ViewHolder holder) {
ViewCompat.animate(holder.itemView)
.scaleX(1)
.scaleY(1)
.setDuration(getAddDuration())
.setInterpolator(mInterpolator)
.setListener(new DefaultAddVpaListener(holder))
.setStartDelay(getAddDelay(holder))
.start();
}
}
This is how I have done without using any libraries - by inserting delays in the loop to remove items & restore (if needed)
clearItemsView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final List<LineItem> lineItemsCopy = new ArrayList<>(lineItems);
new Thread(new Runnable() {
#Override
public void run() {
for (int i=0; i<lineItemsCopy.size(); i++) {
runOnUiThread(new Runnable() {
#Override
public void run() {
salesOrderItemListAdapter.removeItem(0);
}
});
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
Snackbar snackbar = Snackbar.make(coordinatorLayout, getString(R.string.items_cleared_message), Snackbar.LENGTH_LONG)
.setAction(getString(R.string.label_undo), new View.OnClickListener() {
#Override
public void onClick(View v) {
new Thread(new Runnable() {
#Override
public void run() {
for (int i=0; i<lineItemsCopy.size(); i++) {
final int finalI = i;
runOnUiThread(new Runnable() {
#Override
public void run() {
salesOrderItemListAdapter.restoreItem(lineItemsCopy.get(finalI), 0);
}
});
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
}).setActionTextColor(Color.YELLOW);
snackbar.show();
}
});
I would like to update the progressBar with Handler and for loop but without success.
Code:
public void increase_splash_bar (int from, int to)
{
Handler handler1 = new Handler(Looper.getMainLooper());
for (progress_k = from; progress_k<=to ;progress_k++)
{
handler1.postDelayed(new Runnable()
{
#Override
public void run()
{
FrontLayout.update_splash_progress_bar(progress_k, 100);
}
}, 2000);
}
}
Question:
The progress bar increase immediately to the end value instead of progressively.
Why?
Try this:
public void increase_splash_bar (int from, int to)
{
Handler handler1 = new Handler(Looper.getMainLooper());
for (progress_k = from; progress_k<=to ;progress_k++)
{
final int curr_progress_k = progress_k;
handler1.postDelayed(new Runnable()
{
#Override
public void run()
{
FrontLayout.update_splash_progress_bar(curr_progress_k, 100);
}
}, progress_k * 100); // adjust "100" value to adjust speed
}
}
Repeat a task with a time delay?
#inazaruk
private ProgressBar progressBar;
private Handler mHandler;
private int progressInt = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
progressBar = (ProgressBar) findViewById(R.id.pb);
progressBar.setProgress(0);
mHandler = new Handler();
runnable.run();
}
Runnable runnable = new Runnable() {
#Override
public void run() {
try {
updateProgress();
} catch (Exception ignored) {
} finally {
mHandler.postDelayed(runnable, progressInt);
}
}
};
private void updateProgress() {
progressInt += 1;
if (progressInt > 100) {
mHandler.removeCallbacks(runnable);
} else {
progressBar.setProgress(progressInt);
}
}
try this code:
Solution 1
public void increase_splash_bar (int from, int to)
{
Handler handler1 = new Handler();
class Task implements Runnable {
int start,end;
Task(int a,int b) { start = a; end = b;}
#Override
public void run() {
for (int i =start ; i <= end; i++) {
final int value = i;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler1.post(new Runnable() {
#Override
public void run() {
progressBar.setProgress(value);
}
});
}
}
}
Thread t = new Thread(new Task(from, to)); //call it
t.start();
}
Solution 2: More Simple
If thread is too much to ask for this problem..
you can use the following solution to use a single Handler to update progressbar:
code
public class HandlerDemo extends Activity
{
ProgressBar bar;
Handler handler = new Handler()
{
#Override
public void handleMessage(Message msg)
{
bar.incrementProgressBy(5);
}
};
boolean isRunning = false;
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
bar = (ProgressBar) findViewById(R.id.progress);
}
public void onStart()
{
super.onStart();
bar.setProgress(0);
Thread background = new Thread(new Runnable()
{
public void run()
{
try
{
for (int i = 0; i < 20 && isRunning; i++)
{
Thread.sleep(1000);
handler.sendMessage(handler.obtainMessage());
}
}
catch (Throwable t)
{
// just end the background thread
}
}
});
isRunning = true;
background.start();
}
public void onStop()
{
super.onStop();
isRunning = false;
}
}
Hope it helps..
Hello guys I'm trying to update the UI after a short delay and for this I'm using handler's postdelayed method. The following code initially sets my textview text to "Processing" and the code that's included in the the handler's runnable gets executed but doesn't update the UI? Please note that this is done in a Fragment
TextView progressText=(TextView)parent.findViewById(R.id.inProgressText);
progressText.setText("Processing");
getActivity().getMainLooper()).postDelayed(new Runnable() {
#Override
public void run() {
TextView progressText=(TextView)parent.findViewById(R.id.inProgressText);
progressText.setText("Request completed");
}
}, 3000);
Thanks for your help
Change
getActivity().getMainLooper()).postDelayed(new Runnable()
to:
final Handler handler = new Handler();
Then
handler.postDelayed(new Runnable() {
#Override
public void run() {
final TextView progressText=(TextView)findViewById(R.id.inProgressText);
progressText.setText("Request completed");
}
}, 3000);
Use this code
public class MainActivity extends Activity {
TextView progressText = null;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_main, container, false);
progressText = (TextView) rootView.findViewById(R.id.inProgressText);
progressText.setText("Processing");
progressText.postDelayed(new Runnable() {
#Override
public void run() {
progressText.setText("Request completed");
}
}, 3000);
return rootView;
}
}
Please try this way : I checked in Fragement and it is working perfectly.
View parent= inflater.inflate(R.layout.your_fragement, container, false);
TextView progressText=(TextView)parent.findViewById(R.id.inProgressText);
progressText.setText("Processing");
progressText.postDelayed(new Runnable() {
#Override
public void run() {
progressText.setText("Request completed");
}
}, 3000);
I would like to make an animation, I have 10 images of a ball and I want to do:
Image1 visible, all the other images hidden
Image2 visible all the other images hidden
...
Image10 visible all the other images hidden
Should I do that in one animation (how?) or should I create a java method showImage(String pathimage) which would show the image located at pathimage and hide the others? Could I put any idea of time delay with using
handler.postDelayed(new Runnable() {
public void run() { }
}
EDIT, here is my code
The problem is that run() is always called in what seems to be an infinite loop. But I don't see where is my error
GAMEACTIVITY
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
.....
ImageView image2 = (ImageView) findViewById(R.id.imageView3);
image2.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
myAsyncRunnable mar = new myAsyncRunnable(GameActivity.this);
mar.execute(null);
PhotoTask photoTask = new PhotoTask(camera,surfaceCamera,isPreview,holder,GameActivity.this);
photoTask.execute(null);
Log.w("GAMEACTIVITY","PHOTOTASK");
return false;
}
});
}
My Async Task
public class myAsyncRunnable extends AsyncTask<Void, Boolean, Void> {
GameActivity gameactivity;
#Override
protected Void doInBackground(Void... params) {
pull();
return null;
}
public myAsyncRunnable(GameActivity gameactivity) {
super();
this.gameactivity = gameactivity;
}
public void pull() {
final ImageView image3 = (ImageView) gameactivity.findViewById(R.id.imageView3);
final int drawables[] = new int[] {R.drawable.pull2,R.drawable.pull3,R.drawable.pull4,R.drawable.pull5,R.drawable.pull6,R.drawable.pull7};
Runnable runnable = new Runnable() {
#Override
public void run() {
for (int i=0;i<drawables.length;i++) {
image3.setImageResource(drawables[i]);
gameactivity.handler.postDelayed(this, 500);
Log.w("asyncrunnable","run"+i);
}
}
};
gameactivity.handler.post(runnable);
}
}
You should not create 10 ImageViews to do such animation.
Only one ImageView should suffice! Use a handler to update the Image using any flavor of setImage every x milliseconds or so.
Initialize the images in an array and the current image in an integer :
int drawables[] = new int[] {R.drawable.image1, R.drawable.image2, ...};
int mCurrent = 0;
Then initialize your updater:
Runnable runnable = new Runnable() {
#Override
public void run() {
if(mCurrent>=drawables.length)mCurrent=0;
imageView.setImageResource(drawables[mCurrent]);
mHandler.postDelayed(this, 3000); //every 3 seconds
}
};
Finally when you want to start, just post the runnable:
mHandler.post(runnable);
Your code is wrong:
Try this pull function (although i do not really approve the whole code style but no time to fix that)
public void pull() {
final ImageView image3 = (ImageView) gameactivity.findViewById(R.id.imageView3);
final int drawables[] = new int[] {R.drawable.pull2,R.drawable.pull3,R.drawable.pull4,R.drawable.pull5,R.drawable.pull6,R.drawable.pull7};
for (int i=0;i<drawables.length;i++) {
final int j = i;
Runnable runnable = new Runnable() {
#Override
public void run() {
image3.setImageResource(drawables[j]);
}
};
gameactivity.handler.postDelayed(this, 500 * i);
Log.w("asyncrunnable","run"+i);
}
}
}