I need to perform an action after onClick method of OnClickListener has run.
Here is my code for onClickListener:
View.OnClickListener imgButton0Handler0 = new View.OnClickListener() {
int identifier=0;
public void onClick(View v) {
//check if tile is found and return if it is
if(isFound[identifier]==true) return;
//set tile as open
checkField[identifier]=1;
//set background on predetermined
button0.setBackgroundResource(tiles[identifier]);
}
};
After this has run, and the background is set I would like to call a method checker(int identifier) which will check for other open tiles and change backgrounds accordingly.
This method needs to be run separately because the background is only displayed after onClick finishes, and I need predetermined background shown for a short time before checker method changes it to something else.
How can I accomplish this?
Have You Tried Post Delayed see this,
View.OnClickListener imgButton0Handler0 = new View.OnClickListener() {
int identifier=0;
public void onClick(View v) {
//check if tile is found and return if it is
if(isFound[identifier]==true) return;
//set tile as open
checkField[identifier]=1;
//set background on predetermined
button0.setBackgroundResource(tiles[identifier]);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
checker(identifier) // your method call
}
}, 3000); // 3 second
}
};
Related
I added the following lines of Code into my OnCreate method.My goal is to assign a button to two functions and to call them up alternately. With the first click the text of the button should be changed and the EditText should be editable. At the second click, the fields should no longer be editable and the button text should change to the first alternative. I have implemented two OnClickListeners and the program structure seems logical to me. Nevertheless, I get an error message; "Cannot resolve symbol onClickListener". What can I do to get the setup described above up and running? Thanks for all responses!
private Button ProfilUpdate;
ProfilUpdate=findViewById(R.id.buttonProfilUpdate);
.
.
.
.
final ProfilUpdate.OnClickListener listener2 = new View.OnClickListener() {
#Override
public void onClick(View v) {
ProfilUpdate.setText("Profil bearbeiten");
profilVorname.setFocusable(false);
}
};
ProfilUpdate.OnClickListener listener1 = new View.OnClickListener() {
#Override
public void onClick(View v) {
ProfilUpdate.setText("Ă„nderungen speichern");
profilVorname.setFocusable(true);
v.setOnClickListener(listener2);
}
};
ProfilUpdate.setOnClickListener(listener1);
why don't you create a boolean isFirstClick = true , and then check it in the same listener
ProfilUpdate.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isFirstClick){
//Do the job for the first click process
isFirstClick= false;
}else {
//Do the job for the second click process
isFirstClick= true;
}
}
};
ProfilUpdate.setOnClickListener(listener);
There can only be one click listener on one view at a time. Use ProfileUpdate.setOnClickListener(listener object). To get the alternate functionality, you can define a Boolean to keep track of the state, for the example, define a class variable at the top Boolean shouldChangeText = true, and in the onClick body in the listener, do something like:
If (shouldChangeText) { // change the text
}
else { // clear the text
}
shouldChangeText = !shouldChangeText
I am making an android app. Here's what I want: User clicks a button, button text changes to "xyz", and then the program waits for 1 second and then the button text changes to "abc".If I use Thread.sleep(1000) then the program does stop for a second but the button text doesn't change to "xyz" before the program goes to sleep.
You can use Handler to achieve this. Using postdelayed method you can do this. Below is the code to do this
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
button.setText("xyz"); // text changed to xyz
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
button.setText("abc"); //text changed to abc after 1 second.
}
},1000);
}
});
The problem is that I have an Android app that doesn't seem to show the xml layout when I put this while loop into the class file. The loop is as follows:
while(!clicked){
button_a.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player= MediaPlayer.create(GameActivity.this, R.raw.a);
player.start();
clicked = true;
letterTapped = 0;
}
});
}
The whole project works completely fine without it so I'm pretty sure that there must be something wrong with the loop that I am overlooking.
If you want me to put any other bits of code up here I will be more than happy to.
To stop listening as soon as the button is pressed, you can use this code:
button_a.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
player= MediaPlayer.create(GameActivity.this, R.raw.a);
player.start();
letterTapped = 0;
// Ignore further clicks
button_a.setOnClickListener(null);
// Disable button so the user knows that he can't click again
button_a.setEnabled(false);
}
});
I am using the android developer tools in Eclipse, programming in Java, and I need to make an object move across the screen as long as a button is pressed. I've been doing research for hours, and I cannot find any methods to accomplish this. I've tried running threads, which often crash or seemingly don't execute. I've also tried an onClickListener which reads the button state and uses it to determine whether or not the button is still pressed. I'm currently using a while loop, but this just freezes the program. I believe that this is the best method, and I've tried to use Thread.sleep in order to limit the number of iterations per second, as I believe that this is the reason it is freezing. Am I on the right track or am I way off in left field? Here is a snippet of code:
rightButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
while(arg0.isPressed())
{
mover.updateCoordinates(1, 0);
}
}
});
Would you try this another method?
Firstly declare your button as class variable, declare a Handler and a Runnable:
private Button rightButton; // You will assign this in onCreate() method
private Handler mHandler = new Handler();
private Runnable mRunnable = new Runnable() {
#Override
public void run() {
if(rightButton.isPressed())
{
// If press state is pressed, move your item and recall the runnable in 100 milliseconds.
mover.updateCoordinates(1, 0);
mHandler.postDelayed(mRunnable, 100);
}
}
};
Then your button's onClickListener will looks like this:
rightButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0)
{
// Instead of performing a loop here, just call a runnable, do simple press state checking there.
mHandler.postDelayed(mRunnable, 100);
}
});
Create a loop that updates the views, waits, and calls itself after it finishes waiting. Within the loop, have an animation if statement with a boolean field that move on true and does not move on false. Have the onClick method toggle the boolean field's value.
Try:
myView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
// Start your animation here
} else if (e.getAction() == MotionEvent.ACTION_UP) {
// Stop your animation here
}
return false;
}
});
References:
MotionEvent
OnTouchListener
Alternatively, override onKeyUp and onKeyDown of the View class and check for the KEYCODE_ENTER KeyEvent
well at school every week we are making a calculator each week on a different platform (wp7,javascript,iphone,android), today it's android, so i have a method that receives all the keystrokes and depending on the value of the key my class do something to get the value of the button in c# is the sender parameter , in javascript this , in android??
private OnClickListener leclicke= new OnClickListener() {
public void onClick(View v) {
//get the id of the button that fired the click event
//findViewById(R.id.???);
} };
thank you.
private OnClickListener leclicke= new OnClickListener() {
public void onClick(View v) {
//get the id of the button that fired the click event
int id = v.getId();
} };
then you must check if this view has an id or not using this
if(id == View.NO_ID){
//this view does not have an id
}
else{
//the view has an id
}
Call the method getId()
v.getId();
If you want to use the same OnClickListener for all buttons, then you can do something like this:
Button b1 = (Button)findViewById(R.id.button1);
Button b1 = (Button)findViewById(R.id.button2);
private OnClickListener leclicke= new OnClickListener() {
public void onClick(View v) {
if(v.equals(b1)) {
// handle button 1
} else if(v.equals(b2)) {
// handle button 2
} // etc.
} };
But this is a little clunky. Another thing you can do is specify a separate on click method for each method by setting the on click property for the button in the UI Designer, and then declaring that method in your Activity, e.g. public onClickButton1(View v);