Search code examples
androidandroid-edittextimeoptions

Android ime options not working


I am trying to set up a listener for the android ime options, that stores the value of the EditText in the shared preferences. I have it set up like so, but when I press the "return" key on my keyboard, nothing happens and it never goes into the listener. Any ideas on why I'm doing wrong?

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

    mDone = (Button) findViewById(R.id.done);
    mTemperature = (EditText) findViewById(R.id.temperature);

    mDone.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

            int action = event.getAction();
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                // Do whatever you want here
                SharedPreferences preferences = getSharedPreferences(SHARED_PREFERENCES, Context.MODE_PRIVATE);
                SharedPreferences.Editor edit = preferences.edit();

                int updateSweater = Integer.parseInt(mTemperature.getText().toString());
                edit.remove("sweater");
                edit.putInt("sweater", updateSweater);
                edit.commit();
                preferences.getInt("sweater", 0);

                Toast.makeText(SettingsActivity.this, "Sweater Weather Updated", Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(SettingsActivity.this, MainActivity.class);
                startActivity(intent);
                return true;
            }
            return false;
        }
    });
}

This is how I set up my EditText

<EditText
    android:id="@+id/temperature"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="@color/white"
    android:textColorHint="@color/gray"
    android:textSize="25sp"
    android:layout_centerHorizontal="true"
    android:hint="Farenheit"
    android:inputType="phone"
    android:imeOptions="actionDone"/>

Solution

  • I believe it is not working because setOnEditorActionListener should be defined to your EditText (and not to your Button).

    mTemperature = (EditText) findViewById(R.id.temperature);
    
    mTemperature.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    
            int action = event.getAction();
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                // Do whatever you want here
                SharedPreferences preferences = getSharedPreferences(SHARED_PREFERENCES, Context.MODE_PRIVATE);
                SharedPreferences.Editor edit = preferences.edit();
    
                int updateSweater = Integer.parseInt(mTemperature.getText().toString());
                edit.remove("sweater");
                edit.putInt("sweater", updateSweater);
                edit.commit();
                preferences.getInt("sweater", 0);
    
                Toast.makeText(SettingsActivity.this, "Sweater Weather Updated", Toast.LENGTH_SHORT).show();
    
                Intent intent = new Intent(SettingsActivity.this, MainActivity.class);
                startActivity(intent);
                return true;
            }
            return false;
        }
    });
    

    If you need to take any action after click in mDone, it is better to set a ClickListener to mDone