Anko: Setting supportActionBar

Created on 22 May 2016  路  3Comments  路  Source: Kotlin/anko

I have been trying to set toolbar like this:

class MainActivity : AppCompatActivity() {

  override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      ui()
  }
}
fun MainActivity.ui() = frameLayout {

    toolbar {
        title = "Mist Player"
    }.let { setSupportActionBar(it) }

    mainView { recyclerList().let { bindList(it) } }
}

but can't get it to work. How to get around this ?

fixed question

Most helpful comment

I think the easiest way is to assign an id to your toolbar, and set the action bar in onCreate(), like this:

In res/values/ids.xml

    <resources>
        <item name="toolbar" type="id"/>
    </resources>

In your MainActivity class:

    onCreate(...) {
        super.onCreate(...)
        setSupportActionBar(findOptional<Toolbar>(R.id.toolbar))
    }

    toolbar {
        title = "Mist Player"
        id = R.id.toolbar
    }

All 3 comments

I think the easiest way is to assign an id to your toolbar, and set the action bar in onCreate(), like this:

In res/values/ids.xml

    <resources>
        <item name="toolbar" type="id"/>
    </resources>

In your MainActivity class:

    onCreate(...) {
        super.onCreate(...)
        setSupportActionBar(findOptional<Toolbar>(R.id.toolbar))
    }

    toolbar {
        title = "Mist Player"
        id = R.id.toolbar
    }

@gregschlom findOptional gives an error? how to use it ? and what it does ?

how to change baground for activity and how to use id in anko
@mridah
@lupajz
@gregschlom

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Anilugale picture Anilugale  路  4Comments

SuleymanovTat picture SuleymanovTat  路  4Comments

maomaobug picture maomaobug  路  4Comments

nkanellopoulos picture nkanellopoulos  路  4Comments

telenc picture telenc  路  3Comments