-1

I am developing Android project in Kotlin.

I got my toolbar:

@BindView(R.id.tool_bar)
lateinit var toolbar: Toolbar

I want to set its title :

// student.id is a int type
toolbar.title =  student.id

Student's id is a Int type. toolbar.title expects a value of type CharSequence. How to assign my id to toolbar's title? I mean how can I convert student.id to CharSequence so that I can assign it to my toolbar title ?

Leem
  • 17,220
  • 36
  • 109
  • 159

3 Answers3

0

Use:

toolbar.title = student.id.toString()
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • No, compiler still complains – Leem Jan 01 '20 at 16:43
  • 3
    @Leem: Complains about *what*? [`String` is an implementation of `CharSequence`](https://developer.android.com/reference/java/lang/String), and [`toString()` on `Int` returns a `String`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/to-string.html). – CommonsWare Jan 01 '20 at 16:54
0

Can you try kotlin string template for your case

toolbar.title = "${student.id}"
Raghul Vaikundam
  • 588
  • 4
  • 20
0

This is best way to convert any type of number numbers (int, float, double) into string:-

toolbar.title = "" + student.id

Remember one thing, "" this must be before the number.
After that, put a + and then, put any type of number.

This technique is supported by many programming languages like Java, Kotlin, Javascipt etc.

Shariful Islam Mubin
  • 2,146
  • 14
  • 23