英文:
How to add day night switcher in toolbar menu
问题
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_previous:
onBackPressed();
break;
case R.id.nav_next:
if (webView.canGoForward()) {
webView.goForward();
}
break;
case R.id.nav_reload:
checkConnection();
break;
// Add functionality for night mode and day mode icons
case R.id.nav_night_mode:
// Change app theme to night mode
break;
case R.id.nav_day_mode:
// Change app theme to day mode
break;
}
return super.onOptionsItemSelected(item);
}
In the code snippet above, I've added placeholders for the functionality related to the night mode and day mode icons. You will need to implement the actual logic to change the app theme to night mode and day mode within the respective cases.
英文:
This is the image of my toolbar Menu there are 5 icons i.e backward, forward, reload, moon symbol, and sun symbol.
now I want to add functionality to moon and sun icon i.e when the moon icon is clicked app theme changes to night mode and when sun icon is clicked the app theme. changes to day mode.
*This is the code i have written for backward, forward and reload but now what should. i write for night mode and daymode for android app
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar_menu,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_previous:
onBackPressed();
break;
case R.id.nav_next:
if (webView.canGoForward()) {
webView.goForward();
}
break;
case R.id.nav_reload:
checkConnecttion();
break;
}
return super.onOptionsItemSelected(item);
}
答案1
得分: 1
像这样做:
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
// 获取应用的夜间模式状态
int nightMode = AppCompatDelegate.getDefaultNightMode();
case R.id.nav_previous:
onBackPressed();
break;
case R.id.nav_next:
if (webView.canGoForward()) {
webView.goForward();
}
break;
case R.id.nav_reload:
checkConnecttion();
break;
case R.id.moon:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
break;
case R.id.sun:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
break;
}
// 重新创建活动以使主题更改生效。
recreate();
return super.onOptionsItemSelected(item);
}
我建议你查看这个链接:LINK
它很好地描述了解决问题的另一种方法,当然你可以根据需要进行修改。
英文:
do something like this:
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
//Get the night mode state of the app
int nightMode = AppCompatDelegate.getDefaultNightMode();
case R.id.nav_previous:
onBackPressed();
break;
case R.id.nav_next:
if (webView.canGoForward()) {
webView.goForward();
}
break;
case R.id.nav_reload:
checkConnecttion();
break;
case R.id.moon:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
break;
case R.id.sun:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
break;
}
// Recreate the activity for the theme change to take effect.
recreate();
return super.onOptionsItemSelected(item);
}
I suggest you to take a look at this link: LINK
It describe very well a different way to solve your problem but of course you can modify it according to your needs
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论