Home > other >  restore default animation view of .setOnClickListener
restore default animation view of .setOnClickListener

Time:05-11

The Image of the issue

Before clicked, the view of "setOnClickListener" is like the right picture

The action of code setOnClickListener:

card_sales.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            view.startAnimation(fade);
            if(m_inventory.equals("1")){
                card_sales.setBackgroundColor(getResources().getColor(R.color.white_greyish));
                Intent profil = new Intent(getActivity(),InventoryActivity2.class);
                startActivity(profil);
            }else if(m_inventory.equals("0") || m_inventory.equals("") || m_inventory == null){
                Toast.makeText(getActivity(),"Access Denied",Toast.LENGTH_LONG).show();
            }
        }
    });

then i press back button from the activity

@Override
public void onBackPressed() {
    finish();
    formstock.setVisibility(View.VISIBLE);
}

and then the "setOnClickListener" view is like on the left picture.

Please, give me solution for this issue :(

CodePudding user response:

you shouldn't set a fixed color when button clicked with setBackgroundColor. instead use selector for background

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <color android:color="@color/white_greyish" />
    </item>
    <item>
        <color android:color="@color/white" />
    </item>
</selector>

save this file in drawable folder and set this in XML for card_sales

android:background="@drawable/filename"

imho even better effect (ripple) you can achieve with default selector

android:background="?android:attr/selectableItemBackground"

btw. when you call finish() then this Activity gets destroyed, so its unnecessary to call formstock.setVisibility(View.VISIBLE);. if you really need to show something at that moment and with above line it works as you expected - you have very wrong architecture and some memory leak for shure

  • Related