How to Replace timer with turn based onClick in Android? - java

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.

Related

trying to input values to the selected editTexts

I recently started using android studio. So I come across a problem, which is like this: I have 2 buttons and 5 editTexts. Using onClickListener, I am trying to input values to the editTexts. But the problem is I can make only one editText to listen what button I am pressing. If I use different onClickListener for 5 editTexts, only the last one is responding to the buttons (which is obvious). I want to happen like this: Whatever etitText I select, when I press a button, the value will reflect on only that one. But it is not working. I was hoping something like a if statement, where if I select a specific editText the onClickListener will respond to that.
I created this following method.
private void selectBox(EditText e, Button b1, Button b2) {
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
Button b = (Button) v;
e.append(b.getText().toString());
}
};
b1.setOnClickListener(listener);
b2.setOnClickListener(listener);
}
then I want something like this in onCreate method:
if(editText1 is selecte`enter code here`d in the emulator) {
selectBox(editText1, button1, button2);
} else if(editText2 is selected in the emulator) {
selectBox(editText2, button1, button2);
} ......
How would I proceed? Thanks.
Here You Go !
fun handleText() {
var flag: Int
txtBox1.setOnClickListener {
flag = 1
if (flag == 1) {
button.setOnClickListener {
txtBox1.text = "add text to field 1"
}
}
}
txtBox2.setOnClickListener {
flag = 2
if (flag == 2) {
button.setOnClickListener {
txtBox2.text = "add text to field 2"
}
}
}
}
Post: I don't like Java as much as I like Kotlin. That's why your question is Java and my answer is Kotlin.

How can my function fork for few seconds in 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

Do I need to add many methods, or is it possible to call one method

I have a tic tac toe app, and I want to know whether it is possible to set all the tic tac toe buttons to one on_click event, and then create a variable to get the ID of the button clicked, then pass it as a parameter to another method which will do the actual functionality, OR do I need to create different on_click events for each button?
You can do something like this, and add as many "cases" as needed:
View.OnClickListener sharedClickHandler = new View.OnClickListener() {
public void onClick(View view) {
switch(view.getId()) {
case R.id.button1:
// handle first button
break;
case R.id.button2:
// handle second button
break;
}
}
}
You can just use one listener - the onClick method takes a View parameter, which is the view that was clicked on. You can then find out which of your buttons that was:
View.OnClickListener sharedClickHandler = new View.OnClickListener() {
public void onClick(View view) {
int id = view.getId();
// Do the right thing based on the ID
}
}
Exactly how you do what you need to do based on the ID is up to you. For simple examples you could just use a switch/case statement; in other cases if you're mapping from ID to something else (a mutable object representing game state for example) you could use a Map<Integer, GameObject> and just get the right one...
hello you can use the same click event for button, you can attach for example an integer as a tag to the button so that you can know which button was clicked and handle accordingly.
button1.setTag(1);
button2.setTag(2);
button3.setTag(3);
button1.setOnClickListener(buttonClick());
button2.setOnClickListener(buttonClick());
button3.setOnClickListener(buttonClick());
public View.OnClickListener buttonClick(){
View.OnClickListener click = new View.OnClickListener() {
#Override
public void onClick(View v) {
int numberClicked = v.getTag();
//You have now the button clicked
}
};
return click;
}

Android button comparing

I've got very very basic problem in Java (Android) writing. Here it is, I've got code:
public void WriteValue (View sender){
Button bt=(Button)sender;
}
WriteValue is performed, when user click button. And now I want to compare button which user clicks, with button that Id I know. Something like
if(UserButton==ClearButton) Display.setText("0");
Thanks for help
Tux:)
You could just compare ids.-
public void WriteValue (View sender) {
Button bt = (Button)sender;
if(bt.getId() == R.id.clearButtonId) {
Display.setText("0");
}
}
step 1 : make your Class implement OnClickListener
step 2 :
// global variable
Button btOne ,bTwo;
step 3 :
// in onCreate() method
btOne = (Button)findViewById(R.id.btOne);
btTwo = (Button)findViewById(R.id.btTwo);
btOne.setOnClickListener(this);
btTwo.setOnClickListener(this);
step 4 : implement method from interface
puvlic void onClick(View v)
{
if(v==btOne)
{
// do work for btone
}
if(v==btTwo)
{
// do work for btTwo
}
}
You don't need to cast the View to a Button here. You can simply get the id of the View clicked which is the param sent to the function (assuming this is an onClick() function as defined in xml or by setting the listener in Java). So it could be something like this
public void WriteValue (View sender)
{
int id = sender.getId(); // get the id of the View clicked
swith (id) // switch logic on that id
{
case (R.id clearButtonId): // if the clearButton was clicked do this
Display.setText("0");
break;
default:
Display.setText("Some other String);
break;
}
}
You could also use if/else instead of a switch but this is more readable, IMHO. This easily allows you to add more logic for other Buttons.
Also consider using Java naming conventions. According to this code, I assume Display is a TextView or EditText so it should start with a lower-case letter (ex. display).

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
}
...
}
});
...

Categories

Resources