I need to click once to implement the methods in Onclick but the problem here that in counter it won't count until i click button constantly and i have several check conditions that print text to the user if true these texts don't show up until i click the button one more time , how to handle that issue ? i want the button to be clicked once then all the code inside Onclick implemented properly
Life.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{ counterLife();
Help.setVisibility(View.INVISIBLE);
Reset.setVisibility(View.VISIBLE);
// other code
.............
void counterLife() //To count
{
if (a && T2 ==0 && T3 == 0)
{
if(countLife == 6)
{ //code
text.setText("You Completed 6 Lives ");
}
else
countLife ++ ;
}
The first time you click you should use the OnFocusChangeListener to get the event.
After foucus is on the View all extra clicks will invoke the OnClickListener event.
You can also do this:
setOnFocusChangeListener(new View.OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// call your onclick method
}
}
});
Related
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.
I have a button like a switch where I am trying to setClickable(false) after I click it so that only the first click will be handled
(additional clicks are ignored in the case of accidental double-clicks/multiple-clicks).
Here is a similar code:
Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button.setClickable(false);
//do other things
}
});
Then eventually, I have a code somewhere where I will reset the clickable to true, depending on a state variable, so I can switch-off.
The problem is when I click the button very quickly, it seems the succeeding clicks are still handled.
Is there a delay to the effects of setClickable()?
Also, I have read about using setEnabled(false) instead, but I cannot use it in my case. I need the button to still be enabled but not clickable.
Judging from your comment you probably need something like this.
Boolean SWITCH_ON = false;
Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(!SWITCH_ON ){
SWITCH_ON = true;
}
}
});
Button.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
if(SWITCH_ON ){
// do your task for long click here ...SWITCH_ON
}
return true;
}
});
You can use button.setEnabled(false); within your onClick method to disable the button.
Disabled buttons don't trigger the onClick method, and you can easily re-enable it with button.setEnabled(true); when needed.
You could add another variable named buttonEnabled or so and initialize it with true. Then in the onclick do this:
Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button.setClickable(false);
if(buttonEnabled) {
//do other things
}
buttonEnabled = false;
}
});
Note that you need to change the variable to if you want to reactivate it
I have button with for inside his onclik. i Want stop 'for'or hide button with stopping method but i'm not able to do this.
I have tried with while and variable++ but it don't work with visability gone but it was hiding after 'for' completed.
public void siema(View view) {
for(i=2;i<5; i++){
some if's
}}
and what my problem is : if i click button 5 times it goes 5 times i want to block it or make method siema stop on second click of the button.
use this code that would help you
int count=1;
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(count>1)
{
//do action
}else{
count++;
}
}
});
this will help you.
i have listview with 4 items on it ,i need one click event on 3 items of listview.
so how to club three item to apply click event on it .
.
state ,view and comp these three items are on listview
holderView.state.setOnClickListener((View.OnClickListener) this);
holderView.view.setOnClickListener((View.OnClickListener) this);
holderView.comp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}});
thanks
You can do this in multiple ways.
1. Implement Onclick listener and set Listener to all.
holderView.state.setOnClickListener(this);
holderView.view.setOnClickListener(this);
holderView.comp.setOnClickListener(this);
#Override
public void onClick(View v) {
if (v.getId() == R.id.id1
|| v.getId() == R.id.id2
|| v.getId() == R.id.id3) {
// Do your task here
}
}
2. Create a single instance of OnCLickListener and set to all.
View.OnClickListener clickListener=new View.OnClickListener() {
#Override
public void onClick(View v) {
// Do your stuff
}
};
Set it to all your views.
holderView.state.setOnClickListener(clickListener);
holderView.view.setOnClickListener(clickListener);
holderView.comp.setOnClickListener(clickListener);
//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.