How to Make Your Action Bar Appear Again

This lesson describes how to hide the status bar on unlike versions of Android. Hiding the status bar (and optionally, the navigation bar) lets the content use more of the display space, thereby providing a more than immersive user experience.

Figure 1 shows an app with a visible condition bar:

system bars

Figure one. Visible status bar.

Figure two shows an app with a hidden status bar. Note that the action bar is hidden too. You should never evidence the action bar without the status bar.

system bars

Figure 2. Hidden status bar.

Hide the Condition Bar on Android four.0 and Lower

You can hide the status bar on Android iv.0 (API level 14) and lower by setting WindowManager flags. You can practise this programmatically or by setting an activeness theme in your app'southward manifest file. Setting an activity theme in your app's manifest file is the preferred arroyo if the status bar should always remain hidden in your app (though strictly speaking, you lot could programmatically override the theme if yous wanted to). For case:

<awarding     ...     android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" >     ... </application>        

The advantages of using an activity theme are as follows:

  • It's easier to maintain and less error-prone than setting a flag programmatically.
  • It results in smoother UI transitions, because the system has the data it needs to return your UI before instantiating your app'south primary activity.

Alternatively, you can programmatically ready WindowManager flags. This approach makes it easier to hide and evidence the condition bar every bit the user interacts with your app:

Kotlin

class MainActivity : Activity() {      override fun onCreate(savedInstanceState: Package?) {         super.onCreate(savedInstanceState)         // If the Android version is lower than Jellybean, use this telephone call to hide         // the condition bar.         if (Build.VERSION.SDK_INT < xvi) {             window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,                     WindowManager.LayoutParams.FLAG_FULLSCREEN)         }         setContentView(R.layout.activity_main)     }     ... }            

Java

public class MainActivity extends Activity {      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         // If the Android version is lower than Jellybean, use this call to hide         // the condition bar.         if (Build.VERSION.SDK_INT < 16) {             getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,                     WindowManager.LayoutParams.FLAG_FULLSCREEN);         }         setContentView(R.layout.activity_main);     }     ... }            

When you set WindowManager flags (whether through an activity theme or programmatically), the flags remain in event unless your app clears them.

You tin can use FLAG_LAYOUT_IN_SCREEN to set up your activity layout to utilise the same screen expanse that'south bachelor when you lot've enabled FLAG_FULLSCREEN. This prevents your content from resizing when the status bar hides and shows.

Hibernate the Status Bar on Android iv.one and Higher

You can hide the status bar on Android 4.1 (API level 16) and higher by using setSystemUiVisibility(). setSystemUiVisibility() sets UI flags at the individual view level; these settings are aggregated to the window level. Using setSystemUiVisibility() to set UI flags gives you more granular control over the arrangement confined than using WindowManager flags. This snippet hides the status bar:

Kotlin

// Hide the status bar. window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN // Call up that yous should never show the action bar if the // status bar is subconscious, so hibernate that likewise if necessary. actionBar?.hide()            

Java

View decorView = getWindow().getDecorView(); // Hibernate the status bar. int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN; decorView.setSystemUiVisibility(uiOptions); // Remember that you lot should never show the activeness bar if the // status bar is subconscious, so hide that too if necessary. ActionBar actionBar = getActionBar(); actionBar.hibernate();            

Annotation the following:

  • Once UI flags have been cleared (for example, past navigating away from the activity), your app needs to reset them if y'all want to hide the confined once again. See Responding to UI Visibility Changes for a discussion of how to listen for UI visibility changes then that your app can respond accordingly.
  • Where y'all set the UI flags makes a difference. If you lot hibernate the organization confined in your activity's onCreate() method and the user presses Habitation, the arrangement bars will reappear. When the user reopens the activity, onCreate() won't get chosen, so the system confined will remain visible. If y'all desire organisation UI changes to persist equally the user navigates in and out of your activity, set UI flags in onResume() or onWindowFocusChanged().
  • The method setSystemUiVisibility() only has an effect if the view you telephone call it from is visible.
  • Navigating abroad from the view causes flags set with setSystemUiVisibility() to be cleared.

Brand Content Announced Behind the Status Bar

On Android 4.1 and higher, you tin can set your application'due south content to announced backside the status bar, and so that the content doesn't resize as the status bar hides and shows. To practise this, use SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN. You lot may also need to use SYSTEM_UI_FLAG_LAYOUT_STABLE to assistance your app maintain a stable layout.

When you use this arroyo, information technology becomes your responsibility to ensure that critical parts of your app'south UI (for example, the built-in controls in a Maps awarding) don't end upward getting covered by system bars. This could make your app unusable. In most cases you can handle this by adding the android:fitsSystemWindows attribute to your XML layout file, set to true. This adjusts the padding of the parent ViewGroup to leave space for the system windows. This is sufficient for most applications.

In some cases, notwithstanding, you may need to change the default padding to get the desired layout for your app. To directly manipulate how your content lays out relative to the system bars (which occupy a infinite known equally the window'due south "content insets"), override fitSystemWindows(Rect insets). The fitSystemWindows() method is called by the view hierarchy when the content insets for a window have inverse, to allow the window to adjust its content appropriately. By overriding this method you tin handle the insets (and hence your app's layout) however you desire.

jacksonthinty1951.blogspot.com

Source: https://developer.android.com/training/system-ui/status

0 Response to "How to Make Your Action Bar Appear Again"

ارسال یک نظر

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel