Most of the times, I have seen developers not leveraging the power of themes and styles. Themes and styles are a great way to easily create UI that are manageable and compatible for various platforms. Here's the official documentation that explains styles and themes in details, and I consider this portion of documentation to be equally important to any developer working with Android.
For the example of this post, we will see how to make a dialog, a custom dialog, not to have a title. The easiest way to do this, is through code byt writing this snippet in your custom dialog class.
Dialog with Title on Android 2.2
Dialog with no title on Android 2.2
While this would work anyway, and would also be compatible with all the versions of Android. But, it's a good idea to use more of your themes and styles in your programming, so that your code base is maintainable and your apps would behave and feel consistently. Using styles and themes makes it very easy to tweak or adapt to various platform versions and/or device sizes and resolutions. So, let's get in.
The application tag, has a configurable attribute called "android:theme" with which you can set a theme to your application as a whole. You can also specify themes for individual themes for all your activities separately. Sounds nice!!! But, let's stick to one theme for our application as a whole for simplicity.
For this example, we have a theme (it's actually called a style), called MyThemeSelector as shown below. This is specified in the styles.xml in your values folder. Notice, your custom theme is a child of the Theme, which is one of the system's default themes.
<resources> <style name="MyThemeSelector" parent="@android:style/Theme"></style></resources>
Ideally, you should also declare your custom theme to extend one of the basic themes that are available with platforms on or above Honeycomb. For example, here we have created another styles.xml in a folder called values-v11, which looks like this.
<style name="MyThemeSelector" parent="@android:style/Theme.DeviceDefault.Light"></style>
So, your basic theme is now compatible with both older versions and versions greater than Honeycomb. By this, I mean, that, when you run the app, your app would adapt to the platform that it is running on, and would give a consistent look and feel for your users.
Now, coming back to the main problem. "Creating a dialog without title". Here also, we would use themes, as against code. Here are the two new themes that you would be declaring.
For values folder: (Platform versions older than Honeycomb)
<style name="My.Theme.Dialog" parent="@android:style/Theme.Dialog">
<style name="MyThemedDialog" parent="@style/My.Theme.Dialog"></style>
For values-v11 folder: (Platform version for Honeycomb and above)
<style name="My.Dialog.Theme" parent="@android:style/Theme.DeviceDefault.Light.Dialog.NoActionBar"></style>There's a subtle difference between the two versions. Of course, other than the parent classes. In older platform versions, there wasn't a version of the Dialog theme without a title bar. So, what we do here, is to extend the basic Dialog them, and overwrite an attribute, so that our custom "My.Theme.Dialog" is a dialog without a title bar.
<style name="MyThemedDialog" parent="@style/My.Dialog.Theme"></style>
But, for Honeycomb and above, the platform itself provides a version of the Dialog theme, without the title bar (or the Action Bar).
And finally, the last step for getting everything to work is set the theme to your dialogs.
MyDialog dialog = new MyDialog(this, R.style.MyThemedDialog);Why didn't we use "My.Theme.Dialog" directly? Well, we could still try and tweak the theme for older versions, in the values folder, by adding a few more attributes.
dialog.setTitle("Here's my title");
Dialog with title on Android 4.1
Dialog with no title on Android 4.1
As you can see, for both older and newer platforms, the app runs by adapting itself and gives a consistent look and feel. You didn't have to do much with your code.
15 lines of XML is the MAGIC here!!!! (I didn't count though, just guessing)
Sample project here.