Khaled Mohamed
Khaled Mohamed

Reputation: 217

onclicklistener is not working

I simply created an application where when the button is clicked it shows a toast. But when I click on the button nothing happens. Here is the code:

package convert.project.convert;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;

    public class ConvertorActivity extends Activity implements OnClickListener {
        /** Called when the activity is first created. */
        EditText dollars,Egyptians;
        Button convert,clear;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
             dollars=(EditText) findViewById(R.id.dollarsET);
             Egyptians=(EditText)findViewById(R.id.EgyptiansET);
             convert=(Button) findViewById(R.id.button1);
             clear=(Button) findViewById(R.id.button2);
        }
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch(v.getId())
            {
            case R.id.button1:
                Toast.makeText(this,"hhh",Toast.LENGTH_LONG).show();
                break;

            case R.id.button2:
                Toast.makeText(this,"ygygy",Toast.LENGTH_LONG).show();
            }
        }
    }

Upvotes: 0

Views: 289

Answers (2)

Andrew Mackenzie
Andrew Mackenzie

Reputation: 5737

You can specify the name of the onClickListener method in your XML layout file as a property of the Button.

In this example as written, you could define "onClick" as being the method for both buttons.

Upvotes: 2

MByD
MByD

Reputation: 137312

You need to register the listener:

clear.setOnClickListener(this);
convert.setOnClickListener(this);

Upvotes: 4

Related Questions