I am trying to add a Toolbar to my application, but now instead of it, a white rectangle corresponding to the sizes of the Toolbar is displayed without everything. I did everything according to the guide: developer.android.com But without result ahead.
This is how the toolbarlayout.xml file looks in the menu folder:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" tools:context="com.mazgutov.rail.app.HeroSelectActivity" > <item android:id="@+id/action_settings" android:orderInCategory="100" app:showAsAction="never" android:title="Hi"/> </menu> This is what layout activity looks like in which I want to add a Toolbar:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.v7.widget.Toolbar android:id="@+id/my_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:elevation="4dp" android:theme="@style/ThemeOverlay.AppCompat.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".StartActivity" android:background="@mipmap/background" android:layout_below="@+id/my_toolbar"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start" android:id="@+id/button_start" android:onClick="onClickStart" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/heroes_list" android:layout_above="@+id/button_start" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@+id/edit_search_hero" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/edit_search_hero" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cleaer selection" android:id="@+id/button_clear" android:layout_below="@+id/heroes_list" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> </RelativeLayout> </RelativeLayout> I added to the onCreate (..) method of this Activity in the following lines:
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar); setSupportActionBar(myToolbar); It did not work, I began to search further and came across the article: Habrahabr After that, a method appeared in the Activity file:
@Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.toolbarlayout, menu); return true; } I met somewhere that helped someone to replace the previous method with:
@Override public boolean onPrepareOptionsMenu(final Menu menu) { getMenuInflater().inflate(R.menu.toolbarlayout, menu); return super.onCreateOptionsMenu(menu); } Manifest of my application:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mazgutov.rail.app"> <application android:allowBackup="true" android:icon="@drawable/icon" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/Theme.AppCompat.Light.NoActionBar"> <activity android:name=".StartActivity" android:windowFullscreen = "true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".TimersListActivity" android:label="Timers"></activity> <activity android:name=".HeroSelectActivity" android:label="Heroes"></activity> <activity android:name=".SettingsActivity" android:label="Settings" android:theme="@style/AppTheme.NoActionBar"></activity> </application> </manifest>