Vidya Sagar
Vidya Sagar

Reputation: 180

Button OnClick action initiated after 6 seconds in android

I have 5 buttons in my activity inside a linear layout. I have written a code to display a toast message when I click one of those buttons. It is displaying the toast message after 6 seconds of Click action. I couldn't think what the problem could be.. Here is the code I have written in android studio

public class HomePage extends AppCompatActivity implements View.OnClickListener {

     private Button loginButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page);

        makeNotificationBarTransparent();

        loginButton = (Button)findViewById(R.id.login_btn);

        loginButton.setOnClickListener(this);





        Intent i = getIntent();
        Toast.makeText(getApplicationContext(),i.getStringExtra("UserName"),Toast.LENGTH_LONG).show();
    }

    private void makeNotificationBarTransparent() {
        //Making notification bar transparent
        if(Build.VERSION.SDK_INT >= 21){
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.login_btn:
                Toast.makeText(getApplicationContext(),"CLicked",Toast.LENGTH_LONG).show();
                break;
        }
    }
}

Can anyone tell me What could be the problem?

Upvotes: 0

Views: 469

Answers (2)

SGX
SGX

Reputation: 3353

You mean the Toast message will be show after clicking the button for 6 seconds? Try to use this:

Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
           @Override
           public void run() {
               // show toast here...
           }
    }, 6000); // 6 seconds

Or you can use CountDownTimer:

new CountDownTimer(6000, 1000) {

    public void onTick(long millisUntilFinished) {
       // do every 1 second
    }

    public void onFinish() {
        // show your toast after 6 seconds.
    }

}.start();

Is this what you need? If not, tell me more about your problems :)

Upvotes: 0

Daniel
Daniel

Reputation: 1729

It might be the fact that you show another toast message that you set to show up, as two toast messages cannot display at once.

I am referring to this toast message:

        Toast.makeText(getApplicationContext(),i.getStringExtra("UserName"),Toast.LENGTH_LONG).show();

Upvotes: 2

Related Questions