Reputation: 35
I am trying to implement the toolbar that Android made available in v21 with a menu where the menu items are set to show "ifroom", but the menu items always collapses into the overflow. They also collapses into the overflow if the menu items are set to show "always".
I hope someone can point out what needs to changed to make it behave correct. I would like to find a solution which doesn't do it using appcompat to achieve it, as I can see most other solutions does.
Below I have attached my toolbar layout, menu layout and screenshoot of how it looks in the simulator, as well as the main kotlin file creating the toolbar.
Screenshoot from emulator
toolbar_main.xml
<?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">
<android.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:elevation="4dp"
android:theme="@style/ToolbarTheme"
android:popupTheme="@android:style/ThemeOverlay.Material.Light"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="0dp" />
</android.support.constraint.ConstraintLayout>
menu_main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/miCompose"
android:icon="@drawable/ic_mail_outline_black_24dp"
android:title="@string/menu_compose"
android:orderInCategory="2"
app:showAsAction="ifRoom" />
<item
android:id="@+id/miProfile"
android:icon="@drawable/ic_perm_identity_black_24dp"
android:title="@string/menu_profile"
android:orderInCategory="1"
app:showAsAction="ifRoom"/>
</menu>
mainActivity.kt
import android.app.Activity
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.widget.Toolbar
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.activity_main.view.*
class MainActivity : Activity() {
val TAG: String = "MainActivity"
override fun onCreate(savedInstanceState: Bundle?) {
Log.d(TAG, "onCreate ")
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val myToolbar = findViewById<View>(R.id.my_toolbar) as Toolbar
myToolbar.setTitle(R.string.title_activity_main)
myToolbar.inflateMenu(R.menu.menu_main)
}
}
Upvotes: 1
Views: 437
Reputation: 6495
You are using all Android APIs, not support library.
So try using android
instead of app
:
android:showAsAction="ifRoom"
Upvotes: 3