Categories
Android

Double <enter-key>s in EditTexts

This Example responds to double <enter-key>s. In addition to the three Android classes used in the previous Example, it uses a MotionEvent.

EmptyEnter.java

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.inputmethod.EditorInfo;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

public class EmptyEnter extends Activity {
  EditText editText;
  boolean lastCharWasEnter;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    editText=(EditText)findViewById(R.id.editText);
    lastCharWasEnter=false;
    
    editText.setOnTouchListener(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View view, MotionEvent event) {
        lastCharWasEnter=false;
        return false;
      }
    });
    
    editText.addTextChangedListener(new TextWatcher() {
      public void beforeTextChanged
      (CharSequence s, int start, int count, int after) {
      }
      public void onTextChanged
      (CharSequence s, int start, int before, int count) {
        //check if exactly one char was entered and it was an 
        if (before==0 && count==1 && s.charAt(start)=='\n') {
          lastCharWasEnter=!lastCharWasEnter;
          if (lastCharWasEnter) return;
          Integer startInteger=new Integer(start);
          Intent intent=new Intent("enter");
          intent.putExtra("Start", startInteger.toString());
          mySendBroadcast(intent);
        }
        else lastCharWasEnter=false;
      }
      public void afterTextChanged(Editable s) {
      }
    });
  }

  private void mySendBroadcast(Intent intent) {
    //Can't instantiate LocalBroadcastMananager in Anonymous context
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
  }
  
  @Override
  public void onResume() {
    super.onResume();
    // Register messageReceiver to receive messages.
    LocalBroadcastManager.getInstance(this).registerReceiver
    (startReceiver, new IntentFilter("enter"));
  }

  // handler for received Intents for the "enter" event 
  private BroadcastReceiver startReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
      // Extract data included in the Intent
      int start=Integer.parseInt(intent.getStringExtra("Start"));
      //replace the <enter>s
      editText.getText().replace(start-1, start+1,"");
      //respond to the double <enter-key>
      editText.getText().replace(start-1, start-1,"<>");
    }
  };
  
  @Override
  protected void onPause() {
    // Unregister since the activity is not visible
    LocalBroadcastManager.getInstance(this).unregisterReceiver(startReceiver);
    super.onPause();
  }
}
Categories
Android

New Way to Handle <enter-key>s in Edittexts

A dependable way to respond to the <enter-key> in an EditText is with a TextWatcher, a LocalBroadcastManager, and a BroadcastReceiver. I think you need to add the v4 support library to use the LocalBroadcastManager. I use the tutorial at vogella.com: 7.3 “Local broadcast events with LocalBroadcastManager” because of its complete concise code Example. In onTextChanged before is <the index of the end of the change before the change>minus start. When in the TextWatcher the UI thread is busy updating editText’s editable, so we send an Intent to wake up the BroadcastReceiver when the UI thread is done updating editText.

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.Editable;
in onCreate:
editText.addTextChangedListener(new TextWatcher() {
  public void onTextChanged
  (CharSequence s, int start, int before, int count) {
    //check if exactly one char was added and it was an <enter>
    if (before==0 && count==1 && s.charAt(start)=='\n') {
    Intent intent=new Intent("enter")
    Integer startInteger=new Integer(start);
    intent.putExtra("Start", startInteger.toString()); // Add data
    mySendBroadcast(intent);
in the BroadcastReceiver's onReceive:
int start=Integer.parseInt(intent.getStringExtra("Start"));
editText.getText().replace(start, start+1,""); //remove the <enter>
//respond to the <enter> here
Categories
Android

Updated the downloads

I took some time out to update the downloads in Android Questions, using the things I had learned while developing abicus, and I posted an excerpt from a comment on my blog’s EditText Enter Key post on stackoverflow.

Categories
abicus Android CoCo

SubVersion

My son added a subversion repository to my site. I had added number functions, string functions, power, compares, stop, and end. I got run, goto, then, and else working and plunged on into on i goto a,b,c without committing run, goto, then, and else. The working version crashed. I couldn’t get it to compile no matter what I tried. I checked out the last revision that I had committed and added run, goto, then, and else. Needless to say, I committed them before moving on. Now I commit regularly.

Categories
Android CoCo

Arithmetic Added

My Android coco app now does arithmetic. Parens, add, sub, unary minus, times, divide, or, and, not. Now on to the string functions.

Categories
Android CoCo

Simple Editor Done

My simple line oriented editor with a full screen display now inserts and deletes lines and moves up and down in the file. Now to work on my interpreter. So far it does END STOP and PRINT. Next up GOTO. Already an empty enter switches back and forth between editing and direct mode.

Categories
Android CoCo

Rewrote Translate

I completely rewrote translate. No more SQLite and no more String manipulations other than FileStream and substring. My benchmarks are completely out of this world. Now off to the user editor.

Categories
Android PC's

Stack Overvflow

I took some time out to answer a couple of questions on Stack Overflow. I have used it extensively to find out information about the Android. This is the first time I have contributed. I got a couple of plus votes for one of my answers. I was so excited. I now have a reputation of 21. It’s not much, but I’m sill very proud.

Categories
Android CoCo

Translate is Done

I’m having a lot of fun working on my Coco simulator for the Android. Now that the translate phase is done, I am moving on to the editing phase.

Categories
Android CoCo

Literals for Coco for the Android

Coco for the Android now translates symbols, keywords, number variables, string variables, number literals, string literals, and arrays. Now that everyone is up to date, back to work.