Android Show HTML Bulleted List from strings.xml

If you search on the internet about how to show a bulleted list from strings.xml using HTML tags, most of the answers will say that you should use a WebView, or to use the bullet symbol directly, or other alternatives. And on some posts I even read that you can’t use <li> tag from HTML because it is not recognized in Android. This is totally untrue. Here, in this post you can see all the HTML tags which are supported and which are NOT supported. I tried them all ūüôā

Below I will show you how I used <li> tag in order to show a bulleted list from strings.xml.

strings.xml

activity_main.xml

Activity

 

And below is the result ūüôā

As you can see, is pretty easy to show a bulleted list if you need to show it on a TextView. So there’s no need to refactor your code to use WebView or other alternatives. Of course, if you don’t like this approach, you can use the others. I don’t want to imply this the correct and only way to show bullets. But I wanted to write this post, because I didn’t find this approach anywhere else and maybe someone will need it, or like it more than the other ones :P.

Please let me know if you find this post useful by writing a comment or rating it. Also don’t forget to share it if you liked it ūüėČ

Android – HTML in textView

Display HTML format

First we will present below what are the supported tags for a textView and what are not supported. You can skip this section if you are already familiar with these or you want to jump directly into action ūüôā

Supported tags
Unsupported tags
  • <a href=”…”>
  • <b>
  • <big>
  • <blockquote>
  • <br>
  • <cite>
  • <del>
  • <dfn>
  • <div align=”…”>
  • <em>
  • <font size=”…” color=”…” face=”…”> Size is working only for strings in strings.xml, for strings in Java code it seems that size DOESN’T change. Face¬†is working in both xml and java code but only for default faces that Android supports like (monospace, serif, etc)
  • <h1>
  • <h2>
  • <h3>
  • <h4>
  • <h5>
  • <h6>
  • <i>
  • <img src=”…”>
  • <li>
  • <p>
  • <s>
  • <small>
  • <strike>
  • <strong>
  • <sub>
  • <sup>
  • <tt>
  • <u>

 

  • <abbr>
  • <acronym>
  • <address>
  • <area>
  • <article>
  • <aside>
  • <audio>
  • <base>
  • <basefont>
  • <bdi>
  • <bdo>
  • <body>
  • <button>
  • <canvas>
  • <caption>
  • <center>
  • <code>
  • <col>
  • <colgroup>
  • <data>
  • <datalist>
  • <dd>
  • <div>
  • <dl>
  • <dt>
  • <details>
  • <dialog>
  • <dir>
  • <embed>
  • <fieldset>
  • <figcaption>
  • <figure>
  • <font size…>????? doesn’t work for strings declared in Java code.
  • <footer>
  • <frame>
  • <frameset>
  • <form>
  • <head>
  • <header>
  • <hr>
  • <html>
  • <iframe>
  • <input>
  • <ins>
  • <kbd>
  • <label>

 

  • <legend>
  • <link>
  • <main>
  • <map>
  • <mark>
  • <menu>
  • <menuitem>
  • <meta>
  • <meter>
  • <nav>
  • <noframes>
  • <noscript>
  • <object>
  • <ol>
  • <optgroup>
  • <option>
  • <output>
  • <param>
  • <picture>
  • <pre>
  • <progress>
  • <q>
  • <rp>
  • <rt>
  • <ruby>
  • <samp>
  • <script>
  • <section>
  • <select>
  • <source>
  • <span style=””>
  • <style>
  • <summary>
  • <table>
  • <tbody>
  • <td>
  • <textarea>
  • <tfoot>
  • <th>
  • <thead>
  • <time>
  • <title>
  • <tr>
  • <track>
  • <ul>
  • <var>
  • <video>
  • <wbr>

 

1. String in Java code

Examples that don’t work

  • Using alpha channel

Note: 80 from¬†#800000FF should have added a blue color with an alpha of 50% to the “blue” text

Examples that work

  • Using rgb

  • Using hexa (it works with lower case letters also)

2. String in strings.xml

When the text that needs to be formatted is located in strings.xml, there are a few ways of getting the text. Unfortunately, not all of them will keep the style.

strings.xml

