Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting Action Bar title and subtitle

I want to set title and subtitile of my action bar before compile time. I got a way to do it like this:

ActionBar ab = getActionBar(); ab.setTitle("My Title"); ab.setSubtitle("sub-title"); 

But I don't want to do it on run time. Is there any xml file or any location where I can specify these titles?

I am trying to achieve this:

enter image description here

Edit:

The reason why I want it in xml is that I want my app to be supported in API level 8. And the method getActionBar() is supported at least on level 11.

like image 572
Adil Malik Avatar asked Jan 12 '13 19:01

Adil Malik


People also ask

How can I change action bar title color?

xml file: Just go to res/values/styles. xml file. edit the xml file to change the color of action bar.

How to set Action bar name in Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have taken text view to show status bar tittle.

What is the difference between action bar and toolbar?

What is the difference between the toolbar and the action bar? The most obvious difference between the two is the updated visual design of the toolbar. The toolbar no longer includes an icon on the left side and decreases some of the spacing between the action items on the right side.


2 Answers

You can do something like this to code for both versions:

/**  * Sets the Action Bar for new Android versions.  */ @TargetApi(Build.VERSION_CODES.HONEYCOMB) private void actionBarSetup() {   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {     ActionBar ab = getActionBar();     ab.setTitle("My Title");     ab.setSubtitle("sub-title");    } } 

Then call actionBarSetup() in onCreate(). The if runs the code only on new Android versions and the @TargetApi allows the code to compile. Therefore it makes it safe for both old and new API versions.

Alternatively, you can also use ActionBarSherlock (see edit) so you can have the ActionBar on all versions. You will have to do some changes such as making your Activities extend SherlockActivity and calling getSupportActionBar(), however, it is a very good global solution.

Edit

Note that when this answer was originally written, ActionBarSherlock, which has since been deprecated, was the go-to compatibility solution.

Nowadays, Google's appcompat-v7 library provides the same functionality but is supported (and actively updated) by Google. Activities wanting to implement an ActionBar must:

  • extend AppCompatActivity
  • use a Theme.AppCompat derivative

To get an ActionBar instance using this library, the aptly-named getSupportActionBar() method is used.

like image 96
A--C Avatar answered Sep 22 '22 23:09

A--C


The title for the actionbar could be in the AndroidManifest, here:

<activity      . . .           android:label="string resource"      . . . </activity> 

android:label A user-readable label for the activity. The label is displayed on-screen when the activity must be represented to the user. It's often displayed along with the activity icon. If this attribute is not set, the label set for the application as a whole is used instead (see the element's label attribute). The activity's label — whether set here or by the element — is also the default label for all the activity's intent filters (see the element's label attribute). The label should be set as a reference to a string resource, so that it can be localized like other strings in the user interface. However, as a convenience while you're developing the application, it can also be set as a raw string.

like image 27
Rodrigo Amaro Reveco Avatar answered Sep 22 '22 23:09

Rodrigo Amaro Reveco