simulate blinking of a button android - android

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

Related

How to change background of a button in android and retain it after few seconds

I referred this --> https://stackoverflow.com/a/31367723/12553303
I tried above solution but it is not working --> not displaying that drawable for few second
here is my code:
buynow.setOnClickListener(object : View.OnClickListener{
override fun onClick(v: View?) {
// set the color red first.
buynow.setBackgroundResource(R.drawable.mybuttonred)
// change to original after 5 secs.
Handler().postDelayed(Runnable { buynow.setBackgroundResource(R.drawable.mybutton)
Toast.makeText(applicationContext,"ksjdf",Toast.LENGTH_LONG).show()
},
5000)
}
})
Even the toast is not working on click
what I am missing?
This works fine with me you need to make view final so you can access it inside handler body
buyNow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
view.setBackgroundColor(Color.RED); //set the color to red
// Delay of 2 seconds (200 ms) before changing back the color to black
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
view.setBackgroundColor(Color.BLACK); //set the color to black
}
}, 200);
}
});

How can I change automatically the selected item in a ListView?

I've a ListView with some items that users can select.
I want that the first element appears selected, and after 1 or 2 seconds, the selected item will be the second automatically.
How can I do this?
When a item is selected, it can has a bold text for example.
I have a custom adapter for the ListView.
Update:
listview.setSelection(1);
System.out.println(listview.getSelectedItemPosition());
I've tested the code with this println, but it returns "-1". Not selects any row.
For a great tutorial on ListView see this tutorial: http://www.vogella.com/tutorials/AndroidListView/article.html
Back to the original question to select an item use the following:
ListView.setSelection(int);
Calling will simply change the UI without direct user input.
For the delay, you can use the following snippet:
final Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(new Runnable() {
#Override
public void run() {
ListView.setSelection(int);
}
}, 100);
For information on how change the Android ListView background color of selected item see this post.
If you are asking to iterate through the list and change it's selector you can use a handler like this:
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
#Override public void run() {
int currentSelectedIndex = mListView.getSelectedItemPosition();
if ((currentSelectedIndex + 1) < listCount) {
mListView.setSelection(currentSelectedIndex + 1);
} else {
mListView.setSelection(0);
}
handler.postDelayed(runnable, 1000);
}
};
And then in your code, in onCreate() for example call:
handler.postDelayed(runnable, 1000);
This will change the list selection every second as you would like.
If you do want to stop the automatic list selector, call this:
handler.removeCallbacks(runnable);
It's considered bad UX to automatically change the UI without direct user input, but if you want to select an item in a ListView programatically, you can call
ListView.setSelection(int);
Sets the currently selected item. If in touch mode, the item will not
be selected but it will still be positioned appropriately. If the
specified selection position is less than 0, then the item at position
0 will be selected.
For the delay, you will want to place this inside a Handler.
public class ListLooper {
private LoopHandler mHandler;
private ListView mListView;
public ListLooper(Activity activity) {
mListView = new ListView(activity);
mHandler = new LoopHandler(mListView);
}
private void start() {
mHandler.sendEmptyMessageDelayed(LoopHandler.MSG_LOOP, LoopHandler.DELAY);
}
private void stop() {
mHandler.removeMessages(LoopHandler.MSG_LOOP);
}
private static class LoopHandler extends Handler {
private static final int MSG_LOOP = 1;
private static final int DELAY = 2000;
/**
* Use a WeakReference so we don't keep an implicit reference to the Activity
*/
private WeakReference<ListView> mListRef;
private int mPosition;
private LoopHandler(ListView list) {
mListRef = new WeakReference<>(list);
}
#Override public void handleMessage(Message msg) {
super.handleMessage(msg);
// Check if we still have a reference to the ListView, and the Activity/Fragment hasn't been destroyed
if (mListRef.get() == null) {
return;
}
// If we're looping, run this code
if (msg.what == MSG_LOOP) {
int count = mListRef.get().getAdapter().getCount();
mListRef.get().setSelection(mPosition);
// If the position is less than the count, increment it, otherwise set it to 0
if (mPosition < count - 1) {
mPosition++;
} else {
mPosition = 0;
}
// Send the same message again, so we repeat this process
sendEmptyMessageDelayed(MSG_LOOP, DELAY);
}
}
}
Thanks so much!
This instruction:
listview.setSelection(int)
does not work in my code.
When I select some item, the background colour turns blue. That's correct.
But when the activity is just loaded, there is not item selected automatically.
I use performItemClick and the item is selected, highlighted and the getSelectedItem method will return the correct value:
ListView1.performItemClick(ListView1.getChildAt(1),1,ListView1.getItemIdAtPosition(1));
where 1 = position in the list

display background color for particular time in textview android

In my application I am using gcm to update data. first the data are saved in database and then displays in textviews.What I need is, when data is updated, I have to change the background color of textview just for 2 seconds.Please help me.
U can use a Handler :
final View v = findViewById(R.id.yourView);
// Change the color
v.setBackgroundColor(color1);
Handler h = new Handler ;
h.postAtTime(new Runnable(){
#Override
public void run() {
// Change color after 2 seconds
v.setBackgroundColor(color2);
}
}, 2000);
First create a Runnable class
private Runnable revertTextViewColor = new Runnable() {
public void run() {
textView.setBackgroundColor(Color.WHITE); //Put your original color here
}
};
Then when database is updated and textview needs to be highlighted
Change the color of textview
textView.setBackgroundColor(Color.BLUE); //Put any color here
Handler customHandler = new Handler(); //Create a handler
customHandler.postDelayed(revertTextViewColor , 2000); //Schedule the color to revert to original color in 2 secs i.e. 2000ms

Changing the picture of the button continuously for a specific time

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

Android:- change the text on buttons sequentially using animation

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.

Categories

Resources