HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Delete (Reverse Backspace) Button

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
buttonreversedeletebackspace

Problem

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;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText ed;

Button dlt;

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

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

dlt.setOnClickListener(new OnClickListener() {
@SuppressLint("NewApi")
public void onClick(View v) {

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

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

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

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

Solution

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

-
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);


do

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.

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

a11 != null && !a11.trim().equals("")


-
Use static field access. Instead of t1.LENGTH_SHORT use Toast.LENGTH_SHORT. All the fields t,t1 and t2 are unnecessary.

-
All your fields should be local variables.

Code Snippets

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);
a11 != null && !a11.trim().equals("")

Context

StackExchange Code Review Q#52585, answer score: 7

Revisions (0)

No revisions yet.