Android what is title bar

Statically Typed

because Hindley-Milner rocks

Android: Dynamic and Custom Title Bars

Arguably the worst part in playing around with Android is its insistence to put that ugly title bar above everything I do as a default to my Activities. Really, Google, was that necessary? I think you could have done better and I know that I can do better. Thankfully you gave us free reign to substitute a custom variant or edit it out altogether. This post is going to explore the ways in which we can create custom title bars and more importantly just how far we can push the limits (of bad taste?)

This post is going to go into the internals of some of the Android classes and platform. We’ll focus our attention on things related to the Title Bar. If that isn’t of interest to you there are several Q&A/Blog posts that could very easily be of interest:

They’ll provide you with some ideas and inspiration but they won’t give you the “why” it works that way. Why almost always takes more time than most people are willing to spend. Hopefully, I can spend that time for you. Please feel free to correct me when I’m wrong (my ego isn’t large enough to not welcome criticism.)

Theme and Style Attributes

At the basic level Title bars are controlled by a combination of Style and Theme parameters. Styles influence Layouts and Themes, Activities. This distinction is important and good programming practice should promote separation of concerns; Android developers are no exception.

To understand how the Title Bar is laid out in an unmodified, plain, vanilla application look first at the standard Theme found in the Android kernel repository for Themes. The following three attributes containing the word “Title” stand out in the “Theme” Theme:

Following the references from the default Theme to the Android kernel repository for Styles, we first find the “WindowTitle” Style:

The attributes “singleLine,” “shadowColor,” and “shadowRadius” imply that the Title Bar is nothing more than a TextView. This is further backed up by the attributes in the “DialogWindowTitle” in the next Style group:

For completeness, the “WindowTitleBackground” Style:

contains a reference to a drawable.

As I said at a basic level this is what controls the default look and feel of the Title Bar within an Android App. Anything that can be done within the XML resources of a TextView can be done in the Style attributes to yield something different from default. You will have to write your own Theme which makes use of the new Style and tie it in the manifest file.

Dynamically Updating the Default Title Bar

The Android developers at Google realized people using their SDK might have the need to modify the text or the text color of the Title Bar. Activity provides four methods, two mutators and two accessors handling the text and text color local state. Changing either property of one Activity will not impact other Activities of the application. To prove the point look at the Activity code on the Android git repository:

Notice that the code delegates the process of setting the text and text color to the contained Window class using setTitle and setTitleColor. This will be important later.

Thus, if all you want to do is dynamically update the text of the Title Bar or the color of that text use those two methods. You can call them from anywhere in the Activity code and expect that the Title Bar will redraw itself.

Читайте также:  Slave lord elven conquest андроид

Animated Title Bars

Getting back to the Styles and Themes, the background drawable seems like a perfect first step in adding something more profound to the Title Bar. In the last post to create a dynamic Splash Screen I made the background transparent and placed an animation overlay across the entire view extent (with ample padding.) A TextView is more limited as we are not dealing with a full Activity and probably should avoid placing anything transparent within its background for aesthetic reasons. I say probably because there are some of you who may wish to limit the size of the Activities without limiting the sizes of the Views. It’s a dirty hack. Let’s not indulge our wicked desires and look at animating the Title Bar.

Adding an animation to the Title Bar is almost as simple as replacing the background attribute drawable resource with an animation resource. The one catch is that we need to access the background of the TextView to set the animation in motion. In order to do that, we can’t exactly have things hidden away in the Theme and Styles section. We need it explicitly stated just as we would with any layout XML file and labled with an “id” attribute:

What follows next seems almost ludicrous, we have to ask our phone if it supports a custom title bar. I can only assume this is a carry over from earlier versions of Android as we’re perfectly safe to add in customized Title Bars through attributing Themes and Styles. And if it’s not, well, that’s why they pay you to develop Apps.

The code goes something like this:

