I want to disable a button when the user hits the button five times.
Here's the code
if(mLatestindex<=4) {
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
mIsCheater = false;
updateQuestion();
mFalseButton.setEnabled(true);
mTrueButton.setEnabled(true);
mLatestindex++;
}
}
Your mLatestIndex <= 4 is placed wrongly.
You should check the index inside the Click Listener
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mLatestIndex < 5) {
// Do the stuff
// This will execute only if the index is <= 4
}
mLatestIndex++;
}
});
This will execute the code only if mLatestIndex is less than 5, and increment it.
If you need disable button on 5 times click in general just add global counter and increment it in onClick() with check
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clickerCount++;
if (clickerCount >= 5) {
button.setEnabled(false);
}
}
If you need to disable view when it was clicked 5 times in certain amount of time, you could store timestamp of last clicked time and compare it with current time
Related
I am using following code for using a button. I works.(sendBtn is a button in a fragment)
sendText = view.findViewById(R.id.send_text);
View sendBtn = view.findViewById(R.id.send_btn);
sendBtn.setOnClickListener(v -> send(sendText.getText().toString()));
Now i want to disable the button for 1 sec after a click
I found following solution but above code works "without onClick(View v)"method and without implementing View.OnClickListener in class. How to provide delay in such case..How code is working without onClick method.
#Override
public void onClick(View v) {
((Button) findViewById(R.id.click)).setEnabled(false);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
((Button) findViewById(R.id.click))
.setEnabled(true);
}
}, 1000);
}
});
Use this method when you want to interrupt user clicks:
private static long mLastClickTime = 0L;
public static boolean isOpenRecently() {
if (SystemClock.elapsedRealtime() - mLastClickTime < 1000) {
return true;
}
mLastClickTime = SystemClock.elapsedRealtime();
return false;
}
Usage:
if (v.getId() == R.id.sendBtn) {
if (isOpenRecently()) return;
// Your logic
}
Basically you are using lambda in your code, where v->{} represents the onCLick(View v) function.
sendBtn.setOnClickListener(v -> send(sendText.getText().toString()));
You can do the following to disable the button for 1 second
void doOnSendButtonClick(View v){
//Send the message (your logic here)
send(sendText.getText().toString());
//Disable button
sendBtn.setEnabled(false);
//enable button after 1000 millisecond
new Handler(Looper.getMainLooper()).postDelayed(() -> {
sendBtn.setEnabled(true);
}, 1000);
}
And call this method when user clicks on the button
sendButton.setOnClickListener(view -> doOnSendButtonClick(view));
You could use a CountDownTimer.
CountDownTimewr timer = new CountDownTimer(1000, 1000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
// enable button
((Button) findViewById(R.id.click)).setEnabled(true);
}
}
#Override
public void onClick(View v) {
// disable btn and start timer of one second in millis
((Button) findViewById(R.id.click)).setEnabled(false);
timer.start();
}
});
Please try this way
Handler(Looper.getmainLooper()) and remove callback after you done your task inside runnable thread...also try to put this logic in any method outside in this class and call from setonclicklistener
Try the Timer.shedule with timertask
you can use runOnUIThread method inside where you can update UI.
you can also try to check the time difference of click and enable again in looping like while.
I'm trying to add a new food with some add ons, basically everytime the add field button is press it will create two edittext which is the name and price of the add on, there will be a scenario whereby users wants to delete the edittext name and price. I've successfully created two edittexts also setting them with the IDs that i'm giving them in integer. But I'm having problem with deleting the two edittext with a button whenever i click the delete field button.
// Add On
addOnLayout.removeAllViews();
addFields.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AddOnCounters++;
etFoodName = new EditText(addfoodAddOn.this);
etFoodName.setId(ETFoodID);
etFoodName.setHint("Enter your AddOn Name");
etPrice = new EditText(addfoodAddOn.this);
etPrice.setId(ETPriceID);
etPrice.setHint("Enter your AddOn Price");
Log.d("-------","-------------------what is edittext name id " + ETFoodID);
Log.d("-------","-------------------what is edittext price id " + ETPriceID);
ETFoodID++;
ETPriceID++;
addOnLayout.addView(etFoodName);
addOnLayout.addView(etPrice);
}
});
removeFields.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(AddOnCounters>=1 && ETFoodID > 0 && ETPriceID > 10){
addOnLayout.removeViewAt(ETFoodID);
addOnLayout.removeViewAt(ETPriceID);
ETFoodID--;
ETPriceID--;
}
}
});
I'm expecting the result to be whenever i click the delete field button, it will delete the very bottom last two edittexts which is the name and price.
Replace your listener
removeFields.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(AddOnCounters>=1 && ETFoodID > 0 && ETPriceID > 10){
View v1 = addOnLayout.findViewById(ETFoodID);
View v2 = addOnLayout.findViewById(ETPriceID);
addOnLayout.removeView(v1);
addOnLayout.removeView(v2);
ETFoodID--;
ETPriceID--;
}
}
});
After the button click just do this:
addFields.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
etPrice.setText("")
etFoodName.setText("");
}
}
Remove it from its parent.
removeFields.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
removeFromParent(etFoodName);
removeFromParent(etPrice);
}
});
static void removeFromParent(View view) {
((ViewGroup)view.getParent()).removeView(view);
}
I have two Button for increase and decrease the text size of a TextView.
They both work, but: if I increase and then decrease, the first click on the decrease button will increase the text, and the second and so on will decrease. And the other way around.
This is my code:
int txtSize = 18;
volumeUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTextView.setTextSize(txtSize++);
}
});
volumeDown.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mTextView.setTextSize(txtSize--);
}
});
Man. Try this. Avoid to increase or decrease a variable like that when you use it multiple time. Your code set first txtSize in TextView before increase it.
So in the second button, you set the increased value before decrease it.
int txtSize = 18;
volumeUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtSize = txtSize+1
mTextView.setTextSize(txtSize);
}
});
volumeDown.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtSize = txtSize-1
mTextView.setTextSize(txtSize);
}
});
Try to use ++texSize instead of using texSize++
I am using listView to change the value
here is my code. But It's not going to stop. when Listview last index no. suppose 10 then, it's crash. how do i stop next button when listView end.
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtOne.setText(letter[lastView+1]);
txtTwo.setText(letterDetails[lastView+1]);
txtThree.setText(letterB[lastView+1]);
lastView++;
if (lastView == letter.length){
lastView=0;
Toast.makeText(getApplicationContext(), lastView+" ", Toast.LENGTH_SHORT).show();
}
}
});
btnPrevious.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txtOne.setText(letter[lastView-1]);
txtTwo.setText(letterDetails[lastView-1]);
txtThree.setText(letterB[lastView-1]);
lastView--;
}
});
Basically letter.length return size of array. Suppose you have 10 items in array then the index will be 0-9.The reason behind your app is crashing is due to array out of bound error.
What you have to do is just add this line in your if condition.
Code
if (lastView == (letter.length-1))
How to change the android button after 2 clicks ?
the first time to change button i will use this code
{
public void onClick(View v) {
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed));
}
}
I want to change the button view again after pressing it one more time
how can i do that ?
Perhaps do it like this:
int count = 0;
public void onClick(View v) {
count++;
if(count == 2){
count = 0;
b.setBackgroundDrawable(getResources()
.getDrawable(R.drawable.menubuttonpressed));
}
}
This will set the background after every 2nd click on your button (view).
private int clickCount =0;
public void onClick(View v) {
if (clickCount==0) {
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed));
} else {
// do something else
}
clickCount++;
}
Well, one way is to keep a counter.
numberOfClicks = 0;
...
public void onClick(View v) {
...
if(numberOfClicks==0)
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed0));
else if(numberofClicks==1)
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.menubuttonpressed1));
...
numberofClicks++;
}