Reputation: 380
In all activities in my project, I am hiding the status bar and doing more of these activity-related things (that all my activities have in common). There is the function I prepared for hiding the status bar:
void hideStatusBar()
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
How do I perform this function (and not only this one) in every activity I have, without copy-pasting the function to every activity (to not violate DRY)?
Upvotes: 1
Views: 55
Reputation: 1637
private void showSystemUI() {
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
private void hideSystemUI() {
// Enables regular immersive mode.
// For "lean back" mode, remove SYSTEM_UI_FLAG_IMMERSIVE.
// Or for "sticky immersive," replace it with SYSTEM_UI_FLAG_IMMERSIVE
View decorView = getWindow().getDecorView();
decorView.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_IMMERSIVE
// Set the content to appear under the system bars so that the
// content doesn't resize when the system bars hide and show.
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
// Hide the nav bar and status bar
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN);
}
Upvotes: 0
Reputation: 109
If all your changes are related to the visual aspect of your activities, you could specify everything in you AppTheme on styles.xml
To hide the status bar, for example, you would use:
<item name="android:windowActionBar">false</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
Then all activities using your theme will look the same
Upvotes: 0
Reputation: 23664
You can create this as a static utility method.
public final class ActivityUtils {
private ActivityUtils() {}
public static void hideStatusBar(final Activity activity) {
activity.requestWindowFeature(Window.FEATURE_NO_TITLE);
activity.getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
Usage from the activity would look like ActivityUtils.hideStatusBar(this)
Alternatively you can extract this method signature to an interface, and inject this logic into your activity. This will be helpful if you ever want some activities to use slightly different versions of status hiding.
public interface StatusBarHider {
void hideStatusBar(final Activity activity);
}
public class DefaultStatusBarHider implements StatusBarHider {
@Override
public void hideStatusBar(final Activity activity) {
activity.requestWindowFeature(Window.FEATURE_NO_TITLE);
activity.getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
// ex; Possibly Guice or Dagger for injection
public class MainActivity extends Activity {
@Inject
private StatusBarHider hider;
...
}
Upvotes: 1