Examples that¬†DON’T¬†work

  • using getString()

  • using getString() + Html.fromHtml()

 Examples that work

  • using getText()

  • using the resource id directly

  • Using getString() + Html.fromHtml()+<![CDATA[html source code]]>

<![CDATA[html source code]]> is used in strings.xml so that you can use the actual HTML you include without needing to translate all the special characters like <, >, etc.

Note: CDATA is working only if the string is retrieved using Html.fromHtml().

Note 2: Also notice that when we used CDATA color=red is written without ”. If you keep ” they need to be escaped.

What is the difference between getText() and getString()?

  • getText() returns CharSequence
  • getString() returns String

Below is a table in which are presented CharSequence interface and some of the classes that implement this interface.

As you could see from the above table, String¬†objects do not keep style formatting, so this is why calling¬†getString() won’t work. On the other hand,¬†getText()¬†internally uses Html.fromHtml() to parse HTML tags, which returns a Spanned object, which keeps the style formatting of a text.

  • alpha channel

As you can see, the transparency is working for strings declared in strings.xml.¬†For now, I am not sure why it’s working for xml but not for java code. If you know or find an answer please post a comment on the post.

 

 

 

Conclusion

For strings located in strings.xml file use:

textView.setText(getText(R.string.formatted_text));

And for strings in Java code use:

textView.setText(Html.fromHtml(str, Html.FROM_HTML_MODE_COMPACT));

3. General examples

Other motives that might not keep the style on a text:

 

 

 

Resources:

https://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html

http://saket.me/html-tags-textview/?utm_source=Android+Weekly&utm_campaign=4fc49af2d9-android-weekly-265&utm_medium=email&utm_term=0_4eb677ad19-4fc49af2d9-337918897

 

How to get current flavor from gradle

 Figuring out how to pull the current flavor from gradle

While working on an update for one of our games, I needed to figure out how to get the current flavor and use it in code. Since there are a lot of ways to do this, I chose to create a custom build config parameter and set the value in each variant I have.

Adding a new build config field

First thing is to add a new build config property in your flavors. Using buildConfigField helps us do that. Therefore I defined a String called VARIANT and set the value to paid or free depending on what flavors I have.

Just in case you don’t know where productFlavors should be put, it is inside the android definition of your build.gradle.

By doing this you can access that value in your code using the BuildConfig class that is generated by Gradle. You can use the same class to check if the current build is in debug/release mode or other stuff.

Get the current flavor name

Now, to access the newly created property and use it I do something like this:

Conclusions

I use this approach because it gives me the power to name the flavors to whatever I want. I also feel that is less hacky than looping through different directories or figuring out paths to get the flavor and in the end if the flavor name gets changed maybe I will forget to change it in the code as well..

Probably there are a lot more ways to get to the same thing but this feels to me that is a viable solution :). I am curious to see other ways to do the same thing from others. Please post your own solution in the comments below. Thanks!

 

 

 

Signing key SHA1 fingerprints within AndroidStudio

Did you know you can get the signing SHA1 and MD5 fingerprints within AndroidStudio?

If you need the SHA1 or MD5 fingerprints of your Android debug/release key/certificate for a new API or framework that you want to integrate in your project like Firebase or Maps, then you can also get it from AndroidStudio.

Follow the steps below to get your SHA1 or MD5 fingerprint of your key/certificate:

  • Open your project in AndroidStudio
  • Go to the Gradle tab on the right side of the IDE
  • Expand yout project and go to Tasks->Android->signingReport
  • Double click¬†signingReport
  • Below in the Run tab you will see the output where you can also find the fingerprints for each signing configuration your have

You can see the same steps in the picture below.

Android signing key SHA1 & MD5 AndroidStudio
Android signing key SHA1 & MD5 AndroidStudio

Android: CoordinatorLayout RecyclerView First Item not visible

Problem

I had to implement a Toolbar with tabs, together with CoordinatorLayout, and the content of each tab had to be a list of items, for which I used RecyclerView. But when I ran the app, the first item in the list was overlapped by the tabs, so it was not fully visible.

This is how my xml file looked like:

FrameLayout is replaced in the code with a fragment containing the RecyclerView.

Solution

