Different menu items for Activity in debug and release mode - android

For the application that I am developing, there are some menu items that are helpful for debugging the app (something like resetting counts and stuff).
Is there a way (a directive in xml file or else) to tell android to show/hide certain menu items depending on the app being in debug mode or not?
The only thing I know is the following code, that we can do in the Activity itself, I wonder if it can be used to show/hide menu items:
boolean isDebugBuild = (0 != ( //Check if the app is in debug mode
getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE));
Or maybe is there a way to do this in onCreateOptionsMenu()

Add this in your AndroidManifest application tag
android:debuggable=true
and in onCreateOptionsMenu check
if (BuildConfig.DEBUG) {
//true for debuggable mode
} else {
}

Related

How to get ActionMode.Menu working properly in OnActionModeStarted on Android Xamarin Forms?

I have the following code to introduce menu items into the system context menu upon text selection on a Label.
public override void OnActionModeStarted(ActionMode mode)
{
IMenu menu = mode.Menu;
menu.Add("MItem1");
menu.Add("MItem2");
menu.Add("MItem3");
menu.GetItem(0).SetOnMenuItemClickListener(new MenuItemOnMenuItemClickListener(this, 0));
menu.GetItem(1).SetOnMenuItemClickListener(new MenuItemOnMenuItemClickListener(this, 1));
menu.GetItem(2).SetOnMenuItemClickListener(new MenuItemOnMenuItemClickListener(this, 2));
//test code -> this works fine
menu.Add(0, 999, 0, "test");
//item is found, item.IsEnabled == true, item.IsVisible == true
IMenuItem item = menu.FindItem(999);
base.OnActionModeStarted(mode);
}
It works fine on a Lenovo device and was previously working on a Samsung device, but over time due to, I suspect, one or two Samsung system updates, the method no longer has any effect.
I've run the code through the debugger and the code can be stepped through line by line, but the system menu is completed unaffected by the added menuitems and continues as if my code hasn't been called at all.
Any ideas?
I have both a workaround and a solution.
Workaround
I added:
mode.Hide(1);
to the above code. It helps refresh the menu and the correct menu items appear.
Solution
I did another Samsung OS upgrade and the problem has disappeared. Seems like it was an OS problem after all.

Night Mode state not detected in app - even when toggled ON

If night mode is ON, I want to pick a different layout in my activity.
What I have done:
added separate layout folders for night and notnight as follows:
res
-----layout-night
----------my_layout.xml
-----layout-notnight
----------my_layout.xml
Now, when I toggle night mode and reopen the app, I was hoping the layout would change, but it doesn't.
Is there something extra required for this to work?
Is there a permission i need to add for this to work? If yes, which one?
Not looking for an alternate approach. My use case specifically needs a layout to be picked based on night mode status. I'm trying to figure out why the right layout doesn't get picked automatically, when the folders are in place.
UPDATE : Narrowed down problem. The layout layout-notnight gets picked always. Hence, folder structure is working fine. App is not being able to detect the night mode. (Night mode is toggled ON on the device- tried toggling on and off it works on device but app wont detect)
So question now is:
Is a permission required for it being able to detect the state of night mode? like it needs for detecting wifi state and network state.
You could try using UiModeManager. It provide methods setNightMode() and getNightMode(). Maybe it not solve the problem but this is the way to do it.
The documentation says :
On API 22 and below, changes to the night mode are only effective when
the car or desk mode is enabled on a device. Starting in API 23,
changes to night mode are always effective.
MODE_NIGHT_AUTO automatically switches between night and notnight based on the device's current location and certain other
sensors
And from here(notice the BOLD text):
This can change during the life of your application if night mode is
left in auto mode (default), in which case the mode changes based on
the time of day.
So you can try to adjust the device time to a proper value.
But please also be aware that the night mode detection may as well depend on other certain sensors(which the developer site doesn't say clearly). Guess the light sensor might be taken into consideration.
If you are using a toggle button or checkbox, then you can store a Boolean value in SharedPreferences and can use to that to check whether the user wants night mode or not. This would work even after the app is closed and used again.
CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
SharedPreferences sharedPreferences = getSharedPreferences("MyData", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
editor.putBoolean("dark", isChecked);
editor.commit();
}
});
Then, in the next activity
SharedPreferences preferences = getSharedPreferences("MyData", Context.MODE_PRIVATE);
listView = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> adapter;
if(preferences.getBoolean("dark",false) == true)
adapter = new ArrayAdapter<String>(this,R.layout.list_item_dark,name);
else
adapter = new ArrayAdapter<String>(this,R.layout.list_item,name);
Similarly, you can change layout using this method.

