How to Force execution: button.setImageResource(R.drawable.x_sign)? - java

My ticTacToe Assigment picture
In onClickListener, I have line button00.setImageResource(R.drawable.x_sign);
but it will be executed only when listener is finished.
How can I force this command to execute immediately?
Condition in onClickListener for imageButton is:
if ((opField[x][y] == 0) && covekNext){ // field is empty and man have turn
button00.setImageResource(R.drawable.x_sign);
} else return;
When In this situation I play field 21 ( second row, first column).
button00.setImageResource(R.drawable.x_sign);
Images wouldnt be swaped! blank field stay blank, not X.
Bug or my coding error.
Thanks.
final ImageButton button00 = findViewById(R.id.imageButton00);
button00.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
x=0;
y=0;
if ((opField[x][y] == 0) && covekNext){
button00.setImageResource(R.drawable.x_sign);
} else return;
covekNext = false;
opField[x][y]= 1;
if(++opWinLines[0]==3|++opWinLines[3]==3|++opWinLines[6]==3){
covekWin(); //button00.setImageResource(R.drawable.x_sign); //command not executed
return;
}
if (++zauzeto<9) {
computerMove(); //button00.setImageResource(R.drawable.x_sign); //command not executed
} else {
nereseno(); //button00.setImageResource(R.drawable.x_sign); //command not executed
}
}
}); // button00.setImageResource(R.drawable.x_sign); executed

I think you are looking for something like this :
final ImageButton button00 = findViewById(R.id.imageButton00);
button00.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
x=0;
y=0;
if ((opField[x][y] == 0) && covekNext){
button00.setImageResource(R.drawable.x_sign);
} else return;
covekNext = false;
opField[x][y]= 1;
if(++opWinLines[0]==3|++opWinLines[3]==3|++opWinLines[6]==3){
button00.setImageResource(R.drawable.x_sign);
covekWin();
return;
}
if (++zauzeto<9) {
button00.setImageResource(R.drawable.x_sign);
computerMove();
} else {
button00.setImageResource(R.drawable.x_sign);
nereseno();
}
}
});

its because u use returnin your code
clicklisteners suppose to listen to clicks while user works with activity's ui

It works in most of cases but not always
You are only swapped the imageResource in first if condition. So when first else part is executed, image is not being swapped. If you want this line to execute every time, place it as first line in onClickListener as follows.
final ImageButton button00 = findViewById(R.id.imageButton00);
button00.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
x=0;
y=0;
//Set the imageResource here
button00.setImageResource(R.drawable.x_sign);
if ((opField[x][y] == 0) && covekNext){
} else return;
covekNext = false;
opField[x][y]= 1;
if(++opWinLines[0]==3|++opWinLines[3]==3|++opWinLines[6]==3){
covekWin();
return;
}
if (++zauzeto<9) {
computerMove();
} else {
nereseno();
}
}
});
Cheers :)

Everybody, thanks a lot!!!!
Everything about this question was my mistake made within some false call in methods: covekWin(); computerWin(); nereseno().
So this part of code is correct, and this question can be erased!
Thanks again!

Related

Hide ImageButton for first and last

