Is it possible to change the picture of the button with a sequence of images from the drawable continuously for a specific time on the click of the button , I know little about frame animation , is it possible to apply frame animation for changing the pictures of the button ? If not is there any other way of doing it?
please consider frame animation:
AnimationDrawable animation = new AnimationDrawable();
animation.addFrame(getResources().getDrawable(R.drawable.image1), 1000);
animation.addFrame(getResources().getDrawable(R.drawable.image2), 1000);
animation.addFrame(getResources().getDrawable(R.drawable.image3), 1000);
animation.setOneShot(false);
Button btnAnimation = (Button) findViewById(R.id.myBtn);
btnAnimation.setBackgroundDrawable(animation);
//In OnCreate or button_click event you can fire animation
animation.start()
I stole the answer from the this.
If you want to change the background for every click You can call the following :
private int pics[]= {R.drawable.p1, R.drawable.p2, R.drawable.p3, R.drawable.p4, R.drawable.p5};
private Random rand = new Random();
public int set_rand_pic() {
int pos = rand.nextInt(pics.length-1);
mycard.setBackgroundResource(pics[pos]);
return pos;
}
Hope it helps.
you can use Countdown Timer.
new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
//Change the button Bachground here!
}
public void onFinish() {
mTextField.setText("done!");
//set the button background that you want to show on end
}
}.start();
thats all :) enjoy
Related
I need help with a Widget for Android App,
I want to animate a Refresh Button with two ImageViews. On Click the first Image should hide and the refresh button Image which is 180° degrees turned should appear. Than after small delay the 180° turned ImageView should hide again and the first ImageView should appear.
At The end I want to update the Widget.
I would be happy about better ideas to animate a refresh Button in an Android Widget. All StackoverFlow results are talking about the normal App...
While using server call we can make progress in AsynCall as far now we can
fix it like this
Img1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Hide the First Image
//Show the 2nd Img after 3 Sec
DelayMethod();
}
});
public void DelayMethod(){
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//Show the 1st Img after 3 Sec
}
},3000);
}
Maybe put both images(maybe add more images with different angles to make the animation smoother) in a view flipper and then set it to android:autoStart="true"
android:inAnimation="#android:anim/fade_in" android:outAnimation="#android:anim/fade_out" android:animateFirstView="true" Then set the flipping interval as you like using android:flipInterval="10000".
Then you can stop the flipping once the widget is ready.
refrence for flipping view: https://stackoverflow.com/a/9253770/15552614
Here is my solution for my own Problem:
I created an PendingIntent to listen to the button.
remoteviews.setOnClickPendingIntent(R.id.refresh_btn,
getPendingSelfIntent(context, BTN_REFRESH));
protected PendingIntent getPendingSelfIntent(Context context, String action) {
Intent intent = new Intent(context, getClass());
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
I use global toggleState and call a method:
private void refresh(RemoteViews remoteviews, Context context) {
if (toggleState) {//if Button gets clicked onReceive will update all widgets
remoteviews.setViewVisibility(R.id.refresh_btn, View.GONE);
remoteviews.setViewVisibility(R.id.refresh_btn_rev, View.VISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
toggleState = false;
updateAllWidgets(context);
}
}, 500);
} else {
remoteviews.setViewVisibility(R.id.refresh_btn, View.VISIBLE);
remoteviews.setViewVisibility(R.id.refresh_btn_rev, View.GONE);
}
}
toggleState is set to true if Button gets clicked.
i want to make my button switch it's background color thrice a second between two colors -
btn_tp_dark and btn_tp_light
Timer timer = new Timer();
timer.schedule(new TimerTask() {
private View Button;
public void run() {
Button = (View) findViewById(R.id.filmTransparent11);
Button.setBackgroundResource(R.drawable.btn_tp_dark);
}
}, 300);
which is supposed to simulate a blinking effect but i dont know how to achieve this.
<Button
android:id="#+id/filmTransparent11"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="11"
android:background="#00000000"
android:onClick="next1" />
Don't bother using a Timer in android. Instead use a Handler.
You can create a looping task like this:
final Button button = (View) findViewById(R.id.filmTransparent11);
final Handler handler = new Handler();
final Runnable changeBackground = new Runnable() {
private int i;
#Override
public void run() {
// Set background based on task execution counter
if (++i % 2 == 0) {
button.setBackgroundResource(R.drawable.btn_tp_light);
} else {
button.setBackgroundResource(R.drawable.btn_tp_dark);
}
// Repeat task
handler.postDelayed(this, 300);
}
};
// Initiate the task
handler.postDelayed(changeBackground, 300);
One way to achieve it is using the ValueAnimator, with an ArgInterpolator. The duration of the Animation is 300ms, and you probably want to use ValueAnimator.INFINITE and ValueAnimator.REVERSE as repeat mode
I have expandable ListView with cds as a groups and tracks as a children. Each child has TextView, ImageButton(playButton) and SeekBar.
Now when I clicked one of the playButtons I want to get access to related seekBar.
My problem is that I have global SeekBar and I can't get the right SeekBar object to set my Runnable object correctly. So when I have for example 3 children, I click the first play button, but the third SeekBar is starting.
I have no idea how to resolve it. Any suggestions? This code is in getChildView() method.
SeekBar sb_songProgress;
iv_trackRowIcon.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
int song = l_cds.get(groupPosition).getTracks().get(childPosition).getTrackID();
if(ArtistCardActivity.mediaPlayer.isPlaying())
ArtistCardActivity.mediaPlayer.stop();
ArtistCardActivity.mediaPlayer.newTrack(song);
int finalTime = ArtistCardActivity.mediaPlayer.getDuration();
sb_songProgress.setMax((int) finalTime);
sb_songProgress.setClickable(false);
updateProgress(sb_songProgress);
}
});
public void updateProgress(SeekBar sb)
{
int timeElapsed = ArtistCardActivity.mediaPlayer.getCurrentPosition();
sb_songProgress.setProgress((int) timeElapsed);
durationHandler.postDelayed(updateSeekBarTime, 100);
}
Runnable updateSeekBarTime = new Runnable()
{
public void run()
{
int timeElapsed = ArtistCardActivity.mediaPlayer.getCurrentPosition();
//set seekbar progress
sb_songProgress.setProgress((int) timeElapsed);
durationHandler.postDelayed(this, 100);
}
};
You have to use the ViewHolder pattern (see here, section "Overriding BaseAdapter"), then you will be able to handle seekbar progress with ViewHolder this way:
myHolder.sb_songProgress.setProgress((int) timeElapsed);
Hope it'll work
This is a code for blinking textview on a button click..
start.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
recordShow.setVisibility(View.VISIBLE);
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(1000); //You can manage the time of the blink with this parameter
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
recordShow.startAnimation(anim);
}
i have to stop blinking on another button click...what to do..??
Another approach could be:
1. Declare the Animation and TextView objects globally (outside any methods) in your Activity.
private Animation mAnim;
private TextView mRecordShow;
2. Setup a class that sets your animation properties and starts it. Let this class expect a TextView widget as its parameter.
protected void setBlinkingText(TextView textView) {
mAnim = new AlphaAnimation(0.0f, 1.0f);
mAnim.setDuration(1000); // Time of the blink
mAnim.setStartOffset(20);
mAnim.setRepeatMode(Animation.REVERSE);
mAnim.setRepeatCount(Animation.INFINITE);
textView.startAnimation(mAnim);
}
3. Setup another class that stops your animation on a given text view. Let this class expect a TextView widget as its parameter as well.
protected void removeBlinkingText(TextView textView) {
textView.clearAnimation();
}
4. Now you can use your classes wherever desired, passing it the appropriate text views.
e.g.
(a) In your onClick() method where you want to start the animation, replace all your animation code with:
setBlinkingText(mRecordShow);
(b) wherever you want to stop the animation on that text view, just call:
removeBlinkingText(mRecordShow);
The following assumes you want to stop the blink by clicking the same button. If you want to stop the click using a different button, you can split the if-else in the onClick() below into separate click handlers.
First, move anim outside onClick() and make it a field of the containing class. You need anim to be stored somewhere so you can cancel it later.
Animation anim = new AlphaAnimation(0.0f, 1.0f)
anim.setDuration(1000); //You can manage the time of the blink with this parameter
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
Second, create a boolean field in the containing class to keep track of whether the TextView is currently blinking:
boolean mBlinking = false;
Then:
#Override
public void onClick(View v)
{
recordShow.setVisibility(View.VISIBLE);
if(!mBlinking){
recordShow.startAnimation(anim);
mBlinking = true;
} else{
recordShow.clearAnimation(anim); // cancel blink animation
recordShow.setAlpha(1.0f); // restore original alpha
mBlinking = false;
}
}
I have a sequence of buttons in android and I need to change the text of those buttons sequentially (slowly one by one) How can I achieve this? I cant apply any animation here?
Its really hard to tell exactly
what you want to do here but one option could be to use a TimerTask You could repeat changing the text of the buttons at whatever intervals you want. You could use a for loop to iterate over the buttons
Handler myHandler = new Handler();
// your buttons something like this
int[] ButtonArray = {R.id.button1,R.id.button2.....};
String[] stringArray = {"Hi","Hello","oi"....};
// get all the button
private Button[] myButtons=new Button[buttonArray.length];
for(int i = 0; i < buttonArray.length ; i++){
myButtons[i] = (Button) findViewById(mAlphabetsId[i]);
}
//Handler to do repetitive task
.................
Start The repetitive task
counterValue = 0;
Size =buttonArray.length; //Number of buttons
myHandler.postDelayed(mMyRunnable, speed);
.............................
private Runnable mMyRunnable = new Runnable()
{
public void run()
{
if(counterValue<Size){
myButtons[counterValue].setText(stringArray[CounterValue]);
myHandler.postDelayed(mMyRunnable, 1000); //Call again with 1 sec delay
counterValue++;
}else{
myHandler.removeCallbacks(mMyRunnable);
counterValue=0;
}
}
};
This code may contain errors since I did it in a hurry. Try it first.
Let me know if you get stuck.