Android get screen size without action bar

An action bar with ShareActionProvider expanded to show share targets. Similar to an action view , an action provider replaces an action button with a customized layout. However, unlike an action view, an action provider takes control of all the action's behaviors and an action provider can display a submenu when pressed. You can build your own action provider by extending the ActionProvider class, but Android provides some pre-built action providers such as ShareActionProvider , which facilitates a "share" action by showing a list of possible apps for sharing directly in the action bar as shown in figure 6.

Because each ActionProvider class defines its own action behaviors, you don't need to listen for the action in the onOptionsItemSelected method.

Add Action Buttons

If necessary though, you can still listen for the click event in the onOptionsItemSelected method in case you need to simultaneously perform another action. But be sure to return false so that the the action provider still receives the onPerformDefaultAction callback to perform its intended action. However, if the action provider provides a submenu of actions, then your activity does not receive a call to onOptionsItemSelected when the user opens the list or selects one of the submenu items.

Now the action provider takes control of the action item and handles both its appearance and behavior. But you must still provide a title for the item to be used when it appears in the action overflow. The only thing left to do is define the Intent you want to use for sharing. You should call setShareIntent once during onCreateOptionsMenu to initialize the share action, but because the user context might change, you must update the intent any time the shareable content changes by again calling setShareIntent.

The ShareActionProvider now handles all user interaction with the item and you do not need to handle click events from the onOptionsItemSelected callback method. By default, the ShareActionProvider retains a ranking for each share target based on how often the user selects each one. The share targets used more frequently appear at the top of the drop-down list and the target used most often appears directly in the action bar as the default share target.

If you use the ShareActionProvider or an extension of it for only one type of action, then you should continue to use this default history file and there's nothing you need to do. However, if you use ShareActionProvider or an extension of it for multiple actions with semantically different meanings, then each ShareActionProvider should specify its own history file in order to maintain its own history. Note: Although the ShareActionProvider ranks share targets based on frequency of use, the behavior is extensible and extensions of ShareActionProvider can perform different behaviors and ranking based on the history file if appropriate.

Creating your own action provider allows you to re-use and manage dynamic action item behaviors in a self-contained module, rather than handle action item transformations and behaviors in your fragment or activity code. As shown in the previous section, Android already provides an implementation of ActionProvider for share actions: the ShareActionProvider.

To create your own action provider for a different action, simply extend the ActionProvider class and implement its callback methods as appropriate. Most importantly, you should implement the following:. However, if your action provider provides a submenu, through the onPrepareSubMenu callback, then the submenu appears even when the action provider is placed in the action overflow. Thus, onPerformDefaultAction is never called when there is a submenu. Note: An activity or a fragment that implements onOptionsItemSelected can override the action provider's default behavior unless it uses a submenu by handling the item-selected event and returning true , in which case, the system does not call onPerformDefaultAction.

Tabs in the action bar make it easy for users to explore and switch between different views in your app. The tabs provided by the ActionBar are ideal because they adapt to different screen sizes. For example, when the screen is wide enough the tabs appear in the action bar alongside the action buttons such as when on a tablet, shown in figure 7 , while when on a narrow screen they appear in a separate bar known as the "stacked action bar", shown in figure 8.

In some cases, the Android system will instead show your tab items as a drop-down list to ensure the best fit in the action bar.


  • download whatsapp for free nokia e63;
  • Get actual screen size for the application layout dynamically.
  • Status Bar Always Hidden on Android.

To get started, your layout must include a ViewGroup in which you place each Fragment associated with a tab. Be sure the ViewGroup has a resource ID so you can reference it from your code and swap the tabs within it. Alternatively, if the tab content will fill the activity layout, then your activity doesn't need a layout at all you don't even need to call setContentView.

Separating the folders

Instead, you can place each fragment in the default root view, which you can refer to with the android. Notice that the ActionBar. TabListener callback methods don't specify which fragment is associated with the tab, but merely which ActionBar. Tab was selected. You must define your own association between each ActionBar. Tab and the appropriate Fragment that it represents. There are several ways you can define the association, depending on your design.

For example, here's how you might implement the ActionBar. TabListener such that each tab uses its own instance of the listener:. Caution: You must not call commit for the fragment transaction in each of these callbacks—the system calls it for you and it may throw an exception if you call it yourself.

