Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can do something like this to code for both versions:</p> <pre><code>/** * Sets the Action Bar for new Android versions. */ @TargetApi(Build.VERSION_CODES.HONEYCOMB) private void actionBarSetup() { if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.HONEYCOMB) { ActionBar ab = getActionBar(); ab.setTitle("My Title"); ab.setSubtitle("sub-title"); } } </code></pre> <p>Then call <code>actionBarSetup()</code> in <code>onCreate()</code>. The <code>if</code> runs the code only on new Android versions and the <code>@TargetApi</code> allows the code to compile. Therefore it makes it safe for both old and new API versions.</p> <p>Alternatively, you can also use <a href="http://actionbarsherlock.com/">ActionBarSherlock</a> (<em>see edit</em>) so you can have the ActionBar on all versions. You will have to do some changes such as making your Activities extend <code>SherlockActivity</code> and calling <code>getSupportActionBar()</code>, however, it is a very good global solution.</p> <h2><strong>Edit</strong></h2> <p>Note that when this answer was originally written, <code>ActionBarSherlock</code>, which has since been <em>deprecated</em>, was the go-to compatibility solution.</p> <p>Nowadays, <a href="https://developer.android.com/tools/support-library/features.html">Google's <code>appcompat-v7</code> library</a> provides the same functionality but is supported (and actively updated) by Google. Activities wanting to implement an <a href="https://developer.android.com/reference/android/support/v7/app/ActionBar.html"><code>ActionBar</code></a> must:</p> <ul> <li>extend <code>AppCompatActivity</code></li> <li>use a <code>Theme.AppCompat</code> derivative</li> </ul> <p>To get an <code>ActionBar</code> instance using this library, the aptly-named <code>getSupportActionBar()</code> method is used.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload