I tried everything, I can not see items in my action bar, can someone help me?
Here is my java code:
public class mActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(myLayout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_frontend);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_search, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_logout) {
Intent activity2 = new Intent(mActivity.this, LoginPage.class);
startActivity(activity2);
}
if (id == R.id.action_search) {
//toast
}
return super.onOptionsItemSelected(item);
}
}
This is my xml code:
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
android:background="#DDDDDD"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_frontend"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/right_drawer_frontend"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#DDDDDD"
android:layout_margin="5dp"
>
//...
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view_right_frontend"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_right_customer"
app:menu="@menu/menu_right_customer" />
</android.support.v4.widget.DrawerLayout>
<android.support.design.widget.FloatingActionButton
//...
/>
</android.support.design.widget.CoordinatorLayout>
And this is my xml menu code:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_logout"
android:orderInCategory="100"
android:title="@string/action_logout"
app:showAsAction="never" />
<item android:id="@+id/action_search"
android:title="Search..."
android:icon="@drawable/ic_search_black_24dp"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
</menu>
I have only a "black" appbar and that's ok but I can't see items in it. I appreciate any help. Thanks in advance.
ah search is an action VIew, you need to initialise that in the onCreateOptionsMenu to do that add these lines to your onCreateOptionsMenu function
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
To show the Search action as an Icon always set showAsAction attribute to always like this
android:showAsAction="collapseActionView|always"