I am trying to get Enter key whenever user pressed.
I tried many codes but neither in emulator nor in device I could get the Enter keypress.
Here is my code :
public class MainActivity extends AppCompatActivity implements View.OnKeyListener {
String barkodsifre;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText sifre = (EditText) findViewById(R.id.barkodsifre);
sifre.setOnKeyListener(this);
}
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
Log.i("a","b");
return true;
}
return false;
}
}
There is only 1 edittext and a textview in xml. Data will be entered to edittext by a barcode scanner. Barcode scanner will send "Enter" at the end of the scanned barcode. I send 'enter' from barcode scanner at the end of the barcode but somehow it never handles enter key.
Is something missed in code or the code is totaly wrong? How can I handle "enter" key which comes from a barcode scanner.
Here is my xml :
<RelativeLayout
android:layout_width="368dp"
android:layout_height="495dp"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp">
<TextView
android:id="@+id/lblbarkod"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:ems="10"
android:inputType="none"
android:text="Barkod Giriniz!"
android:textAlignment="center"
android:textSize="30sp"
android:textStyle="bold" />
<EditText
android:id="@+id/barkodsifre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/lblbarkod"
android:layout_alignLeft="@+id/lblbarkod"
android:layout_alignRight="@+id/lblbarkod"
android:layout_alignStart="@+id/lblbarkod"
android:layout_below="@+id/lblbarkod"
android:imeOptions="actionGo|flagNoFullscreen"
android:inputType="textPassword"
android:maxLines="1"
android:ems="10" />
</RelativeLayout>