How to avoid consecutive button clik for same button in android - java

i want to avoid click event again on the same button continuously (i.e) consecutively click on same button
whether its possible or not

yes it is possible in differents ways :
1) First method : use a boolean variable and update it when you want :
boolean isClickable = true;
Button btn;
// other declarations..
// in the onCreate method :
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(this);
onClick() method :
#Override
public void onClick(View v) {
if(isClickable) {
//this code will be executed only when you set the boolean isClickable to true
//your code here
}
}
And when you want to desactivate you click button ; just set the boolean to false :
isClickable = false;
2) Second method :
there is a method on View which will let your button non Clickable ; and then desactivate the listener on your Button so no Object will listen the event click on your button like this :
btn.setClickable(false);
btn.setOnClickListener(null);
NB : when you want to turn your button clickable again , just set the listener to this and make it clickable again ( for the Second Method ) :
btn.setClickable(true);
btn.setOnClickListener(this);
or just update the value of the boolean isClickable (for the first method)
isClickable = true;

you can set the onClickListener to null when you want to disable handling of this event, and set it back to the same listener you've had before in order to re-enable it .
of course , there are many solutions for this as Houcine has written .

Related

TextView setOnFocusChangeListener not called on button click

I've an application UI with
some TextView inside a ViewHolder
some buttons
If the user change the text inside one of the TextView I need to check the value to be sure that is between some ranges. The check must be done immediately after the user change focus (as example because click on one button, this is needed because I had to do some actions in response to some specific values).
I implemented the check setOnFocusChangeListener() but seems that the event is not fired on button click but only if I click on another TextView
How can I be sure that the value is checked on any kind of event like back button, other UI button click and so on?
For performance reason I prefer that the check is performed only on focus change.
Below my current code for one of the TextView that are inside the ViewHolder:
holder.articleQuantity.setOnFocusChangeListener((v, hasFocus) -> {
if (!hasFocus) {
EditText articleQuantity = (EditText) v;
String currentQuantity = articleQuantity.getText().toString();
if(ArticleModelArrayList.size()>0) {
int adapterPos = holder.getAdapterPosition();
if(adapterPos > 0) {
ArticleModel article = ArticleModelArrayList.get(adapterPos);
if (!currentQuantity.equals("") && (Integer.parseInt(currentQuantity) > 0)) {
article.setQuantity(Integer.parseInt(currentQuantity));
}
articleQuantity.setText(article.getQuantity().toString());
ArticleModelArrayList.set(adapterPos, article);
}
}
}
});

2 OnClickListeners for one Button cannot resolve the OnClick Listener

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

Can't re-enable a view after disabling it with CheckBox

I'm trying to enable/disable a seekbar when a checkbox is checked(or not).
I'm using this to create and refer:
CheckBox checkBoxProva = (CheckBox) findViewById(R.id.checkbox_prova);
boolean varCheckBoxProva = checkBoxProva.isChecked();
And this simple if to disable/enable the view:
if (!varCheckBoxProva) {
seekBarProva.setEnabled(false);
}
All this is inside the onCreate.
When the app starts the seekbar is disabled (so the if works), but if I check the CheckBox it doesn't change to enabled.
EDIT: I've succed thank to the reply of #rohan bhatia.
You are doing all of this in your onCreate method that only gets called when the activity is first created. You need to setup a listener that will be fired when the checkbox is clicked. See: https://developer.android.com/guide/topics/ui/controls/checkbox.html
You need a Listener that notifies when a checkbox is selected or deselected..
checkBoxProva.setOnCheckedChangeListener(new new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
seekBarProva.setEnabled(isChecked);
}
}
);
Accordingly, whenever you click the checkbox the method onCheckedChanged() will be executed.. You will still need to specify this in onCreate -
seekBarProva.setEnabled(checkBoxProva.isChecked());
As onCheckedChanged() only listens for changes in checked state, so to specify initial stage you will need that above line too.
Simply insert an else block
if (!varCheckBoxProva) {
seekBarProva.setEnabled(false);
}else{
seekBarProva.setEnabled(true);
}
Alternative solution:
Personally, I'd recommend adding a listener to the checkbox inside your onCreate method.
checkBoxProva.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(varCheckBoxProva){
seekBarProva.setEnabled(true);
}else{
seekBarProva.setEnabled(false);
}
}
});

How can i stop sending messages which are sending in loop on click of an button in android

I want to know how can I stop message sending in a loop.
When I press stop, messages are still sent.
I have provided a stop() method.
for (i = 0; i < copy; i++) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
if(stop!=1)
sms.sendTextMessage(number,null,message,sentPIn,pdelint);
}
}, 25000);
stop method
int stop=0;
public void st(View view){
Button b=(Button)findViewById(R.id.button2);
b.setText("Stopped");
stop=1;
}
Modify St function to this
public void st() {
Button b = (Button) findViewById(R.id.button);
#Override
b.setOnClickListener(new View.OnClickListener() {
b.setText("Stopped!");
stop=1;
});
}
The problem was u were not using an listener to detect button press. It is not advisable to change the button text use a text view instead!
Most likely he has set onClick attribute in the xml, although only he'll be able to clarify that.
I'm saying that because I see View as a parameter in the method st(View v).
Although if the attribute onClick is not set in xml #Audi 's solution would be good. Though on the contrary if the attribute has been set in the xml then
you should check if view.getId() == R.id.button then apply a button cast to your view and set its text to "Stopped".

Android: Button On Click listener: Single and constant when held down

I am unsure how to word this right but I want a button / on click listener to do this
1 touch = 1 tap = generate 1 row
Hold button down = generate 1 row per seconds or x per second
You know in games like clash of clans when u recruit soldier, u can touch it for 1 soldier or hold it down for more. I want a similar implementation but for a button
Thank you
For one single tap to generate 1 row, use:-
Button b = (Button)findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//put your code here for single tap event
}
});
For long press use the following:-
b.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
//your code here for long press event
}
});

Categories