CarDrawerActivity

public class CarDrawerActivity
extends AppCompatActivity

java.lang.Object
   ↳ android.content.Context
     ↳ android.content.ContextWrapper
       ↳ android.view.ContextThemeWrapper
         ↳ android.app.Activity
           ↳ androidx.activity.ComponentActivity
             ↳ androidx.fragment.app.FragmentActivity
               ↳ androidx.appcompat.app.AppCompatActivity
                 ↳ androidx.car.drawer.CarDrawerActivity


Common base Activity for car apps that need to present a Drawer.

This Activity manages the overall layout. To use it, sub-classes need to:

This class will take care of drawer toggling and display.

This Activity also exposes the ability to have its toolbar optionally hide if any content in its main view is scrolled. Be default, this ability is turned off. Call setToolbarCollapsible() to enable this behavior. Additionally, a user can set elevation on this toolbar by calling the appropriate setToolbarElevation(float) method. There is elevation on the toolbar by default.

The rootAdapter can implement nested-navigation, in its click-handling, by passing the CarDrawerAdapter for the next level to CarDrawerController.pushAdapter(CarDrawerAdapter).

Any Activity's based on this class need to set their theme to Theme.Car.Light.NoActionBar.Drawer or a derivative.

Summary

Inherited constants

Inherited fields

Public constructors

CarDrawerActivity()

Public methods

void onConfigurationChanged(Configuration newConfig)

Dispatch configuration change to all fragments.

boolean onOptionsItemSelected(MenuItem item)
void setMainContent(int resourceId)

Set main content to display in this Activity.

void setMainContent(View view)

Set main content to display in this Activity.

void setToolbarAlwaysShow()

Sets the toolbar to always show even if content in the main view of the Activity has been scrolled.

void setToolbarCollapsible()

Sets the toolbar of this Activity as collapsible.

void setToolbarElevation(float elevation)

Sets the elevation on the toolbar of this Activity.

void setToolbarElevationWithAnimation(float elevation)

Sets the elevation of the toolbar and animate it from the current elevation value.

Protected methods

int getContentContainerId()

Get the id of the main content Container which is a FrameLayout.

CarDrawerController getDrawerController()

Returns the CarDrawerController that is responsible for handling events relating to the drawer in this Activity.

CarDrawerAdapter getRootAdapter()

This method is deprecated. Do not implement this, instead call getDrawerController(). CarDrawerController.setRootAdapter(CarDrawerAdapter) directly.

void onCreate(Bundle savedInstanceState)

Perform initialization of all fragments.

void onPostCreate(Bundle savedInstanceState)
void onStop()

Dispatch onStop() to all fragments.

Inherited methods

Public constructors

CarDrawerActivity

public CarDrawerActivity ()

Public methods

onConfigurationChanged

public void onConfigurationChanged (Configuration newConfig)

Dispatch configuration change to all fragments.

Parameters
newConfig Configuration

onOptionsItemSelected

public boolean onOptionsItemSelected (MenuItem item)

Parameters
item MenuItem

Returns
boolean

setMainContent

public void setMainContent (int resourceId)

Set main content to display in this Activity. It will be added to R.id.content_frame in car_drawer_activity.xml. NOTE: Do not use AppCompatActivity.setContentView(int).

Parameters
resourceId int: Layout to display as main content.

setMainContent

public void setMainContent (View view)

Set main content to display in this Activity. It will be added to R.id.content_frame in car_drawer_activity.xml. NOTE: Do not use AppCompatActivity.setContentView(View).

Parameters
view View: View to display as main content.

setToolbarAlwaysShow

public void setToolbarAlwaysShow ()

Sets the toolbar to always show even if content in the main view of the Activity has been scrolled. This is the default behavior.

setToolbarCollapsible

public void setToolbarCollapsible ()

Sets the toolbar of this Activity as collapsible. When any content in the main view of the Activity is scrolled, the toolbar will collapse and show itself accordingly.

setToolbarElevation

public void setToolbarElevation (float elevation)

Sets the elevation on the toolbar of this Activity.

Parameters
elevation float: The elevation to set.

setToolbarElevationWithAnimation

public void setToolbarElevationWithAnimation (float elevation)

Sets the elevation of the toolbar and animate it from the current elevation value.

Parameters
elevation float: The elevation to set.

Protected methods

getContentContainerId

protected int getContentContainerId ()

Get the id of the main content Container which is a FrameLayout. Subclasses can add their own content/fragments inside here.

Returns
int Id of FrameLayout where main content of the subclass Activity can be added.

getDrawerController

protected CarDrawerController getDrawerController ()

Returns the CarDrawerController that is responsible for handling events relating to the drawer in this Activity.

Returns
CarDrawerController The CarDrawerController linked to this Activity. This value will be null if this method is called before onCreate() has been called.

getRootAdapter

protected CarDrawerAdapter getRootAdapter ()

This method is deprecated.
Do not implement this, instead call getDrawerController(). CarDrawerController.setRootAdapter(CarDrawerAdapter) directly.

Returns
CarDrawerAdapter Adapter for root content of the Drawer.

onCreate

protected void onCreate (Bundle savedInstanceState)

Perform initialization of all fragments.

Parameters
savedInstanceState Bundle

onPostCreate

protected void onPostCreate (Bundle savedInstanceState)

Parameters
savedInstanceState Bundle

onStop

protected void onStop ()

Dispatch onStop() to all fragments.