How do I get the initial entry to return when I click the FloatingActionButton again? Here is the code:

 final FloatingActionButton btnKzt = (FloatingActionButton)header_0.findViewById(R.id.btn_kzt); btnKzt.setImageDrawable(getResources().getDrawable(R.drawable.ic_kzt)); btnKzt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { btnKzt.setImageDrawable(getResources().getDrawable(R.drawable.ic_usd)); } }); 

When you run the application inside the fab there is a KZT entry, when pressed, the entry changes to USD. How to make the second click to return the record KZT ??

    1 answer 1

    announce somewhere in the main class field boolean isUSD = false .
    Next, change your code like this:

     final FloatingActionButton btnKzt = (FloatingActionButton)header_0.findViewById(R.id.btn_kzt); btnKzt.setImageDrawable(getResources().getDrawable(R.drawable.ic_kzt)); btnKzt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { btnKzt.setImageDrawable(getResources().getDrawable(isUSD ? R.drawable.ic_kzt : R.drawable.ic_usd)); isUSD = !isUSD; } }); 

    P.S.
    isUSD ? R.drawable.ic_kzt : R.drawable.ic_usd this entry isUSD ? R.drawable.ic_kzt : R.drawable.ic_usd isUSD ? R.drawable.ic_kzt : R.drawable.ic_usd is a ternary operator. It is similar to the record:

     if(isUSD) { btnKzt.setImageDrawable(getResources().getDrawable(R.drawable.ic_kzt); } else { btnKzt.setImageDrawable(getResources().getDrawable(R.drawable.ic_usd); } 

    But it is written much shorter