How to make android studio autofill proper parameters - android-studio

When implementing some interfaces, android studio autofills the methods with non descriptive parameters like below
override fun onDateSet(p0: TimePicker?, p1: Int, p2: Int,p3: Int) {
//code
}
What I expect
override fun onDateSet(view: TimePicker?, year: Int, month: Int, dayOfMonth:Int) {
TODO("Not yet implemented")
}
I'm not sure where to reset this.

Related

Android studio Kotlin OnClickListerner function

When I implemented the member in the class for the on click listener as shown below:
class QuizQuestionsActivity : AppCompatActivity(), View.OnClickListener {
I was given the option of implementing it as:
override fun onClick(p0: View?) {
I need it to be
override fun onClick(v: View?) {
can someone explain the difference and why I am not getting the option of v: View
Kotlin provide default parameter with alphabets and digits you can simply change it with your variable name like --
override fun onClick(v: View?) { }
override fun onClick(view: View?) { }
override fun onClick(myView: View?) { }
It's quit good and it's meaningful variable name that remember as long time. where P0 and P1 named variable is not like good to remember.
Hope You can understand what I mean to say.
Both of those functions are the same, it's just a different name for the View variable. Kotlin parameters are listed with the parameter name first, then the class name.
If you want p0 to be v instead, just change the parameter name to be v.

setCompoundDrawables not displaying icon

Currently,
I am implementing a Fragment to change the user password. To do so the user has to confirm his password. When both password's match I want to display an icon inside the EditText. to verify this while the user is typing I implemented the following function:
private fun EditText.afterTextChanged(afterTextChanged: (String) -> Unit) {
this.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
override fun afterTextChanged(editable: Editable?) {
afterTextChanged.invoke(editable.toString())
}
})
}
Doing so I can use editText.afterTextChanged{...} to compare the values of the two editTexts. When both values match I am currently trying to display the icon with the following code:
val icon = ResourcesCompat.getDrawable(
resources,
R.drawable.ic_baseline_check_circle_24,
null
)
icon?.setBounds(
0, 0,
icon.intrinsicWidth,
icon.intrinsicHeight
)
editText.setCompoundDrawablesWithIntrinsicBounds(null, null, icon, null)
Unfortuantly this is not working. I already tried using setCompoundDrawables instead of setCompoundDrawablesWithIntrinsicBounds but it makes no difference. Furthermore, I tried using R.drawable.ic_baseline_check_circle_24 directly in the function, but it is not working either.
Does somebody have an idea what's wrong with my implementation?
You need to overwrite the previous Drawable before setting the new Drawable.
The description of the Android document about the function is as follows:
Sets the Drawables (if any) to appear to the left of, above, to the right of, and below the text. Use null if you do not want a Drawable there. The Drawables' bounds will be set to their intrinsic bounds.
Calling this method will overwrite any Drawables previously set using setCompoundDrawablesRelative(Drawable, Drawable, Drawable, Drawable) or related methods.
So you need to first set Drawable to null and then try again.
val icon = ResourcesCompat.getDrawable(
resources,
R.drawable.ic_baseline_check_circle_24,
null
)
icon?.setBounds(
0, 0,
icon.intrinsicWidth,
icon.intrinsicHeight
)
editText.setCompoundDrawables(null, null, null, null) //add this line code
editText.setCompoundDrawablesWithIntrinsicBounds(null, null, icon, null)

RecyclerView(Kotlin): Adding an swipe to delete functionality on a ToDo app with data in SQL database

I am new to kotlin and android studio and currently I am trying to build an todo list applications with my own ideas. Its mostly done but I have to add edit and delete functionality to the tasks that user adds. The tasks that user adds are stored on the device using SQLiteDatabase. This is the base swipe to delete class that I wrote:
abstract class SwipeToDelete(context: Context, dragDir: Int, swipeDir: Int): ItemTouchHelper.SimpleCallback(dragDir, swipeDir) {
override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean {
return false
}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
TODO("Not yet implemented")
}
}
This is the delete functionality that I have added which works great with a button:
fun deleteToDo(todoId: Long){
val db = writableDatabase
db.delete(TABLE_TODO_ITEM,"$COL_TODO_ID=?", arrayOf(todoId.toString()))
db.delete(TABLE_TODO,"$COL_ID=?", arrayOf(todoId.toString()))
}
This is the recyclerview adapter that I am using:
class ItemAdapter(val context: Context,val dbHandler: DBHandler, val list: MutableList<ToDoItem>) :
RecyclerView.Adapter<ItemAdapter.ViewHolder>(){
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {
return ViewHolder(LayoutInflater.from(context).inflate(R.layout.rv_child_item,p0,false))
}
override fun onBindViewHolder(holder: ViewHolder, p1: Int) {
holder.itemName.text = list[p1].itemName
holder.itemName.isChecked = list[p1].isCompleted
holder.itemName.setOnClickListener{
list[p1].isCompleted = !list[p1].isCompleted
dbHandler.updateToDoItem(list[p1])
}
}
override fun getItemCount(): Int {
return list.size
}
class ViewHolder(v : View) : RecyclerView.ViewHolder(v){
val itemName : CheckBox = v.findViewById(R.id.cb_item)
}
}
but for some reason I cannot make it work when I try to call the delete function in this Swipetodelete object:
val item = object : SwipeToDelete(this,0,ItemTouchHelper.LEFT){
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
}
}
I also want to add the edit functionality but if i can get this delete functionality to work i can add it.
Since the SwipeToDelete is an abstract class, you can override the onSwiped function on your Fragment/Activity Class.
You can modify your SwipeToDelete class to:
abstract class SwipeToDelete(context: Context, dragDir: Int, swipeDir: Int): ItemTouchHelper.SimpleCallback(dragDir, swipeDir) {
override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean {
return false
}
}
And then override the onSwiped function inside your fragment/activity and attach it to your recyclerview:
val item = object : SwipeToDelete(this,0,ItemTouchHelper.LEFT){
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
deleteToDo(todoId)
}
}
ItemTouchHelper(item).attachToRecyclerView(recycler)

