1

I referred this --> https://stackoverflow.com/a/31367723/12553303

I tried above solution but it is not working --> not displaying that drawable for few second

here is my code:

   buynow.setOnClickListener(object : View.OnClickListener{
        override fun onClick(v: View?) {
            // set the color red first.
   buynow.setBackgroundResource(R.drawable.mybuttonred)
            // change to original after 5 secs.
            Handler().postDelayed(Runnable { buynow.setBackgroundResource(R.drawable.mybutton)
                Toast.makeText(applicationContext,"ksjdf",Toast.LENGTH_LONG).show()
            },
                5000)
        }
    })

Even the toast is not working on click

what I am missing?

Mohamed AbdelraZek
  • 2,503
  • 4
  • 25
  • 36
IRON MAN
  • 191
  • 3
  • 18
  • Since you called **setOnClickListener** on your view(in your case buynow). You dont need to call **setBackgroundResource again on buynow**. You have the view(buynow) passed as **v** in **onclick method**, you can use it. **Example - v. setBackgroundResource**.. Do handle the **nullabillity** – Debarshi Bhattacharjee Oct 13 '20 at 07:08
  • Are you sure `buynow` view is clickable? Maybe click listener is removed/replaced somewhere else in your code. Do you have any other invocations of `buynow.setOnClickListener(...)`? – Jenea Vranceanu Oct 13 '20 at 07:14
  • im trying @DevBhattacharjee – IRON MAN Oct 13 '20 at 07:16
  • @JeneaVranceanu `Do you have any other invocations of buynow.setOnClickListener(...)?` ---> No – IRON MAN Oct 13 '20 at 07:17
  • Can you provide the entire class code?? – Debarshi Bhattacharjee Oct 13 '20 at 07:21
  • @DevBhattacharjee this is my entire code of button click ...i dont think posting everything will solve anything..code is too big so i cant post...it will irrelevent to post up all – IRON MAN Oct 13 '20 at 07:22

1 Answers1

1

This works fine with me you need to make view final so you can access it inside handler body

buyNow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View view) {
            view.setBackgroundColor(Color.RED); //set the color to red
            // Delay of 2 seconds (200 ms) before changing back the color to black
            final Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    view.setBackgroundColor(Color.BLACK); //set the color to black
                }
            }, 200);
        }
    });
Mohamed AbdelraZek
  • 2,503
  • 4
  • 25
  • 36
  • hi can u help me out here ->https://stackoverflow.com/questions/76480901/facing-issue-while-making-dynamic-views-using-relative-layout – IRON MAN Jun 15 '23 at 11:21