In my application, I have recording button. I want when user clicks on it each one second i change the background in order to simulate blinking. I created a handler and set it to 1 second therefore each one second this handler runs. Here i change the background. this my code:
mUpdateUITimerTask = new Runnable() {
public void run() {
// Simulating blinking for capture button
if(bolToggle) {
bolToggle = false;
captureButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_record_blink));
} else {
bolToggle = true;
captureButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_record));
}
mHandler.postDelayed(mUpdateUITimerTask, 1000);
}
};
When I run the app i see the changes but its not clear. buttons are like this:
When i run the application, red image is showing ok but for white image, it shows red image with a little white halo around it.
I tried to put captureButton.setBackgroundColor(Color.TRANSPARENT); before setting background but result was same.
any suggestion would be appreciated. Thank you.
Found the answer you need: https://stackoverflow.com/a/4852468/1352556
Basically you want an alpha animation. I believe this will make the entire button flash however, do you only want the red dot flashing?
Related
I want to know how to show an image which is in my case a gif loading screen to show when opening the layout of web view to give it time to give time to load contents in web view.
Either i should use a timer for the image to load or anything else.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(net_fragment_1.this,loadinganimate.class);
startActivity(i);
finish();
}
},1000);
I have tried this code but it is crashing.
Note- i am keeping loadinganimate class in the same package.
But whenver i am keeeping in different package and calling it by full path like com.xyz.xyz.loadinganimate.class
it is just showing this class and not resuming to the original windows after a period of time...
remove handler and add these lines in to your code
if (webView.isShown()){
loadinganimate.setVisibility(View.INVISIBLE);
}
I am new to Android development. I am currently building an application that must flash different colors when a button is clicked. When the button is clicked I call a function. This function loops through a list of items and basically at the moment attempts to show blue and green after each other a couple of times. Problem is that it only shows the last color. And this only happens when the thread has exited the method. In the example below it is blue. I have noticed with some of the work I have done that screen changes reflects once the program have left the current method where the loop occurs and sometimes even longer after that. Below is the code :
//Method that sets the color
public void SetVisualLayoutColor(int Color)
{
linearLayout.setBackgroundColor(Color);
linearLayout.invalidate();
linearLayout.refreshDrawableState();
}
//Method that loops and calls above method to set color
public boolean ShowRepititive()
{
boolean successfull = false;
try
{
boolean isGreen = true;
//for (TimingItem timingItem : items) {
for (int i=0;i<=10;i++) {
if (isGreen) {
SetVisualLayoutColor(Color.BLUE);
isGreen = false;
} else
{
SetVisualLayoutColor(Color.BLUE);
isGreen = true;
}
}
successfull = true;
} catch (Exception e)
{
Log.e("Repeating Flash", "showFlashRepititive: ", e);
successfull = false;
}
return successfull;
}
Is there some way to get past this or to force the changes to occur timeously ?
Thank you in advance.
Unless you explicitly run your code on a different thread, the code you write in an Activity, Fragment, or View will run on the UI thread.
When you are performing work on the UI thread, the UI cannot update. The UI only updates after your code has finished executing. Thus your for loop blocks any UI updates until it completes, showing the last color you set.
A more appropriate pattern to use would be to use an animation to change the color
Here is an example of a very basic animation that does a cross-fade between two background colors:
ObjectAnimator.ofObject(view, "backgroundColor", new ArgbEvaluator(), 0xFF0000, 0x00FF00)
.start();
Obviously this won't achieve the flashing effect you are looking for, but you can define your own animations in a similar manner to achieve the desired effect.
As Tanis.7x said, you can't create the desired animation while your code is executed in the main thread. All potential solutions have to have a background Thread and a way for it to communicate with your UI thread. (see AsyncTasks,Threads,Handlers)
An alternative to ObjectAnimator that can work in your specific case is TransitionDrawable. It's not that powerful but if it suits your needs, it's preferable since it's API 1 compatible:
You need to store a transitioncolors.xml in your drawables folder with your transition definition:
<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/green"/>
<item android:drawable="#color/blue"/>
</transition>
and then call:
ImageView view = (ImageView) findViewById(R.id.transitionId);
view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TransitionDrawable transition = (TransitionDrawable) v.getBackground();
transition.startTransition(3000);
});
where R.id.transitionId is:
<ImageView
android:id="#+id/transitionId"
android:background="#drawable/transitioncolors"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
I did some more research after I read Tanis.7x and Sevle's post, which put me in the right direction. For some reason the Object animator did not worked when I ran it and the Transition does not give me the flexibility I wanted. So I came across the ValueAnimator, which worked perfectly for me. I created my own class that extended the ValueAnimator and within the class I control how long each animation must be and also other aspects like running indefinitely until the user interrupts it on the screen. Thanks a lot guys, I really appreciate the help.
I am trying to implement some hints when a user is hovering a button or another view, I see that android have support for onHoverListener but I don't understand how it really works. However I did try to find a solution how to make a floating editText on a button hover but I didn't find any ideas.
I am thinking that hover in android is the same think with long click because you can't hover with finger without clicking the view.
OnHoverListener is only implemented in 2 specific situations: a) when a Bluetooth or USB mouse is plugged into the device, or b) on Galaxy Note devices, when the S-Pen (stylus) is hovering over the object.
In specific setups/situations this can be a very neat feature, but unfortunately, most users will never even know it exists. For your situation, you may want to implement an OnLongClickListener for showing hints/tips as that is pretty standard in Android.
This example will show a TextView or ImageView hint for 5 seconds when a long-click is initiated:
btn.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
findViewById(R.id.hint).setVisibility(View.VISIBLE);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
findViewById(R.id.hint).setVisibility(View.GONE);
}
}, 5000);
return true;
}
});
Hope this helps,
I want to add up on Aaron's answer.
Some devices with sensitive screens - Samsung S5 comes to mind - don't even need an s-pen.
Just need to hold your finger 1/2 inch over the screen and it would trigger it.
button.performClick();
For software demonstration purposes, I want to show the user interface updating after each button performClick(). For example, if the Activity was a calculator I can currently simulate the pressing of buttons [1], [2] and [3] using
btn1.performClick();
btn2.performClick();
btn3.performClick();
However, these updates to the EditText too quickly with no visible pause, i.e. it appears "123" are written to the screen simultaneously. What I want is:-
btn1.performClick() updates UI so people can physically see only button press updated to the EditText before the next button does. Similarly with btn2.performClick() and then btn3.performClick().
You may want to use a library like Robotium, and use Solo.waitForText Method to do what you want.
The problem is that we can not determine in advance the time that it will take to display the text, as it depends on the content of your onClick method.
It's why Robotium may be useful for what you want.
You can either use
Thread.sleep(delay);
or use
handler.postDelayed(Runnable,delay);
Use handler for btn2.performClick() and btn3.performClick() like...
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
// do something after 100ms
btn2.performClick();
}
}, 100);
I want to create a Button that behaves like a switch.
It should change its color when the user clicks it, and keep the color.
So the button is white at first and when the user clicks it, the color changes to black. When the user clicks it again it switches back to white and so on.
I tried it with a simple if else construct but only managed to get the button to be white at first and when being clicked change to black, but it won´t change back to white when clicked again.
Here´s the code so far. I guess it´s a dumb simply mistake but can´t seem to get through with it. "changecolor" is a variable I declared myself.
// Select Button Safe or At-Risk
final Button button7 = (Button) findViewById(R.id.SafeBT);
button7.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// Perform action on clicks, change color
if (changecolor == 0) {
button7.setBackgroundColor(color.black);
changecolor = 1;
} else {
button7.setBackgroundColor(color.white);
changecolor = 0;
}
}
});
Tanks for advice and help in advance.
where you have declared your variable changecolor?? .
Second thingis that you can simply an UI Element which call it : ToggleButton , it is like a Switch Button ON/OFF . is that what you want ? see this link : http://developer.android.com/reference/android/widget/ToggleButton.html