I have built a delete button in Android, just like in Windows, which removes the string on the right hand side of the cursor (one by one). (Instead of the mainstream Backspace which removes the left side.)

Tell me if you have any better ideas.

package com.example.app;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView.BufferType;
import android.widget.Toast;

public class MainActivity extends Activity {
    public Toast t, t1,t2;
    Editable a, b, d, f, a1;
    String e;
    public int c, d1;

    protected void onCreate(Bundle savedInstanceState) {

        final EditText ed;

        Button dlt;

        dlt = (Button) findViewById(R.id.button1);

        ed = (EditText) findViewById(R.id.editText1);

        dlt.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                boolean check = mgr.isActive(ed);

                    Toast.makeText(getApplicationContext(), "Welcome", t2.LENGTH_SHORT)
                a = ed.getEditableText();

                b = a;
                c = ed.getSelectionStart();

                String a11 = b.toString().substring(c);
                Toast.makeText(getApplicationContext(), a11, t1.LENGTH_SHORT)
                String a22 = b.toString().substring(0, c);

                boolean daj = b.toString().isEmpty();
                if (a11 != null && !a11.trim().equals("")) {

                    int strChar = a11.length();
                    String strcut = a11.substring(1, strChar);
                    e = a22.concat(strcut);

                    ed.setText(e, BufferType.EDITABLE);
                    b = ed.getEditableText();
                } else {
                    Toast.makeText(getApplicationContext(), "Cool",




    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;


1 Answer 1

  1. Name your variables and fields meaningfully. I have no idea what a,b,e, etc are.

  2. Don't split your variable declaration and initalization. Instead of

    final EditText ed;
    Button dlt;
    dlt = (Button) findViewById(R.id.button1);
    ed = (EditText) findViewById(R.id.editText1);


    final EditText ed = (EditText) findViewById(R.id.editText1);    
    final Button dlt = (Button) findViewById(R.id.button1);

    And be consistent with your final usage. Either use it everywhere where appropriate or don't. Otherwise I start to wonder why ed is final and dlt is not.

  3. There a TextUtils.isEmpty() method available on Android. Use it instead of

    a11 != null && !a11.trim().equals("")
  4. Use static field access. Instead of t1.LENGTH_SHORT use Toast.LENGTH_SHORT. All the fields t,t1 and t2 are unnecessary.

  5. All your fields should be local variables.

  • \$\begingroup\$ Thanks for your response. I'll surely keep your suggestions i mind the next time I write a code snippet. \$\endgroup\$
    – code
    Jun 7, 2014 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.