TornadoFX key press listener issues - kotlin

When I run the following code
fun main(args: Array<String>) {
Application.launch(HelloWorldApp::class.java, *args)
}
class HelloWorldApp : App(HelloWorld::class)
class HelloWorld : View() {
override val root = hbox {
addEventFilter(KeyEvent.ANY) { event ->
println("pressed:"+event.character)
}
}
}
When I press any keys on my keyboard the println() is never called. Am I missing something?

Simply adding an HBox does not give it focus, and when it doesn't have focus it won't receive key events. You should override onDock and add the listener to the currentScene instead. If you really need to add the listener on the HBox, add the listener and request focus once the view has been docked:
fun main(args: Array<String>) {
launch<HelloWorldApp>(args)
}
class HelloWorldApp : App(HelloWorld::class)
class HelloWorld : View() {
override val root = hbox {
addEventFilter(KeyEvent.ANY) { event ->
println("pressed:" + event.character)
}
}
override fun onDock() {
root.requestFocus()
}
}

Looking for a similar problem I came up with this, which looks simpler, but I do not yet understand any possible subtle differences between using the keyboard control versus explicit focus requesting.
import javafx.scene.input.KeyEvent
import tornadofx.*
fun main(args: Array<String>) {
launch<HelloWorldApp>(args)
}
class HelloWorldApp : App(HelloWorld::class)
class HelloWorld : View() {
override val root = hbox {
keyboard {
addEventHandler(KeyEvent.KEY_PRESSED) { println(it.code) }
}
}
}

Related

How to add a button to the view dynamically?

I'm new to Kotlin and TorandoFX. Maybe I'm missing something very basic in TornadoFX. I want to create from a list (which shoulde be mutable) buttons in the view. If the user clicks on the add button the list should get a new item and this should result in a new button in the view.
Thank you for your help.
I was thinking it should look like this:
import tornadofx.*
fun main(args: Array<String>) {
launch<MyApp>(args)
}
class MyApp: App(MainView::class)
class MainView: View("MainView") {
val values = ArrayList<Int>(listOf(1,2,3)).asObservable()
var count = 4
override val root = vbox {
values.forEach { x ->
button(x.toString())
}
button("add") {
action {
values.add(count)
println(values.toString())
count++
}
}
}
}
this code result in this view, but if I click the button the view doesnt refresh.
This code result in this view, but if I click the button the view doesnt refresh. I think I'm missing something about binding.
We figured out, I was right with the binding part. I just had to use bindChildren() function and give the function an observableArray and a function to for the conversion of the elements of the array as a parameter.
Thank you for the help.
import javafx.collections.FXCollections
import tornadofx.*
fun main(args: Array<String>) {
launch<MyApp>(args)
}
class MyApp: App(MainView::class)
class MainView: View("MainView") {
val values = FXCollections.observableArrayList<Int>(listOf(1,2,3))
var count = 4
override val root = vbox {
vbox {
bindChildren(values) { x ->
button(x.toString())
}
}
vbox() {
button("add") {
action {
values.add(count)
count++
}
}
}
}
}

how do i update configuration in kotlin for change language on radio btn selected in kotlin

how to update this one without using resources.updateConfiguration this commented codes please any can say? and solve it.
when i click th eradio btn an dthe language will have to change
this is mainActivity.kt file
class LanguageChange : AppCompatActivity() {
#RequiresApi(Build.VERSION_CODES.N)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_language_change)
radio_group_btn.setOnCheckedChangeListener { _, i ->
when(i){
R.id.radio_btn_fr_id -> {
setLang("fr")
txt_view1_id.setText(R.string.view_one)
txt_view2_id.setText(R.string.view_two)
Log.i("tag","french")
}
R.id.radio_btn_en_id -> {
setLang("en")
txt_view1_id.setText(R.string.view_one)
txt_view2_id.setText(R.string.view_two)
Log.i("tag","english")
}
}
}
}
private fun Context.setLang(lan: String): Context{
// val config = resources.configuration
// config.setLocale(Locale(lan))
// resources.updateConfiguration(config,resources.displayMetrics)
// onConfigurationChanged(config)
val config = resources.configuration
config.setLocale(Locale(lan))
config.setLayoutDirection(Locale(lan))
applyOverrideConfiguration(config)
return createConfigurationContext(config)
}
#RequiresApi(Build.VERSION_CODES.N)
override fun attachBaseContext(newBase: Context?) {
super.attachBaseContext(ContextWrapper(newBase?.setLang("en")))
}
}
It's a good question. Short answer you can't. Even with updateConfiguration you can't be sure about result. For more details please read this article https://medium.com/swlh/android-app-specific-language-change-programmatically-using-kotlin-d650a5392220. But i say it again there is no 100% method to change language inside app with default strings.

