Search code examples
androidbuttonandroid-relativelayout

android button click event is not working for me


I have an android application with relative layout where i have placed a button control like this

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >    
<Button
        android:id="@+id/btnCalc"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="708dp"
        android:layout_marginLeft="50dp"
        android:text="@string/btncalc" />
</RelativeLayout>

And For testing the event i have placed toast on click event of this button like this

btncalcu=(Button)findViewById(R.id.btnCalc);
        btncalcu.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v)
            {
                Toast.makeText(getBaseContext(), "a", Toast.LENGTH_LONG).show();
                //calc();
            }

        });

Problem is that when i run the application there is not error or any exception but also nothing displayed there of Toast.Any Can Guess what may b the issue .Plz Help !


Solution

  • Simply try this way instead and see if it works,

        btncalcu.setOnClickListener(new OnClickListener() {
    
            public void onClick(View v) {
                 Toast.makeText(getBaseContext(), "a", Toast.LENGTH_LONG).show();
    
            }
        });
    

    And make sure you have imported the following,

    import android.view.View.OnClickListener;