After the feature request check we set the layout to our XML file and then go about adding in the layout for the Title Bar. We can not, however, attempt to call the “setContentView” method before requesting the new window feature. If you do your App will crash.

To get the animation started we call on our dear ol’ friend the AnimationUtils class:

I’ll leave out the XML for the animation resource. (One comment: This animation will run for the entire lifetime of the Activity and consume precious resources in doing so. Android is powerful but it is still quite limited. Animations aren’t easy on the CPU that ships with some phones.)

Dynamically Updating the non-Default Title Bar

Changing the Title Bar due to actions of the user is just as easy as dynamically adjusting any TextView as long as you can call upon it by id. The only thing you can’t do is hide it. That can only be done setting the FEATURE_NO_TITLE before the call to “setContentView” or within the Theme itself. If you want the option to hide it dynamically, place a TextView within the layout and forget about a Title Bar.

What about the four methods linked to the Activity that were mentioned above? Here’s the part that gets interesting: don’t use them! Go ahead and try to change the Title Bar text using the Activity methods. While you can change the state contained within the Activity itself your custom Title Bar stays static. But why?

Remember when I showed you that the call to Activity’s onTitleChanged delegates to Window‘s setTitle and setTitleColor methods? Let’s have a look at what those look like underneath the hood. Again, we go to the Android git repository:

and meet an abstract base class. No problem, right? Consulting the documentation on the Android resource pages yields the catch-phrase: “The only existing implementation of this abstract class is android.policy.PhoneWindow.” And following that down to the Google Groups Android page gives us this conversation wherein we find out:

As you say, this is an internal class, so not appropriate to discuss on
android-developers (which is for the SDK)…

-Dianne Hackborn, Android framework engineer

Читайте также:  Телеграмм для андроид тв бокс

This implies it’s not within the general repository where we’re accustomed to finding the implementation details. Nor is it mentioned on the Android developer pages. We’ll need to look at the highest level of the framework repository, within the “internal” section (Don your Dick Tracy hats.)

PhoneWindow is found in the framework base, policy section of the code. It took a little sleuthing to find (why can’t they make it easy?) but eventually we can see the implementation details of setTitle and setTitleColor:

And mTitleView is a TextView that appears to not have been set. That’s why you can’t even attempt to trick the Activity by calling the getWindow method to set the title and title color directly. If you do, nothing will happen to the UI but you’ll be able to see that the state of the Window is modified from the code.

A Custom Title Bar

You are not restricted to only a TextView for the Title Bar. In fact, there’s even a way to place an icon on your Title Bar built into the Android Activity code. I won’t get into that here because that’s what I would consider part of the “plain, old, vanilla” Title Bar.

Just like what was done up above in adding an animated background to the Title Bar, making a unique Title Bar involves creating a layout XML resource file. I suggest wrapping your custom Title Bar within a LinearLayout horizontally positioned but you’re free to do whatever you wish:

You don’t need to do anything further but if you’re going to go this far why not shoot for the moon? I’ll modify the Theme so that it’s large enough to fit a Button and I’ll even link to new background Styles just to make this really custom:

Within the Title Bar Styles, let’s remove the single line feature of the Title Bar (you never know,) change the shadowing so it stands out from what will be the actual background, and provide the reference to the drawable resource that’ll be the background:

With all that in place it’s time to see what it looks like:

Yup, that’s pretty awful. This would never go out to a client. I think you can tell why.

What Really Happens to the Default TextView With a Custom Title Bar

Above I mentioned that it appears that the TextView of the PhoneWindow is never set. I lied. It’s not that simple but the explanation would have detracted from the flow of this article. This is where it belongs for those that are truly curious to the inner workings of Android.

There is no place where I could find where the TextView is set to null either in the PhoneWindow or Window source code. However, I did find that the TextView is set to GONE if the mLocalFeatures variable is set to just the right value. GONE means that the TextView is not only invisible but also occupies no space within the layout. Technically speaking, setting the text and text color of the Title Bar really did change the state of the TextView. You just couldn’t see it.

