OnCreateOptionsMenu not called when Style set to a Dialog Theme - android

I have an activity in Android. Specifically, I am using Xamarin.Android, formerly monodroid. I have an activity. When it is running on a phone (small screen device), we want the activity to be full screen. When it runs on a tablet, we want the activity to have a "border" around similar to a dialog. I do a programmatic check to get the screen dimensions and then determine if we are on a tablet or phone. If we are on a phone, I call an activity's SetTheme method and pass in a dialog theme. I have tried several dialog themes with no difference. My menu items do not show. I track this down and my overridden OnCreateOptionsMenu method is not called. I know because I set a breakpoint in the first line of my activty's OnCreateOptionsMenu method and the breakpoint is not hit.
The code that I am using to set the theme is:
this.SetTheme (Android.Resource.Style.ThemeDialog);
I've also tried this from the Activty's theme attributes, but I am getting the same result.
I'm looking for any ideas, thoughts, whatever.
Thanks for your time.
Wally

By default the dialog theme does not have an actionbar. No reason to create menu items without it (on newer android versions).
Call RequestWindowFeature (WindowFeatures.ActionBar); in onCreate to get an actionbar in a dialog themed activity. Then OnCreateOptionsMenu will be called

Related

Android change previous activity theme

I want change hole application theme and i can do it and handle this by saving in sharedPreference and set theme in my BaseActivity.
Changing theme is in separate activity, but when i go back to previous activity it has not updated theme.but if i close the application and reopen or move to unopened activity, theme changed.
I tried to set theme in onResume but nothing changed.
Theme can only be set before setContentView() is called in your Activity. You can try something like calling recreate() after updating your theme, but I would advise against that.
Here are some popular theming libraries that handle these things for you:
Aesthetic
Colorful
Magica Sakura

Android ActoinBar homeAsUp only works once

I'm working on an app that only uses a single Activity and switches out fragments as they are needed with the navigation drawer.
Now we want to navigate back from one of these fragments by using the homeAsUp button in the ActionBar.
I have followed all the steps to set the button up. From disabling the navigation drawer setDrawerIndicatorEnabled(false) and calling setDisplayHomeAsUpEnabled(true) in the fragment's onCreateView().
I also set setHomeButtonEnabled(true) in the MainActivity's onCreate() however because the app is already in the MainActivity, we cannot specify a Parent Activity.
Whenever I run a fresh install of the app, the homeAsUp button works and is registered in the onBackPressed(), not onOptionsItemSelected() method. However, when I close the app and run it again the button does not even register clicks.
In onBackPressed() I check a few conditions, but it does not block the button press.
In onOptionsItemSelected() I check for android.R.id.home.
Unfortunately, I cannot post the code.
This post describes what I'm trying to achieve: Switching between Android Navigation Drawer image and Up caret when using fragments
I managed to fix the issue I was experiencing. It was a very simple mistake.
Because I'm not the original author of this code, I went through the MainActivity thoroughly.
As it turns out the original author called setDisplayHomeAsUpEnabled in the onCreate function (which is extremely long), but near the end he also called setSupportActionBar, which made the first call of setDisplayHomeAsUpEnabled useless. Moving setDisplayHomeAsUpEnabled below setSupportActionBar fixed my problem.
If you did everything correctly, make sure that your code is written in the correct order.
Furthermore, if you use custom toolbars in other fragments, remember to set your original Support Action Bar by calling setSupportActionBar(toolbar.find(this)) in onResume of your MainActivity.

setTheme works but only after loading theme from manifest

