Monthly Android Morsels [December 2015]

Tips and Tricks for December 2015:

  1. Hyphenation in Android TextView – In API 23, Google introduced hyphenation for TextViews. Hyphenation is the term used for breaking up of words when they can’t fit on one line. There is a whole bunch of technical stuff involved in it and it can differ between languages. To take advantage of hyphenation in android, add the following to your TextView:
    • breakStrategy – simple, high_quality, balanced
    • hyphenationFrequency – full, normal, none
      • <TextView
              android:id="@+id/text_view_page"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:breakStrategy="high_quality"
              android:hyphenationFrequency="full"        
              android:text="@string/long_text" />

        Hyphenation Frequency = none

        Hyphenation Frequency = none

        Hyphenation Frequency - Full. Break Strategy - High Quality
        Hyphenation Frequency – Full. Break Strategy – High Quality
  2. Color Picker – When declaring a colour in your layouts, you can click on the colour icon on the side of the layout xml file. A new feature that I have discovered is the option to select the “Closest material colour”. This is such an awesome feature as I am always looking up the material colours from the website:  color picker in android studio
  3. Plurals – Sometimes you want to use a string but it needs to be a plural based on the number of items that you provide it. Instead of creating different strings yourself and manually switching between them, take advantage of the built in mechanism:
    <plurals name="how_many_oranges">
            <item quantity="zero">No oranges found</item>
            <item quantity="one">%d orange</item>
            <item quantity="other">%d oranges</item>
    </plurals>

    When using this string in your code, you would typically use it in the following way:

    String oranges = getResources().getQuantityString(R.plurals.how_many_oranges, 3, 3);

    See more about plurals here

  4. Digits – If you need an EditText to only accept certain characters, you can easily specify this in the layout. By using the android:digits field you can specify the characters that your EditText can accept. No additional code needed.
     <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:digits="1234mnp."
            android:id="@+id/editText"/>

     

  5. Great External Resources:

What Android things have you discovered this month? Leave a comment below!

Happy Holidays to everyone!