0

I have set an event listener for two buttons and event listener for a EditText control in my java code:

public void doNewButtonClick( View view ) {
    View.OnClickListener onSnap = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            TextView calculatorTextView = (TextView) findViewById(R.id.textView);
            calculatorTextView.setText( "GO TO SLEEP" );
            Log.d(TAG, "In the Handler for NEW BUTTONS");

        }
    };
    Button button1 = (Button) findViewById(R.id.buttonA);
    Button button2 = (Button) findViewById(R.id.buttonB);


    button1.setOnClickListener( onSnap );
    button2.setOnClickListener( onSnap );

}

public void onEditTextInput( View view ){
    EditText myMessage = (EditText) findViewById(R.id.userText);
    myMessage.setOnEditorActionListener(new OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int actionID, KeyEvent event) {
            Log.d(TAG, "BEFORE THE SEND");
            EditText userMessage = (EditText) textView;
            TextView phone = (TextView)findViewById(R.id.textView2);

            if( actionID == EditorInfo.IME_ACTION_GO){
                Log.d(TAG, "IN THE SEND");
                phone.setText( userMessage.getText() );
            }
            return false;
        }
    });
}

This is my xml:

 <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/buttonA"
        android:layout_below="@+id/button6"
        android:layout_toLeftOf="@+id/button6"
        android:layout_toStartOf="@+id/button6"
        android:layout_marginTop="119dp"
        android:drawableLeft="@drawable/abc_ic_star_black_16dp" />

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/buttonB"
        android:layout_alignBottom="@+id/buttonA"
        android:layout_alignRight="@+id/button6"
        android:layout_alignEnd="@+id/button6" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/userText"
        android:layout_above="@+id/buttonB"
        android:layout_alignLeft="@+id/buttonA"
        android:layout_alignStart="@+id/buttonA"
        android:imeOptions="actionGo"
        android:inputType="text"
        android:layout_alignRight="@+id/buttonB"
        android:layout_alignEnd="@+id/buttonB" />

I have two issues: 1) The event handlers don't work unless I also go into the control's properties and set the OnClick property to the desired method. In other words, I'll click a control many times and nothing will happen unless I set the OnClick property to a function, then the controls and their event handlers work as expected.

2) Setting the OnClick property of each control to the desired event handler( callback function) forces me to have to click the control twice for it to work. This happens since I am setting the OnClick property and also defining the OnClick function in the event handler.

Why are my event handlers not responding? Why do I have to set the OnClick property? Am I forced to set the OnClick property and not use the Anonymous class method to make event handlers?

Thank you!!

Naveen
  • 830
  • 9
  • 19
Jay5
  • 3
  • 5
  • Possible duplicate of [How to handle button clicks using the XML onClick within Fragments](http://stackoverflow.com/questions/6091194/how-to-handle-button-clicks-using-the-xml-onclick-within-fragments) – SaravInfern Jul 23 '16 at 07:38

1 Answers1

0

You can simple use onClick property on button as shown below

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/buttonB"
    android:onClick="doNewButtonClick"   //<--- Like this
    android:layout_alignBottom="@+id/buttonA"
    android:layout_alignRight="@+id/button6"
    android:layout_alignEnd="@+id/button6" />

And your method will be

public void doNewButtonClick(View v){
   // you can handle different views based on the 
   //'id' you get v.getId() and handle different functionalities for each button.
}

Or simply

Button btn = (Button) findViewById(R.id.mybutton);

btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // do anything
    }
});

You can use either one of them.

Naveen
  • 830
  • 9
  • 19