Kotlin Recycleview ,how to make onItemClick for views

I am attempting to make a function :
In recyclerView
when you click the user image ,navigate to the userActivity,
and when you click the "gift" icon ,navigate to otherActivity .
As follow is my Adapter.kt :
package Users.UserReceiveGiftItem
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.gearsrun.www.R
import com.squareup.picasso.Picasso
import kotlinx.android.synthetic.main.user_receive_gift_item.view.*
class UserReceiveGiftAdapter(val userList : List<UserReceiveGiftItem>) : RecyclerView.Adapter<UserReceiveGiftAdapter.UserHolder>(){
private lateinit var mListener :onItemClickListener
interface onItemClickListener{
fun onItemClick(view:View,position: Int)
}
fun setOnItemClickListener(listener: onItemClickListener){
mListener = listener
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): UserHolder {
val layoutInflater = LayoutInflater.from(parent.context)
return UserHolder(layoutInflater.inflate(R.layout.user_receive_gift_item,parent,false),mListener)
}
override fun onBindViewHolder(holder: UserReceiveGiftAdapter.UserHolder, position: Int) {
holder.render(userList[position])
}
override fun getItemCount(): Int = userList.size
class UserHolder(val view : View,listener:onItemClickListener) : RecyclerView.ViewHolder(view){
fun render(userList: UserReceiveGiftItem){
Picasso.get().load(userList.user_img).into(view.user_img)
view.user_name.text = userList.user_name
view.time.text = userList.time
view.userId.text = userList.userId
view.giftImg.setImageResource(userList.giftImg)
}
init {
view.setOnClickListener {
listener.onItemClick(it,absoluteAdapterPosition)
}
}
}
}
And I use in the Activity :
package com.gearsrun.www.UI.Receive
import Users.UserReceiveGiftItem.UserReceiveGiftAdapter
import Users.UserReceiveGiftItem.UserReceiveGiftItem
import android.content.Intent
import android.graphics.drawable.ColorDrawable
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.ActionBar
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.gearsrun.www.R
import com.gearsrun.www.UI.Gift.AwaitingToUnwrapActivity
import com.gearsrun.www.UI.Sunflower.SunflowerAvailableActivity
import com.gearsrun.www.UI.User.UserDetailActivity
import kotlinx.android.synthetic.main.activity_who_receive_gift.*
import kotlinx.android.synthetic.main.user_receive_gift_item.view.*
class ReceiveActivity : AppCompatActivity() {
val userList : List<UserReceiveGiftItem> = listOf(
UserReceiveGiftItem(
"https://i.pinimg.com/564x/63/85/68/63856877880614e0dab080071513156f.jpg",
"Sharry",
"10 mins ago",
"517ddY",
R.drawable.donut
),
)
lateinit var gift_unwrap : ImageView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_receive)
changeColor(R.color.font_green)
initRecycler()
gift_unwrap = findViewById(R.id.gift_unwrap)
gift_unwrap.setOnClickListener {
}
}
private fun changeColor(resourseColor: Int) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.statusBarColor = ContextCompat.getColor(applicationContext, resourseColor)
}
val bar: ActionBar? = supportActionBar
if (bar != null) {
bar.setBackgroundDrawable(ColorDrawable(resources.getColor(resourseColor)))
}
}
fun initRecycler(){
rvUser.layoutManager = LinearLayoutManager(this)
val adapter = UserReceiveGiftAdapter(userList)
rvUser.adapter = adapter
adapter.setOnItemClickListener(object:UserReceiveGiftAdapter.onItemClickListener{
override fun onItemClick(view: View, position: Int) {
view.user_img.setOnClickListener {
val intent = Intent(this#ReceiveActivity,UserDetailActivity::class.java)
startActivity(intent)
}
view.giftImg.setOnClickListener {
val intent =Intent(this#ReceiveActivity,SunflowerAvailableActivity::class.java)
startActivity(intent)
}
}
})
}
}
It is a bit rare that it is able to navigate successfully ,however ,it dosen't react for the first click ..Could you please take a look my code ?Thank you guys in advance !!
You are using the click listener of the list item to add more click listeners to its children. So the first time you click it, the children don't have listeners yet and won't do anything. It is also error prone that a parent and its children both have click listeners, because it's undefined what will happen if you change the click listener as you are clicking it.
Instead, you should define your interface in the adapter to handle both types of clicks. In the ViewHolder, set click listeners only on the relevant children to be clicked.
Also, I think you're misusing lateinit. I would make the property nullable. And it is redundant to have a setter function for a property's value.
If you make the ViewHolder class inner, then you don't have to pass instances of the listener to the view holder instances, which will cause problems if you ever change the Adapter's listener.
Finally, personally I think the listener should return the list item, not the specific view and position in the list. Those are implementation details that the outer class doesn't need to know about.
class UserReceiveGiftAdapter(val userList : List<UserReceiveGiftItem>) : RecyclerView.Adapter<UserReceiveGiftAdapter.UserHolder>(){
var onItemClickListener: OnItemClickListener? = null
interface OnItemClickListener{
fun onUserClick(item: UserReceiveGiftItem)
fun onGiftClick(item: UserReceiveGiftItem)
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): UserHolder {
val layoutInflater = LayoutInflater.from(parent.context)
return UserHolder(layoutInflater.inflate(R.layout.user_receive_gift_item,parent,false))
}
override fun onBindViewHolder(holder: UserReceiveGiftAdapter.UserHolder, position: Int) {
holder.render(userList[position])
}
override fun getItemCount(): Int = userList.size
inner class UserHolder(val view : View) : RecyclerView.ViewHolder(view){
fun render(userList: UserReceiveGiftItem){
Picasso.get().load(userList.user_img).into(view.user_img)
view.user_name.text = userList.user_name
view.time.text = userList.time
view.userId.text = userList.userId
view.giftImg.setImageResource(userList.giftImg)
}
init {
view.user_img.setOnClickListener {
onItemClickListener?.onUserClick(userList[absoluteAdapterPosition])
}
view.giftImg.setOnClickListener {
onItemClickListener?.onGiftClick(userList[absoluteAdapterPosition])
}
}
}
}
In Activity or Fragment:
adapter.onItemClickListener = object: UserReceiveGiftAdapter.OnItemClickListener {
override fun onUserClick(item: UserReceiveGiftItem) {
val intent = Intent(this#ReceiveActivity, UserDetailActivity::class.java)
startActivity(intent)
}
override fun onGiftClick(item: UserReceiveGiftItem) {
val intent = Intent(this#ReceiveActivity, SunflowerAvailableActivity::class.java)
startActivity(intent)
}
}
What I think after looking your code is, that you are setting adapter to your recyclerview first and then you are injecting a click listener to adapter.
What happens here is that some items from recycler view are loaded and they wouldn't have the click listener at that time,
for example if you scroll down, the newly appeared items on the screen will have proper click listeners, now if you scroll up, the previous one's will also have listener attached.
What to do to get rid of this problem:
Change your initRecycler() method as below:
fun initRecycler(){
rvUser.layoutManager = LinearLayoutManager(this)
val adapter = UserReceiveGiftAdapter(userList)
adapter.setOnItemClickListener(object:UserReceiveGiftAdapter.onItemClickListener{
override fun onItemClick(view: View, position: Int) {
view.user_img.setOnClickListener {
val intent = Intent(this#ReceiveActivity,UserDetailActivity::class.java)
startActivity(intent)
}
view.giftImg.setOnClickListener {
val intent =Intent(this#ReceiveActivity,SunflowerAvailableActivity::class.java)
startActivity(intent)
}
}
})
//set adapter after setting click listener to your adapter.
rvUser.adapter = adapter
}

Navigation controller AlertDialog click listner

I'm using Android's Navigation component and I'm wondering how to setup AlertDialog from a fragment with a click listener.
MyFragment
fun MyFragment : Fragment(), MyAlertDailog.MyAlertDialogListener {
...
override fun onDialogPostiveCLick(dialog: DialogFragment) {
Log.i(TAG, "Listener returns a postive click")
}
fun launchMyAlertDialog() {
// Here I would typically call setTargetFragment() and then show the dialog.
// but findnavcontroller doesn't have setTargetFragment()
findNavController.navigate(MyFragmentDirection.actionMyFragmentToMyAlertDialog())
}
}
MyAlertDialog
class MyAlertDialog : DialogFragment() {
...
internal lateinit var listener: MyAlertDialogListener
interface MyAlertDialogListener{
fun onDialogPostiveCLick(dialog: DialogFragment)
}
override fun onCreateDialog(savdInstanceState: Bundle?): Dialog {
return activity?.let {
val builder = AlertDialog.Builder(it)
builder.setMessage("My Dialog message")
.setPositiveButton("Positive", DialogInterface.OnClickListener {
listener = targetFragment as MyAlertDialogListener
listener.onDialogPositiveClick(this)
}
...
}
}
}
This currently receives a null point exception when initializing the listener in MyAlertDialog.
To use targetFragment, you have to set it first as you commented, unfortunately jetpack navigation does not do this for you (hence the null pointer exception). Check out this thread for alternative solution: https://stackoverflow.com/a/50752558/12321475
What I can offer you is an alternative. If the use-case is as simple as displaying a dialog above current fragment, then do:
import androidx.appcompat.app.AlertDialog
...
class MyFragment : Fragment() {
...
fun onDialogPostiveCLick() {
Log.i(TAG, "Listener returns a postive click")
}
fun launchMyAlertDialog() {
AlertDialog.Builder(activity)
.setMessage("My Dialog message")
.setPositiveButton("Positive") { _, _ -> onDialogPostiveCLick() }
.setCancellable(false)
.create().show()
}
}

Function declaration error when compiling with Kotlin 12

I've inherited an older Kotlin codebase, and attempting to compile with the newest compiler had many issues. The one that I'm having trouble figuring out are these strange functions that are hanging out in the middle of a class, without any apparent call. I'm wondering if anyone knows what this used to be, and what it was replaced with in newer versions of Kotlin?
public class SomeAdapter(val friends: SomeAdapterProvider, val listener: OnItemClickedListener) : RecyclerView.Adapter<SomeAdapter.ViewHolder>() {
trait OnItemClickedListener {
fun onItemClicked(f: Friendship)
}
private inner class ViewHolder(v: View) : RecyclerView.ViewHolder(v), View.OnClickListener {
override fun onClick(v: View) {
listener.onItemClicked(somethings[getPosition()])
}
val text: TextView by inject(android.R.id.text1)
val image: Picture by inject(R.id.imageview);
{
itemView setOnClickListener this
}
}
{
setHasStableIds(true)
}
}
Specifically, the lines in question are the itemView setOnClickListener this and setHasStableIds(true) , both in-between braces just hanging out.
Prefix those 2 function blocks with "init",
see Prefixes For Initializer Blocks
As noted by #Andrey in his comment to the question, and along with the answer from #D3xter (adding init to the initialization blocks), here is the updated code (for reference):
public class SomeAdapter(val friends: SomeAdapterProvider, val listener: OnItemClickedListener) : RecyclerView.Adapter<SomeAdapter.ViewHolder>() {
trait OnItemClickedListener {
fun onItemClicked(f: Friendship)
}
private inner class ViewHolder(v: View) : RecyclerView.ViewHolder(v), View.OnClickListener {
override fun onClick(v: View) {
listener.onItemClicked(somethings[getPosition()])
}
val text: TextView by inject(android.R.id.text1)
val image: Picture by inject(R.id.imageview);
init { // FIXED here
itemView setOnClickListener this
}
}
init { // FIXED here
setHasStableIds(true)
}
}