How can my function fork for few seconds in java? - java

Im making apk in android studio,in java,and I need to my button change color for few seconds.For example,if the original color is blue,it needs to get changed to red for one second and get back to blue.

You can use Handler().postDelayed method on click of button to achieve your goal. Check the code below for reference:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
button.setBackgroundColor(ContextCompat.getColor(R.color.yourSecondaryColor));
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
button.setBackgroundColor(ContextCompat.getColor(R.color.yourBaseColor));
}
},1000);
}
});
You can change the time according to your needs. Just change 1000 to your required value.

Using threads:
After changing colors use:
Thread.sleep(1000);
and then change color again

Related

How to Replace timer with turn based onClick in Android?

I am trying to make a TicTacToe online multiplayer game in Android Studio using Firebase, below is the code I am using for the buttons (1 of the 9 blocks of TicTacToe). Right now I am using a timer to stop the user from clicking any other buttons after he once click the button to give the other user time to click. But I want to make it so that one user won't be able to click any button till the other user clicks a button. Is there a way to do so?
The default values of nothing & face & a1 is "zero" and default value of added is "one" whereas rface is just a string variable getting its value from "face".
//below clicking
a1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//below
//starting if condition
if (ra1.equals(nothing)){
if (rface.equals(nothing)){
a1.setBackgroundResource(R.drawable.rounded);
Firebase refChild = ref2.child("a1");
refChild.setValue("round");
refChild = ref2.child("face");
refChild.setValue("one");
}
else if (rface.equals(added)) {
a1.setBackgroundResource(R.drawable.crossed);
Firebase refChild = ref2.child("a1");
refChild.setValue("cross");
refChild = ref2.child("face");
refChild.setValue("zero");
}
//timer below
a1.setClickable(false);
a2.setClickable(false);
a3.setClickable(false);
b1.setClickable(false);
b2.setClickable(false);
b3.setClickable(false);
c1.setClickable(false);
c2.setClickable(false);
c3.setClickable(false);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
a1.setClickable(true);
a2.setClickable(true);
a3.setClickable(true);
b1.setClickable(true);
b2.setClickable(true);
b3.setClickable(true);
c1.setClickable(true);
c2.setClickable(true);
c3.setClickable(true);
}
}, 3000);
//timer above
}
}
});
I suggest having two different OnClickListeners rather than an if statement. One for Xs and the other for Os. Then you can set the listener back and forth. For example, at the end of the OnClickListener for X, it calls setOnClickListener() using the one for Os.
I finally came up with this algorithm for my solution:
online=0;
offline=1;
if (online!=offline){
offline=random;
online=offline;
if(shape=0)
{
click(set round);
}
else if(shape=1)
{
click(set cross);
}
}
where shaper is another integer which I am using as a switch.

How to make a textview appear and disappear?

The default is disappear. I want to know how to make a text-view appear and disappear.
You can set particular View's visibility Like :
yourTextViewName.setVisibility(View.VISIBLE) // For Visible/Appear
yourTextViewName.setVisibility(View.INVISIBLE) // For Invisible/Disappear
yourTextViewName.setVisibility(View.GONE) // For Gone / View not takes any space at Run time
Hope this will Helps.(:
Here is the method you are looking for;
private void makeTextViewDisappear(){
yourTV.setVisibility(View.VISIBLE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
yourTV.setVisibility(View.INVISIBLE);
// OR yourTV.setVisibility(View.GONE) to reclaim the space used by textview
}
}, 10000); //for 10 seconds
}
If this worked, don't forget to mark this as the right answer. Regards.
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
yourTextViewName.setVisibility(View.VISIBLE)
}
}, 5000); <--- change time here

Android Notification Area Customization

I don't know whether this question get minus points, but I searched every where and my last resort is stackoverflow.
I need to add five buttons to notification area in horizontally. And each button I need to add even listener. I know it is possible to do with RemoteViews. But I never seen anyone adding event listener to each element.
These are the references if anyone need to refer.
Notifications Documentation
How to create a custom notification on android
SlidingDrawer API
You can add 5 anonymous listeners, or a single named listener.
Anonymous:
Button b1 = new Button(...);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// first listener's code goes here
}
});
Button b2 = new Button(...);
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// second listener's code goes here
}
});
...
named is much the same, but contains a switch statement to differentiate what happens:
View.OnClickListener myListener = new View.OnClickListener() {
public void onClick(View v) {
String buttonTitle = ((Button)v).getText();
if ("title1".equals(buttonTitle)) {
// do things for the first button's click
} else if ("title2".equals(buttonTitle)) {
// do things for the second button's click
}
...
}
});
...

Call the listener several times

There is a listener code.
Method onCreate:
rssListView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
//Then it stops
rssListView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
}
);
I want to run it again, when the button clicked:
b1.setOnClickListener(
new View.OnClickListener() {
// #Override
public void onClick(View v) {
// ...
}
}
);
Tell me how I can activate the listener on the button?
I think you talking about Pull-to-Refresh functionality. If yes - look at this article.
What is it you want to run again.
If you want the function to be called on button click then do the function call in the onClick method of the listener. Once a listener is added to a UI Widget it keeps on listening to any UI Events on that object till the application is running.

Setting an Android Button visible after a certain period of time?

I have a button that I don't want to be clickable until a certain amount of time has run (say, 5 seconds?) I tried creating a thread like this
continueButtonThread = new Thread()
{
#Override
public void run()
{
try {
synchronized(this){
wait(5000);
}
}
catch(InterruptedException ex){
}
continueButton.setVisibility(0);
}
};
continueButtonThread.start();
But I can't modify the setVisibility property of the button within a different thread. This is the error from the LogCat:
10-02 14:35:05.908: ERROR/AndroidRuntime(14400): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
Any other way to get around this?
The problem is that you can touch views of your activity only in UI thread. you can do it by using runOnUiThread function. I would like to suggest you to use
handler.postDelayed(runnable, 5000)`
You must update your view from UI-thread. What you are doing is you are updating from non-ui-thread.
Use
contextrunOnUiThread(new Runnable(){
#Override
public void run() {
// TODO Auto-generated method stub
}});
or use handler and signalize hand.sendMessage(msg) when you think is right time to update the view visibility
Handler hand = new Handler()
{
#Override
public void handleMessage(Message msg) {
/// here change the visibility
super.handleMessage(msg);
}
};
You can use the postDelayed method from the View class (A Button is a child of View)
here is simple answer i found
Button button = (Button)findViewBYId(R.id.button);
button .setVisibility(View.INVISIBLE);
button .postDelayed(new Runnable() {
public void run() {
button .setVisibility(View.VISIBLE);
}
}, 7000);

Categories

Resources