Currently I build learning app for children. This app learn alphabet from "A" to "Z". If click the next button the alphabet will switch from A to B, B to C, and etc. But, I want if "A" the previous button (ImageButton) will be hide and if "Z" the next button will be hide.
public class ScrBelajarHuruf extends AppCompatActivity implements View.OnClickListener {
TextView txtHuruf;
ImageButton btnAudioHuruf, btnNextHuruf, btnPreviousHuruf;
MediaPlayer mediaPlayer;
int counterHurufList;
int counter = 0;
int counterAudioHurufList;
int[] stringHurufList = {
R.string.huruf_a,
R.string.huruf_b,
R.string.huruf_c,
R.string.huruf_d,
R.string.huruf_e,
R.string.huruf_f,
R.string.huruf_g,
R.string.huruf_h,
R.string.huruf_i,
R.string.huruf_j,
R.string.huruf_k,
R.string.huruf_l,
R.string.huruf_m,
R.string.huruf_n,
R.string.huruf_o,
R.string.huruf_p,
R.string.huruf_q,
R.string.huruf_r,
R.string.huruf_s,
R.string.huruf_t,
R.string.huruf_u,
R.string.huruf_v,
R.string.huruf_w,
R.string.huruf_x,
R.string.huruf_y,
R.string.huruf_z,
};
int[] rawAudioHurufList = {
R.raw.a,
R.raw.b,
R.raw.c,
R.raw.d,
R.raw.e,
R.raw.f,
R.raw.g,
R.raw.h,
R.raw.i,
R.raw.j,
R.raw.k,
R.raw.l,
R.raw.m,
R.raw.n,
R.raw.o,
R.raw.p,
R.raw.q,
R.raw.r,
R.raw.s,
R.raw.t,
R.raw.u,
R.raw.v,
R.raw.w,
R.raw.x,
R.raw.y,
R.raw.z,
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scr_belajar_huruf);
txtHuruf = findViewById(R.id.txtHuruf);
btnAudioHuruf = findViewById(R.id.btnAudioHuruf);
btnNextHuruf = findViewById(R.id.btnNextHuruf);
btnPreviousHuruf = findViewById(R.id.btnPreviousHuruf);
btnAudioHuruf.setOnClickListener(this);
btnNextHuruf.setOnClickListener(this);
btnPreviousHuruf.setOnClickListener(this);
mediaPlayer = MediaPlayer.create(this, R.raw.a);
mediaPlayer.start();
if(counterHurufList == 0) {
btnPreviousHuruf.setVisibility(View.INVISIBLE);
}
}
public void onClick(View view) {
int id = view.getId();
if(id == R.id.btnNextHuruf && counterHurufList < stringHurufList.length - 1 && counterAudioHurufList < rawAudioHurufList.length - 1) {
mediaPlayer.stop();
counterHurufList++;
counterAudioHurufList++;
} else if (id == R.id.btnPreviousHuruf && counterHurufList > 0 && counterAudioHurufList > 0) {
mediaPlayer.stop();
counterHurufList--;
counterAudioHurufList--;
}
txtHuruf.setText(stringHurufList[counterHurufList]);
mediaPlayer = MediaPlayer.create(this, rawAudioHurufList[counterAudioHurufList]);
mediaPlayer.start();
}
}
I tried with
if(counterHurufList == 0) {
btnPreviousHuruf.setVisibility(View.INVISIBLE);
}
But give the previous button hide from "A" to "Z"
Haven't run this code so hopefully it works, but here are my thoughts from what you've shown:
You only perform the check to hide the button in onCreate, so it'll only do it once when the activity is created
You only hide the go back button
You only hide the button, and don't try to make it visible again
In your onCreate method, remove the check
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scr_belajar_huruf);
txtHuruf = findViewById(R.id.txtHuruf);
btnAudioHuruf = findViewById(R.id.btnAudioHuruf);
btnNextHuruf = findViewById(R.id.btnNextHuruf);
btnPreviousHuruf = findViewById(R.id.btnPreviousHuruf);
btnAudioHuruf.setOnClickListener(this);
btnNextHuruf.setOnClickListener(this);
btnPreviousHuruf.setOnClickListener(this);
mediaPlayer = MediaPlayer.create(this, R.raw.a);
mediaPlayer.start();
}
And in your onClick method, make sure you address all the points I mentioned above:
public void onClick(View view) {
int id = view.getId();
if(id == R.id.btnNextHuruf && counterHurufList < stringHurufList.length - 1 && counterAudioHurufList < rawAudioHurufList.length - 1) {
mediaPlayer.stop();
counterHurufList++;
counterAudioHurufList++;
} else if (id == R.id.btnPreviousHuruf && counterHurufList > 0 && counterAudioHurufList > 0) {
mediaPlayer.stop();
counterHurufList--;
counterAudioHurufList--;
}
txtHuruf.setText(stringHurufList[counterHurufList]);
mediaPlayer = MediaPlayer.create(this, rawAudioHurufList[counterAudioHurufList]);
mediaPlayer.start();
// New parts
btnPreviousHuruf.setVisibility(View.VISIBLE);
btnNextHuruf.setVisibility(View.VISIBLE);
if (counterHurufList == 0)
btnPreviousHuruf.setVisibility(View.INVISIBLE);
if (counterHurufList == 25)
btnNextHuruf.setVisibility(View.INVISIBLE);
}
Obviously modify however you like, but ensure that you meet the three criteria :)
switch (stringHurufList[counterHurufList]) {
case "A":
btnPreviousHuruf.setVisibility(View.INVISIBLE);
break;
case "Z":
btnNextHuruf.setVisibility(View.INVISIBLE);
break;
default:
btnPreviousHuruf.setVisibility(View.VISIBLE);
btnNextHuruf.setVisibility(View.VISIBLE);
}
But give the previous button hide from "A" to "Z"
Its because you forgot to return btn visibility to VISIBLE.
Declare an instance variable let's say int currentPosition = 0;. First time make previous button invisible prevBtn.setVisibility(View.INVISIBLE); Use this single position for both next and previous functionality. Like:
public void onClick(View view) {
int id = view.getId();
if(id == R.id.btnNextHuruf) {
currentPosition++;
prevBtn.setVisibility(View.VISIBLE);
if (currentPosition == 25){
nextBtn.setVisibility(View.INVISIBLE);
mediaPlayer.stop();
}
//counterHurufList++;
//counterAudioHurufList++;
} else if (id == R.id.btnPreviousHuruf) {
currentPosition--;
nextBtn.setVisibility(View.VISIBLE);
if (currentPosition == 0){
prevBtn.setVisibility(View.INVISIBLE);
mediaPlayer.stop();
}
}
txtHuruf.setText(stringHurufList[currentPosition]);
mediaPlayer = MediaPlayer.create(this, rawAudioHurufList[currentPosition]);
mediaPlayer.start();
}

