0

I honestly have no idea what's happening but none of the events listed below are hit while debugging, how is this even possible? I don't know what other information to provide that would be useful in this scenario.

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MarkerActivity extends AppCompatActivity {

    private TextView titleTv, hashtagTv, dateTv, colorTv;
    private EditText test_edit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_marker);

        titleTv = (TextView) findViewById(R.id.title_info);
        hashtagTv = (TextView) findViewById(R.id.hashtag_info);
        dateTv = (TextView) findViewById(R.id.date_info);
        colorTv = (TextView) findViewById(R.id.color_info);
        test_edit = (EditText) findViewById(R.id.test_edit);

        test_edit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (event.getAction() == KeyEvent.KEYCODE_SPACE) {
                    test_edit.setText(test_edit.getText().toString() + " #");
                    return true;
                }
                return false;
            }
        });

        test_edit.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.KEYCODE_SPACE) {
                    test_edit.setText(test_edit.getText().toString() + " #");
                    return true;
                }
                return false;
            }
        });

        test_edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        Bundle bundle = getIntent().getExtras();
        if (bundle != null && bundle.containsKey("marker_title")) {
            String marker_title = bundle.getString("marker_title");
            if (marker_title != null) {
                MarkerModel markerModel = DatabaseHelper.get().getMarker(marker_title);

                titleTv.setText("Title: " + markerModel.getTitle());
                dateTv.setText("Date: " + markerModel.getDay() + "/" + markerModel.getMonth() + "/" + markerModel.getYear());
                hashtagTv.setText("Hashtags: " + markerModel.getHashtags());
                colorTv.setText("Color: " + markerModel.getColor());

            }
        }
    }

}

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/color_info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="color" />

    <TextView
        android:id="@+id/date_info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="date" />

    <TextView
        android:id="@+id/title_info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="title" />

    <TextView
        android:id="@+id/hashtag_info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="hashtags" />

    <EditText
        android:id="@+id/test_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

</LinearLayout>
Alex
  • 108
  • 1
  • 11
  • Could you post your layout `activity_marker` code here ? – Henry Oct 22 '15 at 10:14
  • Indeed, you code looks fine. Maybe you are seting any parameter on the activity_marker layout to disable the capacity of your EditText. – Juan Pedro Martinez Oct 22 '15 at 10:16
  • Maybe is an incompability when you set all this events, try only with the TextWatcher, addTextChangedListener, and comment the other 2. It is weird. – Juan Pedro Martinez Oct 22 '15 at 10:19
  • I definitely tried it and doesn't change anything... also cleaned my project and built it again, sometimes code doesn't run – Alex Oct 22 '15 at 10:20
  • I am going to try your code, now I can't live without solve your problem haha – Juan Pedro Martinez Oct 22 '15 at 10:21
  • Alex, I've just tried your code and it is working for me perfectly. The method setOnEditorActionListener I don't know how trigger it, i think you can delete, but the textwatcher is fired everytime that i type a new letter. – Juan Pedro Martinez Oct 22 '15 at 10:31
  • well, I have no clue what's the issue. Should be code that's functional, definitely – Alex Oct 22 '15 at 10:31
  • Are you testing in an emulator or a real device ? Sometime i saw some issues with the emulator. – Juan Pedro Martinez Oct 22 '15 at 10:32
  • Both Method `setOnKeyListener` and `setOnEditorActionListener` also not working for me. What you want to do exactly? – Pratik Butani Oct 22 '15 at 10:42
  • Real device. It seems like there's a problem with Android Studio because I put some logs inside, they do appear. But the breakpoints are not hit... – Alex Oct 22 '15 at 10:48
  • see this comment: http://stackoverflow.com/questions/4886858/android-edittext-deletebackspace-key-event#comment9246143_4887323 – Pratik Butani Oct 22 '15 at 10:50

3 Answers3

0

You need to specify imeOption in edittext in xml before responding to action for setOnEditorActionListener().

<EditText
android:id="@+id/test_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content""
android:inputType="text"
android:imeOptions="actionSend" />

And just for notifying you for this kind of work textWatcher is a better option. That always works.

Zahan Safallwa
  • 3,880
  • 2
  • 25
  • 32
0

I set some Logs inside the TextWatcher and it actually displays it. Still the breakpoints are not hit, seems like it's an issue with Android Studio.. but what could it be? Restarted the IDE, didn't do the trick

Alex
  • 108
  • 1
  • 11
0

I think you have to do like:

test_edit.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {  }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {  }

    @Override
    public void afterTextChanged(Editable s) {
        if(s.toString().endsWith(" "))
        {
            test_edit.setText(test_edit.getText().toString() + " #");
            test_edit.setSelection(test_edit.length());
        }
    }
});

Keep other two listener in Comment. Try It.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437