How can i stop executing method when i click on a button??
I need a code that can stop executing codes in a specific method.
For example..
public class MainLoginActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
final Button lbutton = (Button) findViewById(R.id.buttonL);
lbutton.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View view) {
///there i need a code that stop the execution of myMethod()
}
}
);
myMethod();
}
public void myMethod() { /*all codes i want*/ }
I would suggest the use of a boolean flag.
For example you would have a boolean flag called "stopExecution" which by default is set to false. This flag is then set to "true" by your button click event.
In your method you can then check before every statement whether this flag is still set to false. If so, execute the statement. If however the flag is set to true you would call "return" (or "break" depending on whether a loop is being use) to return from the method and stop executing any more code.
If you have a for loop the following could be done:
for(int i = 0; i < size; i++){
if(stopExecution){
break;
}else{
//execute statement
}
}
Related
I've created a method that is called when my EditText is clicked (or touched, to be specific), which simply takes the current time and displays it exactly as it has been passed by System.currentTimeMillis() in a TextView below. The method is this one:
public void captureTime(View view) {
currentTime = System.currentTimeMillis();
String currentTimeStr = currentTime+ "";
TextView textView = findViewById(R.id.textView);
textView.setText(currentTimeStr);
}
To do that, I've had to add the code below in the onCreate method. It uses setOnTouchListener instead of setOnClickListener since otherwise the captureTime method wasn't always called. Now, this method does what I wanted, but now when I click or touch the EditText, although it does this as required, the keyboard doesn't show up anymore. I have looked into other questions in which the keyboard doesn't show up either but they're not related to my case, although I have tried them. So, what is that is making the keyboard not show up and how can I fix this?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add_time = (EditText)findViewById(R.id.editText);
add_time.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
captureTime(add_time);
return true;
}
return false;
}
});
}
Try removing return true (in the if block). In this case, the "captureTime" method will work and the keyboard will open.
//Initializing clickCount
int clickCount = 0;
public void animateButton(View view) {
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clickCount++;
if(clickCount%2==0 && clickCount==0){ //clickCount=0 declared in global variable
button.animate().translationX(400);
}
else {
button.animate().translationX(-400);
}
}
});
Or you can suggest any other method too.
Your if statement is true only once, at initialization. Afterwards clickCount is 1,2,3,... which is clickCount==0 : False so the if statement is also false.
I have to implement a return method of a class. So the structure is as simple as
public boolean yesNo(String message){
//return something
}
The problem here is that there is an EditText object and a Button and I want return a boolean depending on the answer entered by the User in the EditText. So naturally I have to wait for the user to write and click the Button when he finishes. I already tried setting up an onClicklistener inside the method but then I would not be able to pass anything to the outer method.
I know that in android everything is asynchronous so I you are not supposed to wait for the user to do something but in this case I have no other idea. Also FYI the method that I'm implementing is of the UserInfo class of the Jsch library. From what i understood this UserInfo is designed for user interaction so I figured it would somehow manage "prompt" the user by calling the yesNo method and then "wait" for the user to react. Any ideas? Also I would be happy if anyone would explain to me if there is some error in my logic. Im not quite confident in using AsynTask or runnables but I think its got something to do with this.
The only available documentation of UserInfo class
Thanks a lot!
EDIT: code so far:
public static class MyUserInfo implements UserInfo, UIKeyboardInteractive {
public Button goButton = (Button) MainActivity.contentView.findViewById(R.id.startSSH);
public EditText editText = (EditText) MainActivity.contentView.findViewById(R.id.commandSSH);
public boolean yesNo = false;
public boolean promptYesNo(String str) {
goButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
String answer = editText.getText().toString();
if(answer.equals("yes")){yesNo = true;}
else {yesNo = false;}
}
});
return yesNo;
}
}
Put an OnClickListener on the Button when the screen is initialized, for example at onCreate lifecycle method. At the listener's onClick method you can call the yesNo method with the EditText content.
Try this way at the screen's onCreate method:
mButton = (Button)findViewById(R.id.my_button);
mEditText = (EditText) findViewById(R.id.my_edittext);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
yesNo(mEditText.getText().toString());
}
});
I already tried setting up an onClicklistener inside the method but
then i would not be able to pass anything to the outer method.
What do you want to pass to the outer method? You can create class variables to make it available inside the whole class.
The promptYesNo method:
public void promptYesNo(String s) {
if(s.equals("Yes") //do something
else if(s.equals("No") //do something
}
Change your code this way:
public static class MyUserInfo implements UserInfo, UIKeyboardInteractive {
public Button goButton = (Button) MainActivity.contentView.findViewById(R.id.startSSH);
public EditText editText = (EditText) MainActivity.contentView.findViewById(R.id.commandSSH);
goButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
if (prompt) {
promptYesNo(editText.getText().toString());
}
}
});
}
I have a view (button) and am listening to a click event, but in my while loop it won't call the onClick method:
public class Activity_Main extends Activity implements OnClickListener {
Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button)findViewById(R.id.btn_1);
button1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_1:
//do something make boolean false for example
break;
}
}
while(boolean == true) {
//it's not reacting on the click
}
Is there something I am missing?
You are blocking the main thread with that infinite while loop:
while(boolean == true) {
//it's not reacting on the click
}
and it cant execute any other code. You should be more worried about the ANR coming next rather then the click not responding. Put the while loop in a separate thread as suggested and dont overload the main thread's work with excessive tasks.
If you use Handlers and Messages you can start and stop the recording when the user clicks the button. And to check if the button has been clicked, just use onClick method.
When I execute the code, it always goes to the else condition. But I want the if statement to run when score=true; I cannot figure out how to do this...kindly help me out.
If not this way, is there any other way I can approach?
public class withComp extends Activity {
public boolean isPlayer2=false, score=false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.withcomp);
final Button one = (Button)findViewById(R.id.one);
final TextView winner = (TextView)findViewById(R.id.winner);
one.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if(isPlayer2==false)
{
one.setText("X");
score = true;
one.setEnabled(false);
isPlayer2 = true;
}
else
{
one.setText("O");
one.setEnabled(false);
isPlayer2=false;
}
}
});
if(score == true)
{
winner.setText("won");
}
else {
winner.setText("lose");
}
}
}
Just use
if(score)
Without the comma
You don't need to write
if (variable == true) or if (variable == false)
It's much better just to write
if (variable) or if (!variable)
You're executing the If-Else only once when the activity is created.
Move the if-else-code into the OnClickListener.
And of course remove the semicolon after the if.
For example:
public class withComp extends Activity {
public boolean isPlayer2=false, score=false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.withcomp);
final Button one = (Button)findViewById(R.id.one);
final TextView winner = (TextView)findViewById(R.id.winner);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!isPlayer2) {
one.setText("X");
score = true;
one.setEnabled(false);
isPlayer2 = true;
} else {
one.setText("O");
one.setEnabled(false);
isPlayer2=false;
}
// XXX Move it here
if (score) {
winner.setText("won");
} else {
winner.setText("lose");
}
}
});
}
}
Your problem lies in if(score == true);
if clauses do not need a semicolon after the condition, but a statement. Giving it a semicolon passes it the null statement, meaning nothing will happen.
After that it will execute both the blocks following, overwriting what happens in the block you intended to be executed if score is true.
You can fix this simply by removing the semicolon, letting it correctly execute the block.
Semicolon means end of statement.Remove ;
from if(score == true);
Use it this way"
if(score){
winner.setText("won");
}
else {
winner.setText("lose");
}
a few things,
one as already was pointed out, you are using semicolon ; after if statement, and i'm supprised you were able to compiled and execute this code
two, your if-else block is executed in method create and you setting value of score in listener, which means, blockif-else will be executed after you create your activity, while value score will be setted to true after press button