Here is the code in PhoneWindow responsible for setting the state of the internal TextView:

The getLocalFeatures function returns the mLocalFeatures variable. Down in the source code for Window the mLocalFeatures variable is set:

This is why you need to request the window feature before it setting the Content of the Activity. The PhoneWindow needs to check the variables in the proper order to flag the TextView GONE.

Conclusion

Android provides you wish ample means to modify not only the UI of the Title Bar but also it’s behavior. You are limited only by your imagination but it’s important to understand that some convenience methods are lost when working with customized applications. Underneath the hood there are several assumptions made about an application, the UI state, and appearance that if not flagged in the right order will cause your App to crash before it has a chance to load.

Читайте также:  Alawar для андроид полная версия

If you have any more questions or want an explanation of some other facet working with Title Bars let me know. My blog posts have slowed down recently but they’re also much, much longer than they used to be and take an inordinate amount of time to write between side projects and life. That said, if asked I will attempt to answer, it just might take a bit.

Источник

How to Change Android Title Bar or Toolbar or ActionBar Text Programmatically in Android Studio — Tutorial

Android Title Bar or ActionBar or Toolbar is the header of any screen in an App. We usually keep fixed title names to every Activity. Some times, it is necessary to change the title-text dynamically at runtime inside the Java code. Let us change the toolbar-text programmatically.

You can also check Android GridLayout with equal-width columns for easy implementation.

Change Android Title Bar or Toolbar or Action-Bar text Programmatically

Follow the below steps to create a Toolbar and change its title at runtime.

Step 1: Create a new Android Project using the «Empty Activity» Template.

Step 2: Add the below code to the » activity_main.xml » file manually. Even if you use com.google.android.material.appbar.AppBarLayout or androidx.appcompat.widget.Toolbar in your App, the code that should be used inside the MainActivity.java is the same. .

Step 3: Add the below dependencies to the » build.gradle » Module-level file. Notice that we have upgraded our code to be compatible with AndroidX library.

Step 4: Add the below XML code to » AndroidManifest.xml » file. You can add a Label to each activity. These labels are nothing but Fixed or Static title texts.

Step 5: Now we change the Toolbar Title or ActionBar title dynamically inside the » MainActivity.java » file. In this example, we try to set the title to display the actual time at the time of running the app.

It is a good practice to call for getSupportActionBar() instead of getActionBar(). Also, we have to check the ActionBar variable against the null value before accessing its methods and properties.

This is how we can easily change the title of any ActionBar or Toolbar using Android Studio programmatically.

Learn Java basics before diving into Android for fast coding and development.

It is time to share this Android Studio tutorial with your friends and colleagues.

Источник

How to Create Custom AppBar/ActionBar/ToolBar in Android Studio | Java

Today in this tutorial, we’re going to see how to create a custom AppBar/ActionBar/ToolBar in android.

I also already created a tutorial on this topic, but here I will demonstrate it with different examples.

Gradle dependency

for circle image view:

Set the styles

now go to your styles.xml file and set the theme to NoAction Bar so that we can create our own toolbar.

Design our AppBar/ActionBar/ToolBar

now go to your main XML file and create a ToolBar and set the width and height, make sure min-height should be actionBar size.

this will give you just a toolbar without any field. Look at the below image.

for the logo, we’re using a circular image view. Already add the dependency. Put it inside the toolbar and do the adjustment.

here we set our title to null, so in the main java file assign our toolbar and do the below change

the result will be:

we can also set the logo by using app:logo=”your logo here” in your toolbar

the result will be:

for the menu, create a menu android resource folder and create a menu resource file ( /res/menu/custom_menu.xml)

import that menu in our main java file by using these code of lines

for adding a title simply add a textView inside the toolbar.

Источник

Оцените статью