Can Retrofit convert #Path parameters from custom class to primitive types

I have a following Retrofit request:
#GET("event/{id}")
suspend fun getEvent(#Path("id") eventId: Long): Response<Event>
However in my application I'm using the following wrapper for the event id:
data class EventId(val value: Long)
Is it possible to pass EventId class as a path parameter and somehow tell Retrofit how to convert the value to Long?
Here is the method signature I want to have:
#GET("event/{id}")
suspend fun getEvent(#Path("id") eventId: EventId): Response<Event>
The solution I've found is to register converter factory in the Retrofit Builder:
class EventIdConverterFactory : Converter.Factory() {
override fun stringConverter(type: Type, annotations: Array<Annotation>, retrofit: Retrofit) =
if (type == EventId::class.java) {
Converter<Any, String> { (it as EventId).value.toString() }
} else {
super.stringConverter(type, annotations, retrofit)
}
}

need help when i try to custom hide password kotlin

I want user write "A" for example and see "b", I want some different way with custom keyboard for the app
this what I tried today but not worked
import android.text.method.PasswordTransformationMethod
import android.view.View
class passit: PasswordTransformationMethod() {
override fun getTransformation(source: CharSequence?, view: View?): CharSequence {
return turnit(source!!) }
class turnit(val s:CharSequence):CharSequence{
override val length: Int= s.length
override fun get(index: Int): Char {
return 'f' }
override fun subSequence(startIndex: Int, endIndex: Int): CharSequence {
return s.subSequence(0,s.length) }
}
}
mian give the error: transformationMethod of type cannot be invoked as a function
Petitionadd.transformationMethod(passit())
The function on TextView you're trying to call is setTransformationMethod:
textView.setTransformationMethod(Passit())
Which you can also access from Kotlin as a property:
textView.transformationMethod = Passit()

Resources