Iam creating an app where users can add items using the add item button which is in recycler view but I also want to add menu buttons which should display on the toolbar…
Problem
The menu buttons are not getting displayed on the tool bar. In design preview of menu_main.xml the buttons are being displayed properly.
But when running this app on device the menu buttons are not being displayed.
ActivityMain code: (In this I have created onCreateOptionsMenu method at the bottom)
package com.example.foodmanagement;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
//import androidx.appcompat.widget.Toolbar;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
import android.widget.Toolbar;
public class MainActivity extends AppCompatActivity {
Toolbar mToolbar;
Button mBtnAdd;
BucketRecyclerView mRecycler;
Realm mRealm;
RealmResults<Drop> mResults;
View mEmptyView ;
AdapterDrops mAdapter;
private View.OnClickListener mBtnAddListener = (v) -> {
showDialogAdd();
};
private AddListener mAddListener = new AddListener() {
@Override
public void add() {
showDialogAdd();
}
};
private RealmChangeListener mChangeListener = new RealmChangeListener() {
@Override
public void onChange(Object o) {
Log.d(TAG, "onChange: was called");
mAdapter.update(mResults);
}
};
private MarkListener mMarkListener = new MarkListener() {
@Override
public void onMark(int position) {
showDialogMarkFinish(position);
}
};
private CompleteListener mCompleteListener = new CompleteListener() {
@Override
public void onComplete(int position) {
mAdapter.markComplete(position);
}
};
private void showDialogAdd() {
DialogAdd dialog = new DialogAdd();
dialog.show(getSupportFragmentManager(),"Add");
}
private void showDialogMarkFinish(int position) {
DialogMarkFinish dialog = new DialogMarkFinish();
Bundle bundle = new Bundle();
bundle.putInt("POSITION", position);
dialog.setArguments(bundle);
dialog.setCompleteistener(mCompleteListener);
dialog.show(getSupportFragmentManager(), "Mark");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitymain);
mRealm = Realm.getDefaultInstance();
mResults = mRealm.where(Drop.class).findAllAsync();
mToolbar = findViewById(R.id.toolbar);
mEmptyView = findViewById(R.id.empty_drops);
mRecycler = (BucketRecyclerView) findViewById(R.id.rv_drops);
mRecycler.addItemDecoration(new Divider(this, LinearLayoutManager.VERTICAL));
mRecycler.hideIfEmpty(mToolbar);
mRecycler.showIfEmpty(mEmptyView);
LinearLayoutManager manager = new LinearLayoutManager(this);
mRecycler.setLayoutManager(manager);
mAdapter = new AdapterDrops(this,mRealm, mResults, mAddListener, mMarkListener);
mRecycler.setAdapter(mAdapter);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setActionBar(mToolbar);
}
mBtnAdd = (Button) findViewById(R.id.btn_addd);
mBtnAdd.setOnClickListener(mBtnAddListener);
SimpleTouchCallback callback = new SimpleTouchCallback(mAdapter);
ItemTouchHelper helper = new ItemTouchHelper(callback);
helper.attachToRecyclerView(mRecycler);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=getMenuInflater ();
inflater.inflate ( R.menu.menu_main,menu );
return true;
}
@Override
protected void onStart() {
super.onStart();
mResults.addChangeListener(mChangeListener);
}
@Override
protected void onStop() {
super.onStop();
mResults.removeChangeListener(mChangeListener);
}
}
Menu_main.xml code
<?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/action_add"
android:icon="@drawable/ic_action_add"
android:title="Add"
app:showAsAction="always" />
<item
android:id="@+id/action_sort_descending_date"
android:title="Most Time Left" />
<item
android:id="@+id/action_sort_ascending_date"
android:title="Least Time Left" />
<item
android:id="@+id/action_show_complete"
android:title="Finished" />
<item
android:id="@+id/action_show_incomplete"
android:title="Remaining" />
</menu>
v21/toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/dark_blue">
</Toolbar>
layout/toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/abc_action_bar_default_height_material"
android:background="@android:color/white">
</androidx.appcompat.widget.Toolbar>
values/theme.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.FoodManagement" parent="Theme.MaterialComponents.DayNight.NoActionBar.Bridge">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_500</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
night/theme.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.FoodManagement" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/purple_200</item>
<item name="colorPrimaryVariant">@color/purple_700</item>
<item name="colorOnPrimary">@color/black</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_200</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
2
Answers
Very basic errors.. A-> In ActivityMain.java
import androidx.appcompat.widget.Toolbar
instead ofimport android.widget.Toolbar;
as rightly said by @Mayur Gajraandroidx.appcompat.widget.Toolbar mToolbar
instead ofandroid.widget.Toolbar mToolbar
mToolbar = (androidx.appcompat.widget.Toolbar) findViewById(R.id.toolbar);
and not asmToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
instead ofsetActionBar(mToolbar);
B-> In v21toolbar.xml
<Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
with<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
C-> Change
Theme.AppCompat.Light.DarkActionBar
toTheme.MaterialComponents.DayNight.NoActionBar.Bridge
as accurately pointed out by @Mayur GajraYou need to use
androidx
toolbar while assigning at runtime alsosetSupportActionBar(mToolbar);
instead ofsetActionBar
.Example:
Result:
Note: If you’re looking to have your own action bar then you should have
NoActionBar
innight
theme as well.