Android : check wether action button (home,back, etc.. ) is inside the screen or not?

i would like to explain in more detail,
some of android device have action button like this,
while some of device have like,
in first case all action button is outside the screen while in second case action button is given bottom of screen(inside the screen)... how can i detect that where the action button is present? i have no idea about it..!!
Check the official Android docs : http://developer.android.com/reference/android/view/KeyCharacterMap.html#deviceHasKey%28int%29
The method deviceHasKey will solve your problem. (Use Keycodes KEYCODE_MENU, KEYCODE_HOME, KEYCODE_BACK).
Hope it helps.
Maybe by doing the following check in your onCreate():
for api 14 and up
boolean PermanentMenuKey = ViewConfiguration.get(this).hasPermanentMenuKey(); // true if physical, false if virtual
for lower api:
boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
boolean hasHomeKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME);
if (hasBackKey && hasHomeKey) {
// no navigation bar, unless it is enabled in the settings
} else {
// 99% sure there's a navigation bar
}
You may get your answer here.
Check for navigation bar
p.s. The formal name of the bar is "Navigation Bar"
https://developer.android.com/training/system-ui/navigation.html
found solution here, but not 100% guarantee to work for all device, some device like HTC, LAVA etc not working with that. Also minApi required 13+ for that...!!

setting button in android application

I develop an android application and put it in a android market. users put comments for me and tell me that setting button doesn't work for my application. I don't know what setting button is and how I can enable it for my application.
can any one help me?
I guess what you mean is the menu button. The problem often is that some devices don't have a physical menu button, thus they cannot access the menu of your app.
Try to use a Holo theme for your app's activities and show an ActionBar. By default, the three dots button for your menu will then be shown in the ActionBar on devices that don't have a physical menu button.
For a consistend UI on all devices, you could show the menu button in your ActionBar permanently, whether the device has a physical menu button or not, using this code:
if ( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) { e.printStackTrace(); }
}
Do you use Eclipse?
Check if you have a method onCreateOptionsMenu in your activity class.
Eclipse creates automatically this method and adds a menu in your activity (depending on how you create the activity class file)

How to always show Android settings button on ActionBar?

How can I always show Android settings button on ActionBar? (4.0+)
I would like to show it even if the device has an hardware button for settings, so it would be same with devices with and without hardware buttons.
This is what I'm talking about: http://oi48.tinypic.com/2j104l0.jpg
There is one awful hack which generally have been answered on many questions. Call this from your onCreate():
private void getOverflowMenu() {
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
if(menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
e.printStackTrace();
}
}
It is highly suggested not to use this hack as sHasPermanentMenuKey field can change anytime. However this has been working for me and others uptil now. See
Android action bar not showing overflow
On a second note, the hardware button is present on the phone for a reason. Just showing the addition overflow menu might confuse your users. Ideally a user would be very much accustomed to using his/her personal phone. So, if the user's phone has a hardware button for overflow menu option, then it need not have the icon on the top of action bar. Having an addition button might confuse users due to difference in behavior in different apps.
Hope that helps.
Edit:
In short, its not recommended as sHasPermanentMenuKey field in the Android code can be changed anytime, which will break your app if not found.

Categories

Resources