I just worked through the first introductory application for Android programming and noticed some strange behavior:
When the second Activity was generated it received the line:
getActionBar().setDisplayHomeAsUpEnabled(true);
in the onCreate function. As the tutorial points out, this line requires at least API level 11 and a guard for that and #SuppressLint("NewApi") should be added like so:
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Make sure we're running on Honeycomb or higher to use ActionBar APIs
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
The strange thing is that ignoring this advice and just hitting run worked fine at first but as soon as I made changes to the code it didn't work anymore and I got errors.
So when is lint actually active?
Related
Hello I am using this AppCompatDelegate to change between day/night themes
I have 2 activities A& B
this code called from activity B
it should recreating activity B & A with the chosen style
here is my code
applyNight.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!isNight) {
SharedPrefrencesMethods.savePreferences(this, getString(R.string.night_key), true);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
SharedPrefrencesMethods.savePreferences(this, getString(R.string.night_key), false);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
});
I tested it on android 7 & 6 it's working fine i.e when changing mode in activity B and press back activity A recreating with the new theme.
When trying it on android 9 it's changing the activity B only and not affecting it's parent activity A.
I was having that problem too, and then took the advice of Chris Banes in Google's official Android Developers blog https://medium.com/androiddevelopers/appcompat-v23-2-daynight-d10f90c83e94 to set setDefaultNightMode in the app's application class in the first place, so I created a class EcwgApplication extending Application as he shows, and added android:name=".EcwgApplication" in the application section of the manifest. I also put my method for switching themes in the application class as well, that my settings activity can call when the user changes the theme setting (in addition to updating SharedPreferences with the change before calling it), so it looks like this:
public class EcwgApplication extends Application {
public void onCreate() {
super.onCreate();
int selectedDarkLightTheme = PreferenceManager.getDefaultSharedPreferences(this).getInt(getString(R.string.preferences_dark_light_mode_selected_key), AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
AppCompatDelegate.setDefaultNightMode(selectedDarkLightTheme);
}
public static void setDarkLightTheme(int selectedDarkLightTheme) {
AppCompatDelegate.setDefaultNightMode(selectedDarkLightTheme);
}
}
This worked fine with Android OS versions 24 through 29, but with 21 (the lowest version this app is supporting) through 23 I would get a black screen on returning to the first activity, and while rotating the screen would fix that, it also made clear that UI state was not being saved. So I changed the StartActivity for the Settings screen to StartActivityForResult, and in onActivityResult, check if the version number <= 23, and if so, do this.recreate().
I need to keep doing more testing, but at least so far everything seems to be working great.
I am adding a Up button to my Android application. The app's minimum SDK is 14, and I am testing it with an HTC phone on SDK version 15.
The activity is a subclass of android.app.Activity (not ActionBarActivity from the support package).
ActionBar style display options includes the homeAsUp flag, and I am able to see the standard arrow. However, clicking on the logo does nothing.
I have connected the debugger, and I am able to see that the onOptionsItemSelected method is not called at all. This cannot be because of misspelt name, because other menu items (e.g., Settings) do work (and I can see in the debugger that onOptionsItemSelected method gets called).
The parentActivityName and meta-data PARENT_ACTIVITY are set correctly (although I believe this would only matter if the method got called).
Is there anything I am missing? And how do I get the up button to work?
I encountered the same issue "onOptionsItemSelected" not called when using the ActionBarDrawerToggle it seems like the solution is setting this listener - it is called when the Up button is clicked.
drawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish(); // or any other code you want to run here
}
});
I am using API 10 GingerBread
I am not using a layout, although I did try this with a layout and it still doesn't work. Works fine with a normal Activity subclass, I don't see what the problem is.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_LEFT_ICON);
setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.ic_launcher);
For what it is worth, using FEATURE_NO_TITLE works fine. What gives? Anybody have any suggestions?
Try this code...
requestWindowFeature(Window.FEATURE_LEFT_ICON);
setContentView(R.layout.main); //or whatever layout is shows
setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.ic_launcher);
I have this problem with the Android ActionBarCompat project: On emulators with Android 4.0 the click on the app icon doesn't cause any onOptionsItemSelected event, whereas it works on all other OS versions.
Any input is greatly appreciated!
Are you seeing any touch feedback from the app icon? (Does it glow when you press it?)
Since many activities do not use the action bar home button, in apps that target API 14+ running on Android 4.0 it is disabled by default. (This is so that users don't try pressing it, see it glow, and wonder why nothing happened.) Apps that want to use this should call ActionBar#setHomeButtonEnabled(true).
We should probably revise the ActionBarCompat sample to surface this more clearly. One simple way to get you up and running would be to modify ActionBarHelperICS.java and add the following:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mActivity.getActionBar().setHomeButtonEnabled(true);
}
In an app where you want more control over turning this on and off you would want to make further changes.
I had this problem as well. This code did the trick for me:
public void onCreate(Bundle savedInstanceState) {
...
if (android.os.Build.VERSION.SDK_INT >= 11) {
//noinspection ConstantConditions
getActionBar().setHomeButtonEnabled(true);
} else {
getSupportActionBar().setHomeButtonEnabled(true);
}
}
Some extra info: minSdkVersion="7" targetSdkVersion="18". This is the LAUNCHER activity of my project, so it has no parent activity. Using setDisplayHomeAsUpEnabled(true) in other activities worked just fine.
I found bunch of samples how to remove title/notification bar on Android. However, it's still not a full screen.
I still see a panel on my table with search, home, back and some other button.
Do you have any ideas how to hide this panel?
Generally speaking, I want kind of kiosk mode.
Use this code in onCreate of activity:
getWindow().setFlags(LayoutParams.FLAG_FULLSCREEN, LayoutParams.FLAG_FULLSCREEN);
Sample code for a fullscreen activity:
public class HellofullActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
}
where you request that the window has not the green title bar and take full screen.
The core problem was device specific. I was doing development for Archos and Archos has it's own bar, which isn't not removed by FLAG_FULLSCREEN and FEATURE_NO_TITLE