Quantcast
Channel: onCreateOptionsMenu is never called - Stack Overflow
Viewing all articles
Browse latest Browse all 13

Answer by Allen for onCreateOptionsMenu is never called

$
0
0

That is because the activity does not have the toolbar.

There are 2 steps in order to do it.

First, you need to add the toolbar in your activity.xml which is in res/layout

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent""><!-- add this part--><android.support.v7.widget.Toolbar        android:id="@+id/toolbar"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:background="?attr/colorPrimary"        android:minHeight="?attr/actionBarSize"        android:theme="?attr/actionBarTheme"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constraintTop_toTopOf="parent" /></android.support.constraint.ConstraintLayout>

Second, let your activity append it

in JAVA

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);setSupportActionBar(toolbar);

or in Kotlin

val toolbar = findViewById<Toolbar>(R.id.toolbar)setSupportActionBar(toolbar)

Viewing all articles
Browse latest Browse all 13

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>