Soroush Lotfi
Soroush Lotfi

Reputation: 81

Activity.setTitle doesn't work in runOnUiThread block

I need to get a Note() object from my room database from a background thread and set the title of the note as my activity title, but title = note.title doesn't work and I see my application name in the toolbar. I have also tried supportActionBar?.title and toolbar.title but none of them solved the issue. I'm sure that the database is giving me the right data and I don't know where is the problem. Any help is appreciated.

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_show_note)
    setSupportActionBar(toolbar)
    supportActionBar?.setDisplayHomeAsUpEnabled(true)
}

override fun onResume() {
    super.onResume()
    intent.extras?.also {
        val id = it.getInt(ID_EXTRA)
        Thread(Runnable {
            note = db.noteDao().getNote(id)
            runOnUiThread {
                title = note.title
                tvShowNote.text = note.note
                tvShowTime.text = note.time.format()
            }
        }).start()
    }
}

Upvotes: 0

Views: 144

Answers (2)

Ashik Lanjewar
Ashik Lanjewar

Reputation: 91

You have to set something like this.

supportActionBar!!.title = title //your_title_put_here

Have you tried the same without runOnUiThread.

Upvotes: 1

karan
karan

Reputation: 8853

setTitle is method of action bar, you need to use actionbar's instance to set its property. try using below code after you have set your toolbar as support actionbar.

val actionBar = supportActionBar
actionBar!!.title = "your_title"

Upvotes: 0

Related Questions