Recognize keystrokes in Android Soft Keyboard using Kotlin - android

I am a student and I am currently building an Android application with Kotlin as the programming language. The application's aim will be to register keystrokes from users which I will use for biometric research purpose. I am not proficient in Kotlin or Android Studio as this is the first time I am working in Android Studio.
The application has many activities and I am sharing the code from one of the activities, where I need to recognize the keystrokes in Editexts, while the user presses keys in soft keyboard to type username and password.
I found some Android Developers examples about KeyEvents and some References.
The data I need from the keystrokes are as follows:
a. the key pressed.
b. key pressed time.
c. key released time.
d. its ASCII value.
The XML Code is as follows:
<Button
android:id="#+id/button5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:background="#drawable/rounded_button"
android:fontFamily="#font/pt_mono"
android:onClick="resetval"
android:text="#string/reset"
android:textAlignment="center"
android:textColor="#android:color/background_light"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/button4"
app:layout_constraintStart_toStartOf="#+id/button4"
app:layout_constraintTop_toBottomOf="#+id/button4"
app:layout_constraintVertical_bias="0.007" />
<Button
android:id="#+id/button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:background="#drawable/rounded_button"
android:fontFamily="#font/pt_mono"
android:onClick="input"
android:text="#string/inp"
android:textAlignment="center"
android:textColor="#android:color/background_light"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="#+id/editText9"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="#+id/editText9"
app:layout_constraintTop_toBottomOf="#+id/editText9" />
<EditText
android:id="#+id/editText8"
android:imeOptions="normal"
android:layout_width="320dp"
android:layout_height="28dp"
android:layout_marginTop="72dp"
android:autofillHints=""
android:background="#drawable/rounded_text"
android:ems="10"
android:fontFamily="#font/pt_mono"
android:hint="#string/usrname"
android:inputType="textEmailAddress"
android:paddingLeft="10dp"
android:textAlignment="textStart"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="#+id/textView21"
app:layout_constraintHorizontal_bias="0.494"
app:layout_constraintStart_toStartOf="#+id/textView21"
app:layout_constraintTop_toBottomOf="#+id/textView21" />
<EditText
android:id="#+id/editText9"
android:layout_width="0dp"
android:layout_height="28dp"
android:layout_marginTop="8dp"
android:autofillHints=""
android:background="#drawable/rounded_text"
android:paddingLeft="10dp"
android:ems="10"
android:fontFamily="#font/pt_mono"
android:hint="#string/pass"
android:inputType="textPassword"
android:textAlignment="textStart"
android:textSize="14sp"
app:layout_constraintEnd_toEndOf="#+id/editText8"
app:layout_constraintStart_toStartOf="#+id/editText8"
app:layout_constraintTop_toBottomOf="#+id/editText8" />
<TextView
android:id="#+id/textView21"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:fontFamily="#font/pt_mono"
android:text="#string/train_tem"
android:textAlignment="center"
android:textColor="#color/btext"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
The Kotlin code is as follows:
class trainingT : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
supportActionBar?.title = "Training Template"
setContentView(R.layout.activity_training_t)
//getuserkeystroke()
}
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
return when (keyCode) {
KeyEvent.KEYCODE_D -> {
getuserkeystroke(keyCode)
true
}
else -> super.onKeyDown(keyCode, event)
}
}
private fun getuserkeystroke(keyCode: Int){
Log.d("D should be displayed", "Is it displayed?")//keyCode.toString())
val toast = Toast.makeText(getApplicationContext(), "D is Pressed", Toast.LENGTH_LONG)
val layout = toast.getView() as LinearLayout
if (layout.childCount > 0) {
val tv = layout.getChildAt(0) as TextView
tv.gravity = Gravity.CENTER_VERTICAL or Gravity.CENTER_HORIZONTAL
}
toast.show()
}}
So far I am stuck on point number "a". I am missing something I know that, as the key pressed has to be recognized inside the Editext space, but I have no idea how to. The Kotlin code should call the getuserkeystroke() function as soon as "d key" (I was thinking of recognizing at least a single key and after that random keys) is pressed, display a debug message in the log and should show a toast but its not working.
Please suggest how should I proceed in order to recognize the keys pressed in the Android soft keyboard when typing in real time? And also some suggestions on how to acquire the data I mentioned in points "b", "c" and "d".
The activity screenshot is as follows: Screenshot

Related

how do I add padding in EditText hints

