I have a button in my Android Studio project.I want to implement a functionality that once a user clicks the button value is updated by once now if user clicks it again I dont want to update value. Edited These buttons are in recycler view
CodePudding user response:
Just use a boolean. If the button is clicked set the boolean to true and check on each click if the boolean is false.
Should look something like this:
boolean wasClicked = false;
final Button button = findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(!wasClicked){
//Your actions for your button-click
wasClicked = true;
}
}
});
CodePudding user response:
Button button = (Button)findviewbyId("Button ID")
boolen isClicked = false;
public void onClick(View v){
switch(v.getId()){
case R.id."buttonID"{
if(!isClicked)
isClicked = true;
updateData()// Your Method where your are updating the data;
break;
}
public void updateData(){
if(isClicked){
"Update your data"
}
else{
"do another action"
}
CodePudding user response:
This code is useful if you use RecyclerView.
public MyViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.myview, parent, false);
view.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// add some code
view.setEnabled(false);
}
});
return new MyViewHolder(view);
}
I think this code is useful if you use Button.
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// add some code
button.setEnabled(false);
}
});