How to pass more than one argument? - java

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

Related

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

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!

What data type is

i've created a function and set it to onClick, so every time i hit the button this function is called. It is called, but the value of 4 other functions i can't get them in setMessage() function(when we create a dialog box). So the logic is like, after hitting the button first we check which radioId1 is equal to which id, and call the appropriate fucntion(video,cons,news,pict).These 4 funct. return some string. Then i want to add these strings to some text in my dialog box, like ....SetMessage(checkMyId() + "Some text").SetPositiveButton(...)...
but the problem is my function checkMyId is set to onClick, so it recieves an argument View v, and when i call it "mannualy" in SetMessage() it shows error, because i don't know what to put inside that parenthesis. So the question is, what type of data(or whatever) is that View ? i saw some documentation and didn't understood much or at least how to implement that in my problem.
public void checkMyId(View v) {
int radioId1 = radioGroup.getCheckedRadioButtonId();
if (radioId1 == R.id.radio_one) {
video();
} else if (radioId1 == R.id.radio_two) {
pict();
} else if (radioId1 == R.id.radio_three) {
cons();
} else news();
}
EDITED
Picture of the error
sir you have 2 way
first try this ( HINT : Don't forget to implement ONCLICK in activity )
public void checkMyId(View v) {
switch (v.getId()) {
case R.id.radio_one:
video();
break;
case R.id.radio_two:
pict();
break;
case R.id.radio_three:
cons();
break;
default:
news();
break;
}
}
sec you can use it in OnCreate ( HINT : this on radio group ) HERE
(findViewById(R.id.radio_one)).setOnCheckedChangeListener(new
OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// checkedId is the RadioButton selected
}
});
____ READ This also for single RB

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.

How can I show a button when all views are viewed in viewpager?

Right now I have a save button that I want to show only if all the views inside a viewpager are shown. That means that when the user swipes between views and have seen all views inside a viewpager, then show a save button.
I want to show the save button on every view when they have seen all views hereby after.
The trouble I am having is how to set up the logic. I started out with setting the save button invisible until the last view of the viewpager. On the last view of the viewpager, show the save button. But the problem is when the user goes to the last view (there's a save button) and then goes back to a previous view, the save button is gone.
So, I was wondering how can I show the save button permanently on all views after the user has seen all views?
Here's what I have so far:
I have this snippet inside my InstantiateItem() :
if(isViewed)
{
save_button.setVisibility(Button.VISIBLE);
System.out.println("Is this called? isViewed = true");
}else if (position == numberOfPages.size()-1) {
isViewed = true;
save_button.setVisibility(Button.VISIBLE);
}
where
#Override
public void onPageSelected(int position) {
isViewed = true;
}
EDIT:
I tried the following solutions but with no luck.
Button save_button = (Button) findViewById(R.id.save);
if(isViewed[position])
{
save_button.setVisibility(Button.VISIBLE);
}
if (position == numberOfPages.length-1 && !isViewed[position]) {
isViewed[position] = true;
save_button.setVisibility(Button.VISIBLE);
}
isViewed[position] =true;
And
isViewed[position] = true;
if (isViewed[position] == isViewed[numberOfPages.length-1]) {
save_button.setVisibility(Button.VISIBLE);
}
if (isViewed[position]) {
save_button.setVisibility(Button.VISIBLE);
} else {
save_button.setVisibility(Button.INVISIBLE);
}
In your onPageSelected, do the following
if(isViewed)
{
save_button.setVisibility(Button.VISIBLE);
}
if (position == numberOfPages.size()-1) {
isViewed = true;
save_button.setVisibility(Button.VISIBLE);
}
Note the above are two seperate if statements.
Make your isViewed global and default to false.
boolean []isViewed = new boolean[noOfPages.size()];
#Override
public void onPageSelected(int position) {
if(isViewed[position])
{
save_button.setVisibility(Button.VISIBLE);
}
else {
save_button.setVisibility(Button.GONE);
}
isViewed[position] = true;
}

Starting AsyncTask in the onPostExecute

So here is how it is :
I have an AsyncTask with a TimerTask. Every 15 seconds the AsyncTask is run.
The AsyncTask gets XML data and put those in dynamically created TextView contained in dynamically create TableRow.
onPostExecute i create all my tableRow and TextView and then in order to not have them double instead of refresh i remove the tableRow views.
protected void onPostExecute(Document result)
{
if(TableLayout.getChildAt(1) == null)
{
//All my code to show tableRow and TextView
else
{
int u = 20;
while(tl.getChildAt(1) != null)
{
if(tl.getChildAt(u) != null)
tl.removeViewAt(u);
u--;
}
}
So yeah everything works just fine. I just want to know if there is some way to ask my TimerTask to restart right away instead of waiting x millis sec for timerTask so start again?
Thanks for your help!
So yeah i found out a way to do this. I'll post in case someone else wants to do something like that.
protected void onPostExecute(Document result)
{
if(tl.getChildAt(1) == null)
{
//do some code
}
else
{
int u = 20;
while(tl.getChildAt(1) != null)
{
if(tl.getChildAt(u) != null)
tl.removeViewAt(u);
u--;
}
//do same code over again as above.
}
}
}
This will delete the tableRows and recreate new ones thus "updating" them

Categories

Resources