I have made a login page and in it there are two fields, one for username and one for password, and
I can't seem to figure out a way to add padding to hints so the hints in the field look like this
and if I add the android:paddingTop="30dp"
it looks like this
this EditText is using a background source (field_bg) and also a user icon which changes its colors when the field is selected (custom_username_field).
here is the xml code for the username field.
<EditText
android:id="#+id/usernameField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autofillHints=""
android:background="#drawable/field_bg"
android:cursorVisible="false"
android:drawableStart="#drawable/custom_username_field"
android:drawablePadding="-30dp"
android:ems="10"
android:fontFamily="#font/staatliches"
android:hint="USERNAME"
android:includeFontPadding="true"
android:inputType="textPersonName"
android:textAlignment="center"
android:textColor="#6E6E6E"
android:textSize="20sp"
android:paddingTop="30dp"
app:boxStrokeWidth="#null"
app:boxStrokeWidthFocused="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.507"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.421"
tools:ignore="TouchTargetSizeCheck" />
there is a poor solution to this problem:
android:textAlignment="center"
android:gravity="top"
tools:ignore="RtlCompat"
But in this case, the text that the user will enter will replace the hint and will be at the very top, which will not look nice. It would be possible to change the position of the text after the user has entered the text but for this you need to switch to code, not XML.
I also suspect that you plan to leave a prompt even after the user enters the text.
create hint in string.xml and then use in view
<EditText android:hint="#string/yourhint" />
You can try like this.
<EditText
android:id="#+id/usernameField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/round_10_background3"
android:drawableStart="#drawable/aa"
android:drawableTint="#color/white"
android:ems="10"
android:fontFamily="#font/caveat_semi_bold"
android:hint="USERNAME"
android:inputType="textPersonName"
android:padding="20dp"
android:textAlignment="center"
android:textColor="#color/white"
android:textColorHint="#color/white"
android:textSize="20sp"
app:boxStrokeWidth="#null"
app:boxStrokeWidthFocused="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.507"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.421"
tools:ignore="TouchTargetSizeCheck" />

Add Hide/Show EditText

Let me try to explain the title.
I have a manufacturer TextView that is populated from MYSQL. When a manufacturer is selected it populates the Model TextView.
Now I want to add OTHER in each model, and when it is selected an EditText appears. Something like this:
-FORD
---Mustang
---Escape
---Other
-BMW
---X5
---Z4
---Other
Now whenever Other is selected, the EditText appears. If not, then it remains hidden.
The only way I got it working is by creating a field in mysql for each model. I am hoping there is a better approach.
Here is what I have done.
enter code here
<EditText
android:id="#+id/fordTextView"
android:layout_width="0dp"
android:layout_height="130px"
android:layout_marginStart="16dp"
android:visibility="gone"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:background="#drawable/custom_progress_bar_horizontal"
android:inputType="textPersonName"
android:paddingStart="#dimen/space_12"
android:hint="#string/item_entry__model"
android:paddingLeft="#dimen/space_12"
android:paddingTop="8dp"
android:paddingEnd="#dimen/space_12"
android:paddingRight="#dimen/space_12"
android:paddingBottom="8dp"
android:textAlignment="viewStart"
android:textColor="#color/text__primary"
android:textSize="14sp"
app:font='#{"normal"}'
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/plateNumTextView" />
<EditText
android:id="#+id/bmwTextView"
android:layout_width="0dp"
android:layout_height="130px"
android:layout_marginStart="16dp"
android:visibility="gone"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:background="#drawable/custom_progress_bar_horizontal"
android:inputType="textPersonName"
android:paddingStart="#dimen/space_12"
android:hint="#string/item_entry__model"
android:paddingLeft="#dimen/space_12"
android:paddingTop="8dp"
android:paddingEnd="#dimen/space_12"
android:paddingRight="#dimen/space_12"
android:paddingBottom="8dp"
android:textAlignment="viewStart"
android:textColor="#color/text__primary"
android:textSize="14sp"
app:font='#{"normal"}'
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/fordTextView" />
Here is is the java
if (itemViewModel.holder.model_id.equals(Constants.MODEL_FORD_OTHER)){
binding.get().fordTextView.setVisibility(View.VISIBLE);
binding.get().bmwTextView.setVisibility(View.GONE);
}
if (!itemViewModel.holder.model_id.equals(Constants.MODEL_FORD_OTHER)){
binding.get().fordTextView.setVisibility(View.GONE);
binding.get().fordTextView.setText("");
}
if (itemViewModel.holder.model_id.equals(Constants.MODEL_BMW_OTHER)){
binding.get().bmwTextView.setVisibility(View.VISIBLE);
binding.get().fordTextView.setVisibility(View.GONE);
}
if (!itemViewModel.holder.model_id.equals(Constants.MODEL_BMW_OTHER)){
binding.get().bmwTextView.setVisibility(View.GONE);
binding.get().bmwTextView.setText("");
}
Is there a way of doing this by using only one EditText?
You can achieve this using single EditText, when ever click is registered for particular model Other button, set the model_id of that model as tag to the EditText. And while reading input from EditText, always check for tag to get the model_id
binding.get().modelTextView.setTag(""+itemViewModel.holder.model_id);

