How to add day night switcher in toolbar menu - java

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

Related

Bottomnavigationbar cant setcheckable to false but true works fine

I have two items, if i alternate between them it will uncheck the other item. But if i click once on a item i setChecked(true); but when i click again i want it to setChecked(false); but that doesnt uncheck the item. ive tried setCheckable(false); and that unchecks the item but then i cant check it again even if i write setCheckable(true) before i setChecked(true).
So the code works for one alteration then since its always checked after that the if statement doesnt work anymore. I can add more code if needed, but feels like im missing some small part.
This is the code
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.page_1:
item.setChecked(true);
break;
case R.id.page_2:
if(item.isChecked()) {
item.setChecked(false);
brushComponent_layout.setVisibility(View.INVISIBLE);
}
else {
item.setChecked(true);
brushComponent_layout.setVisibility(View.VISIBLE);
}
break;
default:
break;
}
return false;
}
});
I did a workaround that gave me the same functionality im looking for, still very strange.
instead of checking the if its checkable or checked and unchecking and that not working. I looked at the visability of the layout and just left the item checked all the time. Visually its a bit ugly but it works which is more important.
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.page_1:
item.setChecked(true);
break;
case R.id.page_2:
if(brushComponent_layout.getVisibility() == View.VISIBLE) {
brushComponent_layout.setVisibility(View.INVISIBLE);
}
else {
item.setChecked(true);
brushComponent_layout.setVisibility(View.VISIBLE);
}
break;
default:
break;
}
return true;
}
});

How do i open an edit text popup on actionbar menu click

I am trying to display a popup edit text when "action_guess" is clicked. i am not sure on how to go about this. here is my code:
public MyMenuItemClickListener() {
}
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.action_guess:
Toast.makeText(mContext, "guess code", Toast.LENGTH_SHORT).show();
return true;
default:
}
return false;
}
If you want an EditText in a Dialog, you can follow this example.
https://www.mkyong.com/android/android-prompt-user-input-dialog-example/
If you want it appear in the ActionBar, you can overlay the EditText inside the ActionBar layout and show/hide with setVisibility().

Action Bar Launch-Intent Behaviour with Multiple Menu Items

I am making a simple application that allows the user to take a photo using the dispatchTakePictureIntent() that saves the full-size photo; indicated here; http://developer.android.com/training/camera/photobasics.html#TaskCaptureIntent. And launched using a Camera icon from the action bar. The user then takes a photo presses ok and their picture appears in a list created using a class extending BaseAdapter
public boolean onOptionsItemSelected(MenuItem item) {
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
dispatchTakePictureIntent();
}
return super.onOptionsItemSelected(item);
}
If I launch the intent as above then everything seems to work fine but I have no response to the delete all button (clicking it would ofc just start the camera intent)
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_photo : {
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
dispatchTakePictureIntent();
}
}
case (R.id.action_delete) : {
deleteAllPhotos();
}
}
return super.onOptionsItemSelected(item);
}
However when setup like this I can delete the photos, but the previously taken photo dissapears every time I click the camera icon to take a new photo
Is there any subtle change in lifecycle or anything introduced by this difference? As I am struggling to find how it can cause this different behaviour.
Thanks for any input!
You're missing a break or return statement after your case, so it launches the intent and then deletes all of your previous photos since case statements fall thru to the next one if not told to break.
Try:
switch(item.getItemId()) {
case R.id.action_photo:
...
break;
case R.id.action_delete:
...
break;
}

how to override action bar back button in android?

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...

repaint the frame that contain GlsurfaceView

I have a GLSurfaceView, when the user click a button, it will draw some triangles with default color.
Then when the user selects a color from my inflater menu, it will re-draw the triangles with that particular color.
It works in a weird way, at first it does draw the triangles, and when i select a different color, it doesn't repaint with different color. However, when i turn off my phone and turn back on, the color changed.
Here is my code:
//------------------------------------------------------------------------------------------------
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.issm_menu,menu);
return true;
}
//------------------------------------------------------------------------------------------------
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_about:
Toast.makeText(ISSM.this, "ISSM Application", Toast.LENGTH_SHORT).show();
return true;
case R.id.cl_autumn:
colorMap.setAutumn();
drawFigure();
return true;
case R.id.cl_bone:
colorMap.setBone();
drawFigure();
return true;
default:
return true;
}
public void onClick(View view)
{
this.fillBuffer();
drawFigure();
}
//----------------------------------------------------------------------------------
public void drawFigure()
{
mGLView = new MyGLSurfaceView(this, buff, size, colorMap);;
frame.addView(mGLView);
}
I want that as soon as i hit the color in my menu, the color should change, i do not want to turn off and turn on (by the way, not completely power off, just like sleep and wake up)
Solved it, I need to called frame.removeView() first then add another view.

Categories