Creating button in android studio using kotlin - android

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.

Related

Cleaner way to repeat imageview, textview, and checkbox list in Kotlin android studio?

I'm basically creating a todolist app in android studio with kotlin that has this structure on repeat throughout the entire page. The button is for adding more items to your to do list. So to make things cleaner I'd like to make a simple class object everytime that button is pressed and add it to the layout. I'm fairly new to Kotlin and android and have no idea the best way to do this and don't want to just hardcode a new layout everytime a button is pressed. Any help would be greatly appreciated!
<LinearLayout>
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/weekly_button_1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
android:background="#drawable/plus_sign" />
<TextView
android:id="#+id/monthly"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="#string/monthly"
android:textSize="40dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<CheckBox
android:id="#+id/checkbox_1"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:layout_toRightOf="#id/daily_item_1"
android:textSize="16sp" />
</LinearLayout>
you can checkout the below codelabs example.
https://developer.android.com/codelabs/kotlin-android-training-diffutil-databinding#4

Couldn't enable button using xml and data binding

I'm want to enable button when repeatPasswordInputEditText is not empty, I tried to enable it with android:enabled="#{!repeatPasswordInputEditText.text.toString().isEmpty()}" but it doesn't work, why ? I'm also calling binding.lifecycleOwner = this in onCreateView
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/uRepeatPassword"
android:layout_width="384dp"
android:layout_height="75dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:hint="#string/repeat_password_hint"
app:passwordToggleEnabled="true"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/uNewPassword">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/repeatPasswordInputEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:text="#{passwordChangeViewModel._repeatPassword}"
android:background="#color/white"
app:passwordToggleEnabled="false"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.button.MaterialButton
android:id="#+id/changePasswordButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Change Password"
android:enabled="#{!repeatPasswordInputEditText.text.toString().isEmpty()}"
app:layout_constraintTop_toBottomOf="#+id/uRepeatPassword">
</com.google.android.material.button.MaterialButton>
You cannot listen to changes in EditText text inside the XML itself.
In your code, you have
android:text="#{passwordChangeViewModel._repeatPassword}"
Seems like you are trying to use two-way data-binding here where _repeatPassword is a MutableLiveData<String>. This won't work because it is just one-way right now. You need to replace # with #= to make it two way.
Now that you have two-way data binding working, you can use the value of this live data to enable/disable your button:
android:enabled="#{!_repeatPassword.empty}"
If you are not using two-way data binding, you will have to put the logic in your Activity/Fragment:
repeatPasswordInputEditText.doAfterTextChanged { text ->
changePasswordButton.enabled = text.isNotEmpty()
}

Why does Dialog not show PDFView?

I want to show a PDF in a custom Dialog. I fetch PDF from a service as a ByteArray. I pass the ByteArray to PDFView object and then inflate the Dialog. All the views in the dialog are visible except the pdfview.
Here is my activity code:
val fileName = "filename.pdf"
val dialog = Dialog(this)
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
dialog.setCancelable(true)
dialog.setContentView(R.layout.activity_pdf_view)
val pdfView = dialog.findViewById(R.id.pdfView) as PDFView
val closeBtn = dialog.findViewById(R.id.closeBtn) as ImageButton
val downloadBtn = dialog.findViewById(R.id.downloadBtn) as Button
pdfView.fromBytes(byteArray).load()
dialog.show()
Below is the xml activity_pdf_view.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/transparent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/ticketLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="#drawable/bg_ticket_layout">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="Ticket"
android:textSize="15sp"
android:textColor="#color/black"
android:layout_margin="25dp"/>
<com.github.barteksc.pdfviewer.PDFView
android:id="#+id/pdfView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toBottomOf="#id/title"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginTop="39dp"
android:layout_marginBottom="8dp"
android:layout_marginHorizontal="8dp"/>
<ImageButton
android:id="#+id/closeBtn"
android:layout_width="11dp"
android:layout_height="11dp"
android:background="#color/white"
android:contentDescription="#string/close"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_cancel_icon"
android:layout_marginTop="20dp"
android:layout_marginEnd="24dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
<Button
android:id="#+id/downloadBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/submit_btn_bg"
android:drawableStart="#drawable/ic_file_download_white"
android:text="#string/download_pdf"
android:textColor="#color/white"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="#id/ticketLayout"
app:layout_constraintStart_toStartOf="#id/ticketLayout"
app:layout_constraintTop_toBottomOf="#id/ticketLayout"
android:paddingHorizontal="19dp"
android:drawablePadding="10dp"
android:layout_marginTop="15dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
This is how the view looks:
I debugged the code and confirmed that byteArray is not empty. All the other views like the close button and download button function properly as expected. Only the pdfview doesn't show anything.
I had tried many things first like AlertDialog.Builder, an activity with Dialog theme and many more ways and finally broke the code down to the simplest possible way. Still no change in view.
Please help me find out the issue here. Thanks a lot in advance :)
Update:
The issue is that if I use wrap_content for parent ConstraintLayout, it shows this small view as in screenshot. But when I use match_parent, it shows PDFView correctly (but stretched throughout the screen that looks ugly though). So what I did is hardcoded the height of parent constraint layout. I did some digging on the internet and found that the PDF inside the PDFView is not loaded when the parent ConstraintLayout draws its child layouts. So does anyone know any way where I can redraw the layout after all data is loaded?
As we all know Android Deprecated to show PDF via webview here you can find useful link. https://github.com/eduxcellence/PDFViewer/find/master

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

Recognize keystrokes in Android Soft Keyboard using Kotlin

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

Categories

Resources