I'm trying to set the theme of my Activity during runtime, choosing one of a number of themes. I want the chosen theme to display immediately on startup of the Activity.
In the <application> part of my manifest I have set a default theme with android:theme="#style/AppTheme". And then in my onCreate() I use setTheme(R.style.DarkAppTheme) to set the theme to a user-selected theme (replacing DarkAppTheme with the selected theme).
And based on research it seems that setTheme() should go before onCreate() and before setContentView(), which I do.
BUT although this works to display the Activity in the user selected theme, the Activity first loads with what looks like the default theme, and then after a short delay the correct theme loads.
If I set the user selected theme directly in the manifest, that loads immediately as I want, but of course that is hardcoded and I want to change this dynamically based on a shared preference.
How do I avoid the visible changeover? I want the user selected theme to be displayed right from the start.
Thanks.
The Activity first loads with what looks like the default theme, and then after a short delay the correct theme loads... How do I avoid the visible changeover?
There are 2 parts to an Activity 'Enter/Opening' window animation, when your app is first launched from the icon on the Home Screen:
The 'dark gray rectangle appear' animation. This is the initial blank screen that the system process draws when launching the app (source). It's also known as the "theme preview" screen or "splash screen". It can be white if your app uses a Light theme.
The 'fade in (or circular reveal) of the view layout'. This is the animation of your view layout appearing on top of the dark gray rectangle. It happens after part 1.
Part 1 is what you identified as "what looks like the default theme". You can disable this first part with the following item in your Activity/App style:
<item name="android:windowDisablePreview">true</item>
This will prevent the 'dark gray rectangle appear' animation, and only allow the 'reveal of the view layout', to therefore avoid the visible changeover or flicker. But there are caveats:
You have to ensure that your Activity launches quickly, because there will be no visual animation feedback for the user until your layout is fully loaded. That's why the theme preview is normally on by default.
It causes strange bugs on context menus: For any PopupWindow, the 'Enter' animation will no longer happen, and it will just display instantly ('Exit' animations are not affected). This also applies to system PopupWindows like the overflow menu list, and the drop-down list of an AutoCompleteTextView. This bug happens on Android 4/5/6, but not on Android 7/8. More info here.
Documentation of windowDisablePreview:
Flag allowing you to disable the splash screen for a window. The default value is false; if set to true, the system can never use the window's theme to show a splash screen preview before your actual instance is shown to the user.
Further info:
How to set a theme to whole application in code, but not in the Manifest?
Android - Disable dummy starting window of application
The theme on AndroidManifest just appears if your Activity take too long to load. You can try to tunning up Activity load and remove android:theme="#style/AppTheme" from AndroidManifest or even set a compromise between these two use.
I hope it helps you \o/

How to change themes in android smoothly?

In my application i am using a splash screen for the application start up with the theme #android:style/Theme.Black.NoTitleBar.Fullscreen.
After 2 seconds the app goes on to the main activity which uses this theme: #style/Theme.Sherlock.Light.DarkActionBar.
One is in full screen, the other one is not. The transition between both is not smooth and it takes a while (~1 seconds) for the main activity to adjust to the status bar. Is there some trick to avoid this?
As far as I know, there is no api level solution for this. I suggest Using TransitionDrawable for example changing background color and/or PropertyAimation for changing view's properties like background color or text color. Then you can call setTheme() function of activity to apply your new theme. be careful that setTheme() must be called in onCreate() function before setContentView.

Change focus of activities in android

I am using a dialog themed activity in android to show a popup from a application context. The dialog has a transparent theme, but the issue is that I want the underlying activity to have focus and not the popup though the popup must be visble. How do I achieve this in android?
I think you would have to use a Fragment and just make it look like a popup dialog.
You can not switch focus between activities. There's only one activity "on focus" at the time, and is the one that is being displayed at that point. The transparent background doesn't mean you can access the activity below.
If i don't get it wrong, you want to be able to interact with the activity's controls while having a "Dialog" on the screen. Any sort of Dialog class from Android will not help, since they take the focus away. Not really sure about PopupWindow, but i'm guessing will be the same thing as the documentation says "that appears on top of the current activity."
You are going to have to create a custom dialog using a RelativeLayout/FrameLayout within your activity.

Categories

Resources