How to pass more than one argument?

I am working on Android Studio 3.1.3.
My application consists of eight buttons. I am trying to pass more than one button as arguments from onClick method
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
pauseAudio(audioTwo, audioThree, audioFour, audioFive, audioSix, audioSeven, audioEight);
mediaPlayer = MediaPlayer.create(this,R.raw.birds);
mediaPlayer.start();
break;
to pauseAudio method,
public void pauseAudio(View view, Button audioOne, Button audioTwo, Button audioThree, Button audioFour, Button audioFive, Button audioSix, Button audioSeven, Button audioEight){
if(audioOne.isEnabled()
|| audioTwo.isEnabled()
|| audioThree.isEnabled()
|| audioFour.isEnabled()
|| audioFive.isEnabled()
|| audioSix.isEnabled()
|| audioSeven.isEnabled()
|| audioEight.isEnabled()){
if(mediaPlayer.isPlaying() && mediaPlayer!=null){
mediaPlayer.stop();
}
}
}
this is error it is showing when I hover over the underlined text
I am guessing the way I am passing argument is not the correct way, so please help me out. Thank you in advance.
To make your code easier and hopefully work, try this:
public void pauseAudio(View... views) {
for(View view : views) {
if(view.isEnabled()) {
if(mediaPlayer!=null && mediaPlayer.isPlaying()){
mediaPlayer.stop();
}
break;
}
}
}
Now you can call it like this:
pauseAudio(audioOne, audioTwo, audioThree, ...);
EDIT:
Maybe also check if the view is not initialized:
if(view != null && view.isEnabled())
EDIT 2:
I'm glad to hear that it worked :) Here's a better explanation:
public void pauseAudio(View... views) { //Allow a dynamic initialization of an array of Views
for(View view : views) { //Loop through the array
if(view != null && view.isEnabled()) { //If the current view is not null and enabled
if(mediaPlayer!=null && mediaPlayer.isPlaying()){ //If the MediaPlayer is not null and playing
mediaPlayer.stop(); //Stop the MediaPlayer
}
break; //Break the loop, as we have reached our usecase
}
}
}
Maybe if you publish the log after crash we could help you better. but I already see a control that can crash
if (mediaPlayer.isPlaying () && mediaPlayer! = null) {
mediaPlayer.stop ();
}
. you could first check if mediaPlayer! = null before mediaPlayer.isPlaying () like this:
if (mediaPlayer! = null && mediaPlayer.isPlaying ()) {
mediaPlayer.stop ();
}

How to Stop Click Count in Java Android?

I have a problem related click count. The problem is, I can't stop click when a number a click is given.
For example, I allow users to click a button 3 times, if clicks reached 3 times, then stop count, and do what I want.
This is my code I have used.
private int clickcount = 3;
#Override
public void onClick(View v) {
// Do button click handling here
if ( posisi2==getAdapterPosition() ) {
clickcount--;
tombolbaca.setText("Baca " + clickcount + "x");
// try to stop count but it can't, computer still counting
if (clickcount == 3)
{
mTitle.setVisibility(View.GONE);
rl2.setVisibility(View.GONE);
}
} // adapter
} // onClick
I think the trigger to do something might be when the click count is zero, not three:
if (clickcount == 0) {
mTitle.setVisibility(View.GONE);
rl2.setVisibility(View.GONE);
}
It isn't clear whether the above if statement belongs nested inside the outer if, or if it should be at the method level of onClick().
Note: We could have written if (clickCount <= 0), but there may not be a need to do this (nor may it be desirable), since after you have changed the visibility of those elements to GONE once, you don't need to do it again.
Make this Change,
private int clickcount = 3;
#Override
public void onClick(View v) {
// Do button click handling here
if ( posisi2==getAdapterPosition() ) {
clickcount--;
tombolbaca.setText("Baca " + clickcount + "x");
// try to stop count but it can't
if (clickcount <=0) <== make this change
{
mTitle.setVisibility(View.GONE);
rl2.setVisibility(View.GONE);
}
} // adapter
}
try this
private int clickcount = 3;
#Override
public void onClick(View v) {
// Do button click handling here
if ( posisi2==getAdapterPosition() ) {
clickcount--;
tombolbaca.setText("Baca " + clickcount + "x");
// try to stop count but it can't, computer still counting
if (clickcount == 0)
{
mTitle.setVisibility(View.GONE);
rl2.setVisibility(View.GONE);
}
} // adapter
} // onClick
private int clickcount = 0;
#Override
public void onClick(View v) {
// Do button click handling here
if ( clickcount<3 ) {
clickcount++;
tombolbaca.setText("Baca " + clickcount + "x");
}
//Count stops here..
else
{
mTitle.setVisibility(View.GONE);
rl2.setVisibility(View.GONE);
}
}
}