Two-way databinding screen orentation change changes value

I'm using two-way databinding.
I have some data inside my Room databse and depending on what user writes in edittext showing that value in textview.
When I rotate screen, value gets updated. At fist its empty string then back to how it was. I know this is normal behavior of activity lifecycle.
So I would like to know the best way to get rid of this problem.
Thanks in advance.
EditText:
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/edtOddelek"
android:text="#={viewModel.loginOddelek}"
android:layout_marginEnd="8dp" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/textView3"
app:layout_constraintBottom_toBottomOf="#+id/textView3"
app:layout_constraintStart_toStartOf="#+id/edtGeslo" style="#style/adoEditTextStyle"/>
TextView:
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/txtOddelek"
android:layout_marginTop="8dp"
android:text="#{viewModel.oddelek}"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="12dp"
android:textColor="#color/orangeDark"
app:layout_constraintTop_toBottomOf="#+id/edtOddelek"
app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="12dp"/>
ViewModel:
val loginOddelek = NonNullMutableLiveData<String>("")
val oddelek:LiveData<String> = Transformations.switchMap(loginOddelek){
settingsModel.getSifrPr(it)
}

Creating button in android studio using kotlin

This is my button in activity_main.xml
<Button
android:text="#string/fs"
android:layout_width="154dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/textView"
android:layout_marginEnd="12dp"
android:layout_marginRight="12dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp" android:layout_marginBottom="32dp"
app:layout_constraintBottom_toTopOf="#+id/imageView"
app:layout_constraintHorizontal_bias="0.451"
app:layout_constraintVertical_bias="0.069" android:id="#+id/button2"
style="#style/Widget.AppCompat.Button"
android:background="#android:color/holo_green_dark"
android:onClick="flowerpage"/>
This is the button being created I believe in the Mainactivity.kt
fun flowerpage(view: activity2) {
}
I am new to Kotlin, however, I used to HTML where you could connect two web pages together via HTML link, however, this doesn't seem to be as simple.
button2.setOnClickListener {flower_button()}
this shows compiler error. am I missing an import..??
With kotlin you just need to give your button an id in the XML, then you can do this:
btn_id_you_gave.setOnClickListener {doSomething()}
private fun doSomething() {...}
You don't need to do the OnClick thing in the XML you want to take max advantage of Kotlin.

Adding number in Android using Kotlin

I am trying to add numbers with 3 EditTexts and I want to display it on text view with a calculate button but there's something wrong with Kotlin code as I'm newbie
Here's my code:
class Add : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_adsense)
var input_num1 = num1
var input_num2 = numm2
var input_num3 = num3
result.setOnCLickListener {
var result = input_num1.toStrubg()?.toLong() + input_num2.toStrubg()?.toLong() + input_num3.toLong()?.toString()
}
}
}
<TextView
android:id="#+id/textView"
android:textSize="20dp" />
<EditText
android:id="#+id/num1"
android:inputType="number" />
<TextView
android:id="#+id/textView2"
android:textSize="20dp" />
<EditText
android:id="#+id/num2"
android:inputType="numberDecimal" />
<TextView
android:id="#+id/textView3"
android:textSize="20dp" />
<EditText
android:id="#+id/num3"
android:inputType="numberDecimal" />
<TextView
android:id="#+id/result"
android:textSize="20dp"
android:text="Result" />
<Button
android:id="#+id/Calculate"
android:onClick="Calculate"
android:text="Calculate"
android:textSize="20dp" />
You say you are a newbie but that doesn't mean you will have to ignore the compiler or IDE errors. Clearly toStrubg() is not a keyword and your IDE will make you aware of that. Don't know what the rest of your code looks like so will help you with the result.setOnClickListener.
Try this
result.setOnClickListener {
var result = input_num1.toString()?.toLong() + input_num2.toString()?.toLong() + input_num3.toString()?.toLong()
}
Also I'll advise that you should try and learn Kotlin first before embarking on your Android development journey. Here are some links to help you.
Docs
try

Categories

Resources