Issue
I have two drawable pictures, initially FAB is set to R.drawable.icon1, I want to set it to R.drawable.icon2 when clicking on it, and setting it back to icon1 when clicking one more time on it and so on...
Is there a way to do that?
Any help is appreciated!
Solution
Hope this helps, we have a boolean flag
which denotes the current icon visible in your fab.
FloatingActionButton fab;
boolean flag = true; // true if first icon is visible, false if second one is visible.
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(flag){
fab.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.icon2));
flag = false;
}else if(!flag){
fab.setImageDrawable(ContextCompat.getDrawable(getApplicationContext(), R.drawable.icon1));
flag = true;
}
}
});
Answered By - Chirag
Answer Checked By - Marie Seifert (JavaFixing Admin)