Edit text parsing integer android

I am making android app and I have edit text surrounded by two buttons for increase and decrease
and when I click the button increase or decrease for the first time it did not work but it start working from the second time
e.g if the number in edit text field is 50 when I press increase it still 50 when I press increase again it change to 51 and again it change to 52 and so on
here is my java code for the two buttons
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (quantityEdit.getText().toString().equals("") || quantityEdit.getText().toString() == null) {
quantityEdit.setText("0");
} else {
int a = Integer.parseInt(quantityEdit.getText().toString());
int b = a + 1;
quantityEdit.setText(String.valueOf(b));
}
}
});
sub.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int a = Integer.parseInt(quantityEdit.getText().toString());
if (a >= 1) {
int b = a - 1;
quantityEdit.setText(String.valueOf(b));
} else {
quantityEdit.setText("0");
}
}
});
It looks like you are checking for empty string or null in "add", but not in "sub". As Michael Krause said, you should use TextUtils.isEmpty(), and set a default value before performing add or sub operations.
If you are setting a default value elsewhere, please show your code.
Consider refactoring your code like so
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
quantityEdit.setText(String.valueOf(getIntVal(quantityEdit) + 1));
}
});
sub.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int value = getIntVal(quantityEdit);
if (value > 0) {
value--;
}
quantityEdit.setText(String.valueOf(value));
}
});
// helper method to get the integer value of a TextView
private int getIntVal(TextView textView) {
CharSequence rawValue = textView.getText();
int value;
if (!TextUtils.isEmpty(rawValue)) {
try {
value = Integer.parseInt(rawValue.toString());
} catch (NumberFormatException e) {
value = 0;
// TODO log / notify user
}
} else {
value = 0;
}
}
This way your initialization is handled in one place and it's more clear.
In any event, your first condition is wrong. You do a null check after trying to access the object (it's backwards). Have you checked your logs? This could throw an exception for a null value and "lose" the first click.

how to use onClick function within a condition or calling it in a function? Android

i want to use my PlayerClick(view v) inside a if condition . but it gives me null Exception. it has to retrieve button id.
//Main Code where i wan to call
while (gameLoop > 0){
while(PlayerClick(null) != 1){
if( WinnerCheck(currentUser.getSymbolValue()) == 1 || WinnerCheck(currentUser.getSymbolValue()) == 0 ){
return currentUser;
}
if (gameLoop % 2 == 0)
currentUser = me;
else
currentUser = opponent;
}
gameLoop--;
}
Function Is here
public int PlayerClick(View v) {
//
switch(v.getId()){
case R.id.btnone:
return -1;
case R.id.btntwo:
return -1;
}
return 1;
}
Aim to do this just to pause a while loop while right button is being clicked
im using android:onclick = "PlayerClick" for four buttons in XML
i want to use my PlayerClick(view v) inside a if condition . but it
gives me null Exception.
Because you are passing null to PlayerClick method.
To get it work you should pass Button view which is pressed by user as parameter in PlayerClick method.
findViewById(android.R.id.yourlayout) pass this in PlayerClick()
like
PlayerClick(findViewById(android.R.id.yourlayout))
Do
button.setOnClickListener(this);
Let your activity implement OnClickListener
And when you get onClick(View v) of activity, do as below
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_nativex:
PlayerClick(v);
break;
default:
break;
}
}
public int mPlayerClick;
public void PlayerClick(View v) {
int viewId = v.getId();
if (viewId == R.id.btnone) { mPlayerClick = -1; }
if (viewId == R.id.btntwo) { mPlayerClick = -1; }
else mPlayerClick = 1;
}
And then.
while (gameLoop > 0){
while(mPlayerClick != 1){
...
I don't understand what you are trying to do though, so this might be wrong. This way mPlayerClick will be set to 1 (and stop your loop) only if the user clicks on some button that is not either btnone or btntwo.
Of course all of your buttons need to call PlayerClick() on click.

Categories

Resources