In this post I will walk you through how to achieve customization of various Android UI elements. For those not interested in reading through this, all the code can be found on GitHub. For everyone else let’s get started applying our own theme to our app!
So let’s go ahead and open up Android studio and create a new Android project.
Let’s go ahead and select a Blank Activity and we can keep the default name of MainActivity for this activity.
Now we’re ready to start getting our app customized!
Android has come a long way in terms of its look and feel. Unfortunately, though it’s not as easy to completely get your app’s UI components to match the theme of your app. With iOS you could simply chose the font you wanted for a label from a dropdown. Android doesn’t make this as simple. For example, if you want to use a custom font for your TextView you would have to create the TextView in the layout file, get a reference to that component by its ID then load your font into a TypeFace and apply the Typeface to your TextView reference. Let’s walk through how we would do that with a specific example.
Here’s our layout for our MainActivity and below the layout XML we have a screenshot of what the app looks like. Go ahead and copy and paste this into your activity_mail.xml. We are setting up a RelativeLayout with three components. A TextView, an EditText component and a Button. Pretty simple interface.
Now we can update our MainActivity.java but adding to our onCreate method the following code. With the following code we are going to create a Typeface with our font. Then we are going to apply that Typeface to our TextView. This will allow the TextView to appear with the Gotham-Bold font.
In this example, you can see that in the activity’s onCreate(Bundle savedInstanceState) method we set the content view and from the loaded view layout we get a reference to the TextView with identifier myTextView.
The next step is to create a Typeface object with the font you want to use. I’m using the Gotham-Bold.otf font file, but you would replace with your own font file along with the correct path to said font file if you wanted to create a different directory to keep your font assets in. In my case, under all my font’s can be found in the newly created assets directory where I have a sub-folder named fonts
In the final step we call setTypeface on the desired TextView object and we now have a label with our custom font. Check out the screenshot below to see what this code will generate in the app. Pretty cool huh? We can do the same with an EditText:
This is a quick and dirty way of doing it. It’s effective sure but having to create a reference to each TextView, EditText, Button, etc. generates a lot of the same code. Wouldn’t it be nice to have this code in one place that is called every time you create a layout with a your custom UI component class? If only that could be done. Wait… it can!
Before we get into the nitty gritty add the following class to your project. It’s main function is to load a font from the font’s folder of your app’s assets directory. Just to avoid loading and reloading the same font into memory over and over again as you begin to use it.
Now let’s create a new java file in your project and call it PositiveActionButton.java. Here we’ll create a custom class that extends Button to set the typeface, set the background color, the button font color and set the default font size.
We just need to update our activity_main.xml layout to leverage this new UI component. Here’s the listing below. We’ve replaced the
Here’s what you should be seeing so far. Custom EditText and TextView fonts and a custom light blue button with large white text.
Looking pretty good right. Except we have that ugly default font in our ActivityBar. No offense Google, but that just doesn’t match the app anymore. So let’s update that font to match what we have for our custom Button, EditText and TextView. Let’s go ahead and create a new Java file in our package that we’ll call CustomTypefaceSpan.java. Here’s the code we’ll use in this class
Let’s examine what’s going on here as this is the most complicated thing we’ll be doing. Our constructor takes a Context and a String. Since we’re extending the TypefaceSpan class we’re going to call super(family) to construct a TypefaceSpan object with the font family description should we want to specify one. Next we use the passed in Context to obtain a Typeface and then set that to our private Typeface data member.
Next we’re going to override updateDrawState(TextPaint paint) and updateMeasureState(TextPaint paint) to call a method that we’ll use to set the typeface of the custom Paint so that it knows what to do (i.e. paint the text on the screen with the specified font). Lastly we will want to leverage this so let’s add the following method to MainActivity.java and add a call to this method at the end of the onCreate method.
If we go ahead and run this sample code now we should see the following. An app with a themed ActionBar, TextView, EditText and Button. Pretty cool huh? I think so and I truly hope that this article was helpful. The full sample code project can be found on my GitHub