This property MUST be added to your scrolling view in order to make all list’s items¬†visible:

In my case, as I was using a FrameLayout¬†which contained a RecyclerView, I had to add this property to the FrameLayout, and not directly to the RecyclerView as it was in another xml file and wouldn’t work. But if you declare your RecyclerView directly inside CoordinatorLayout, you should add this property to it, and everything should be fine.

Just a final Note

If you are using ListView instead of RecyclerView together with CoordinatorLayout, you will encounter different scrolling and UI issues that will be difficult to handle. At first I had a ListView and tried to make the app work with it, but I ended up in refactoring the code and used RecyclerView instead. This way, all the UI issues were solved much more easily.

So my recommandation is to refactor your code and replace ListViews with RecyclerViews if you intend to benefit from the power of CoordinatorLayout.

Android Studio can’t open Android Device Monitor

Problem

Recently I had this issue: my Android Device Monitor didn’t open and and an error log file was generated every time I was tring to open it, under¬†Android –¬†sdk –¬†toolslibmonitor-x86_64configuration.¬†Also, I have to mention that it happened on Windows.

I googled it and found different answers but none worked for me, and after some further digging, I found the solution for my case.

Solution (that worked for me)

My .android folder was read-only so I had to change this. The file is located in my case under C:\Users\<your_user>. So right-click on the folderselect Propertiesuncheck read-only option.

Other Solutions

Below there are different other solutions in case the above one doesn’t work for you.

  • Close Android Studio and run it again as Administrator;
  • Kill or end “monitior.exe” running process using Taskmanager in Windows
  • Try to update Android SDK Tools
  • Make sure you have JAVA_HOME¬†in your system environment variables

Android Gradle Dependencies by Flavors

In a previous post about Gradle Flavors and Build Types, we mentioned that we can set different dependencies inside dependencies{} block by flavors, by build type or both combined. Below we will see an example for each situation.

By flavors

  • <flavorName>Compile

By build type

  • <buildTypeName>Compile>

By both flavor and build type

  • <flavorName><BuildTypeName>Compile.
  • In this case you have to define the custom compile in configurations{}¬†block in order to be recognized in dependencies{} block.

    Final result:

 

Error:Execution failed for task ‘:Application:transformClassesWithDexFor

I had this error after updating the project to the latest Build Tools 23.0.0. After spending some hours on google to find a solution without any success, I tried to deactivate the Instant Run option from Preferences. And when I ran the app, it worked!

This is not really a solution, but if you get this error at least you can run your app if you uncheck “Enable Instant Run to swap code/resource on deploy (default enabled) .

Android Set Horizontal Divider to a Vertical LinearLayout

If you need to use a “list” divider, but you need it on a vertical LinearLayout instead of a List you can do this by using some attributes on the LinearLayout. It’s not very straightforward as you maybe¬†expected, but it’s not very difficult either. Before starting, you have to know the 2 major things this tutorial will teach you:

  • how to add divider to a VERTICAL LinearLayout
  • how to add padding at the bottom between divider and content

Summary

  • create 2 drawables: one for shape and one for the actual drawable
  • add¬†2¬†LinearLayout attributes android:showDividers="end"
    and android:divider="@drawable/divider_drawable" for each LinearLayout

Now, let’s begin!

  1. Create a new project (activity_main.xml will be automatically generated. You will have to edit it later with the code from this tutorial).
  2. In the app Рsrc Рmain Рres Рdrawable folder,  create a new drawable and name it divider_shape.xml. Inside this xml we will have to create a rectangle shape with a hight of 1dp for the divider.

 3. Again, in the drawable folder, create another drawable xml and name it divider_drawable.xml. Inside this xml we have to create a layer-list which will use the drawable_shape.xml as drawable. Here is the place where you can handle the padding for the divider. In this tutorial we use top and bottom paddings.

4. Now in activity_main.xml, add the following code:

As you can see the “list” divider we want to simulate, uses the “end” value in order to be displayed at the bottom¬†of the each linearLayout.

NOTE: Don’t forget to add the orientation attribute as VERTICAL for every LinearLayout. Otherwise, the default is Horizontal, and the divider won’t be displayed.

Android LinearLayout Dividers