-2

How do i make a button clickable and may you define me how the code works also. Thanks!

public void btnConvert(View view)
{
    EditText myVal = (EditText) findViewByID(R.id.ev);
    String x = myVal.getText().toString();

    Spinner myFrom = (Spinner)findViewById(R.id.cboFrom);
    String fr = myFrom.getText().toString();

    Spinner myTo = (Spinner)findViewById(R.id.cboTo);
    String to = myTo.getText().toString();

    int frVal = Integer.parseInt(x);
    int toVal = 0;

    if(fr.equals("Meters") && to.equals("Kilometers"))
    {
        toVal = frVal *0.001;
    }
}
justyeah
  • 3
  • 3
  • Here, this link explains it all perfectly http://stackoverflow.com/a/4153842/2844724 – Brian Stacks Mar 23 '15 at 15:40
  • 1
    Did you even search before asking? – timr Mar 23 '15 at 17:13
  • possible duplicate of [How exactly does the android:onClick XML attribute differ from setOnClickListener?](http://stackoverflow.com/questions/4153517/how-exactly-does-the-androidonclick-xml-attribute-differ-from-setonclicklistene) – vrwim Mar 23 '15 at 18:35

1 Answers1

1
Button button = (Button)findViewById(R.id.buttonsIdInXml);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //do what you want here with the click
    }
});

XML

<?xml version="1.0" encoding="utf-8"?>
<Button android:id="@+id/buttonsIdInXml"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
Brian Stacks
  • 162
  • 1
  • 8