You also cannot add these fragment transactions to the back stack. In this example, the listener simply attaches attach a fragment to the activity layout—or if not instantiated, creates the fragment and adds add it to the layout as a child of the android. All that remains is to create each ActionBar. Tab and add it to the ActionBar. If your activity stops, you should retain the currently selected tab with the saved instance state so you can open the appropriate tab when the user returns. When it's time to save the state, you can query the currently selected tab with getSelectedNavigationIndex.

This returns the index position of the selected tab. Caution: It's important that you save the state of each fragment so when users switch fragments with the tabs and then return to a previous fragment, it looks the way it did when they left. Some of the state is saved by default, but you may need to manually save state for customized views. For information about saving the state of your fragment, see the Fragments API guide. Note: The above implementation for ActionBar. TabListener is one of several possible techniques.

Another popular option is to use ViewPager to manage the fragments so users can also use a swipe gesture to switch tabs. In this case, you simply tell the ViewPager the current tab position in the onTabSelected callback. For more information, read Creating Swipe Views with Tabs. As another mode of navigation or filtering for your activity, the action bar offers a built in drop-down list also known as a "spinner".

For example, the drop-down list can offer different modes by which content in the activity is sorted. Using the drop-down list is useful when changing the content is important but not necessarily a frequent occurrence. In cases where switching the content is more frequent, you should use navigation tabs instead.

Hide the status bar

This method takes your SpinnerAdapter and ActionBar. This procedure is relatively short, but implementing the SpinnerAdapter and ActionBar. OnNavigationListener is where most of the work is done. There are many ways you can implement these to define the functionality for your drop-down navigation and implementing various types of SpinnerAdapter is beyond the scope of this document you should refer to the SpinnerAdapter class reference for more information. However, below is an example for a SpinnerAdapter and ActionBar.

OnNavigationListener to get you started click the title to reveal the sample. SpinnerAdapter is an adapter that provides data for a spinner widget, such as the drop-down list in the action bar. SpinnerAdapter is an interface that you can implement, but Android includes some useful implementations that you can extend, such as ArrayAdapter and SimpleCursorAdapter. For example, here's an easy way to create a SpinnerAdapter by using ArrayAdapter implementation, which uses a string array as the data source:.

The createFromResource method takes three parameters: the application Context , the resource ID for the string array, and the layout to use for each list item. A string array defined in a resource looks like this:. The ArrayAdapter returned by createFromResource is complete and ready for you to pass it to setListNavigationCallbacks in step 4 from above. Before you do, though, you need to create the OnNavigationListener. Your implementation of ActionBar. OnNavigationListener is where you handle fragment changes or other modifications to your activity when the user selects an item from the drop-down list.

There's only one callback method to implement in the listener: onNavigationItemSelected. Here's an example that instantiates an anonymous implementation of OnNavigationListener , which inserts a Fragment into the layout container identified by R. In this example, when the user selects an item from the drop-down list, a fragment is added to the layout replacing the current fragment in the R. The fragment added is given a tag that uniquely identifies it, which is the same string used to identify the fragment in the drop-down list. Here's a look at the ListContentFragment class that defines each fragment in this example:.

How To Get Height And Width Of Relativelayout In Android

If you want to implement a visual design that represents your app's brand, the action bar allows you to customize each detail of its appearance, including the action bar color, text colors, button styles, and more. To do so, you need to use Android's style and theme framework to restyle the action bar using special style properties. Caution: For all background drawables you provide, be sure to use Nine-Patch drawables to allow stretching.

The nine-patch image should be smaller than 40dp tall and 30dp wide. The default for this style for this is Widget. ActionBar , which is what you should use as the parent style. ActionButton , which is what you should use as the parent style.

Overflow , which is what you should use as the parent style. The default for this style for this is TextAppearance. Title , which is what you should use as the parent style. Normally, the action bar requires its own space on the screen and your activity layout fills in what's left over.

How to Change Action Bar Background Color and Title color - Android App Development video#09

When the action bar is in overlay mode, your activity layout uses all the available space and the system draws the action bar on top. Overlay mode can be useful if you want your content to keep a fixed size and position when the action bar is hidden and shown. You might also like to use it purely as a visual effect, because you can use a semi-transparent background for the action bar so the user can still see some of your activity layout behind the action bar.