Toolbar menu image
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);
}
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
I have a popup menu with two checkboxs but when I press either of them the menu just disappears and nothing changes in their state. I've looked around and found things that work for other people but they don't work for me
public boolean onOptionsItemSelected(MenuItem item)
{
if(item.isChecked())
{
item.setChecked(false);
}
else
{
item.setChecked(true);
}
switch(item.getItemId())
{
case R.id.lockscreen:
if(item.isChecked()) item.setChecked(!item.isChecked());
break;
case R.id.notif:
if(item.isChecked()) item.setChecked(!item.isChecked());
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
This the XML for the menu
<group android:checkableBehavior="all">
<item android:id="#+id/lockscreen"
android:title="Lockscreen"
android:checked="false"
android:checkable="true"/>
<item android:id="#+id/notif"
android:title="Notification"
android:checked="false"
android:checkable="true"/>
</group>
if(item.isChecked()) {
item.setChecked(false);
}
else {
item.setChecked(true);
}
These lines seem to instantly uncheck the item if it was checked beforeā¦
What are you trying to do in this if-else construct?
Just comment it out and let the other if-statements do all the work (those in the cases).
In my Android project user is clicking few times at button so I want to block that button if it's pressed already. Now SaveOrder() method is fired few times which is not acceptable.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.order_save:
Log.d("Custom Synchronization", "Click!");
SaveOrder();
canQuit = true;
hasAcceptQuit = true;
onBackPressed(); // navigate to other acitivty
return true;
case R.id.order_adnotation:
setAdnotationOrder();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
You could use a boolean that starts with false like:
private static boolean hasPressed = false;
And then when calling the SaveOrder method you check for the boolean to be false and change it:
if(!hasPressed) {
SaveOrder();
hasPressed = true;
}
Then, after the execution of the method, you could change the boolean again so the user can call the method again. Be aware that the animation of click will still happen, so consider showing something like a Toast to the user to inform what's going on.
I am doing the android tutorial and in the Adding Actions Buttons part, openSearch() and openSettings() are undefined. So I made them as private voids in the same class. In the switch, though, openSearch(); is apparently unreachable. When I delete that case, the method in the next case is unreachable. Here is my code.
return super.onOptionsItemSelected(item);
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
private void openSettings() {
Toast.makeText(this, "Search button pressed", Toast.LENGTH_SHORT).show();
}
private void openSearch() {
Toast.makeText(this, "Search button pressed", Toast.LENGTH_SHORT).show();
}
You should get rid of return super.onOptionsItemSelected(item); in the top of your method, or the switch will be never reachable
I want to customize the activity back button in action bar, not in hard key back button. I have overriden the onBackPressed() method. It works with my emulator back button, but not with action bar back button.
I want it to happen with action bar. How can I do this?
Here is my code:
#Override
public void onBackPressed() {
Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
return;
}
I have used this toast whether back pressed is working or not but the actual implementation changes like to move back to previous activity. But this is not working with the button present on top of action bar (besides title of the activity).
Please any one could specify me the problem.
I think you want to override the click operation of home button. You can override this functionality like this in your activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(getApplicationContext(),"Back button clicked", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
If you want ActionBar back button behave same way as hardware back button:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
}
return false;
}
Two things to keep in mind that the user can either press back button or press the actionbar home button.
So, if you want to redirect him to the same destination then you can do this.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return false;
}
#Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
startActivity(intent);
finish();
}
This will take the user to the intent pressing either key or the action bar button.
Sorry mine is a late answer, but for anyone else arriving at this page with the same question, I had tried the above:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
...
if (item.getItemId() == android.R.id.home) {
....
}
....
}
but this failed to catch the "Back" button press.
Eventually I found a method that worked for me on https://stackoverflow.com/a/37185334/3697478 which is to override the "onSupportNavigateUp()" as I am using the actionbar from the "AppCompatActivity" support library. (There is an equivalent "onNavigateUp()" for the newer actionbar/toolbar library.)
#Override
public boolean onSupportNavigateUp(){
finish();
return true;
}
and I removed the "android:parentActivityName=".MainActivity" section from the manifest file.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
break;
}
return true;
}
(1) Add Parent activity for your child activity (AndroidManifest.xml)
<activity
android:name=".ParentActivity" />
(2) override the onSupportNavigateUp method inside the child activity
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return false;
}
I have achieved this, by using simply two steps,
Step 1: Go to AndroidManifest.xml and in the add the parameter in tag - android:parentActivityName=".home.HomeActivity"
example :
<activity
android:name=".home.ActivityDetail"
android:parentActivityName=".home.HomeActivity"
android:screenOrientation="portrait" />
Step 2: in ActivityDetail add your action for previous page/activity
example :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
If you want to return to the previous instance of an Activity by pressing of ActionBar home button, without recreating it, you can override getParentActivityIntent method to use the one from the back stack:
#TargetApi(Build.VERSION_CODES.JELLY_BEAN)
#Override
public Intent getParentActivityIntent() {
return super.getParentActivityIntent().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
}
EDIT:
Also you can achieve the same result by
setting the launchMode of your parent activity to singleTop.
So setandroid:launchMode="singleTop" to parent activity in your manifest.
Or you can use flag FLAG_ACTIVITY_CLEAR_TOP with the UP intent.
reference: Providing Up Navigation
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
return true;
}
//noinspection SimplifiableIfStatement
if (id == R.id.signIn) {
return true;
}
return super.onOptionsItemSelected(item);
}
///////////////////
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
There are several ways how to set up back button in bar:
1) method .setDisplayHomeAsUpEnabled(true); will do it, and then you can simply override android.R.id.home
2) adding <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="my.package.parrent" /> in Android Manifest, but in this case you can not override android.R.id.home in OnOptionsMenuSelected.
.. for those who wonder why it doesn't work for them...