In an Android app, you can use the getSupportActionBar()
method of the AppCompatActivity
class to get the ActionBar
object for the activity. The ActionBar
is a toolbar at the top of the activity that can contain a title, navigation buttons, and other action items.
Here's an example of how you could use the getSupportActionBar()
method to get the ActionBar
object in an AppCompatActivity
:
import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; public class MyActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { // use the action bar actionBar.setTitle("My Activity"); actionBar.setDisplayHomeAsUpEnabled(true); } } }
In this example, the getSupportActionBar()
method is used to get the ActionBar
object for the activity. If the ActionBar
object is not null
, the setTitle()
and setDisplayHomeAsUpEnabled()
methods are used to set the title and enable the home button in the action bar, respectively.
Note that the AppCompatActivity
class is a subclass of the Activity
class that provides support for the action bar on devices running Android 2.1 (API level 7) and higher.