Tech tip of this week

New features in Android Studio 3.0

Below are major features introducing in Android Studio 3.0
  • Kotlin Programming language support
  • Android Profiler (CPU, Memory, Network)
  • Apk analyzer improvements
Check All the new features in official Android developer documentation:
https://android-developers.googleblog.com/2017/05/android-studio-3-0-canary1.html

Download preview version from canary channel
https://developer.android.com/studio/archive.html

Kotlin Programming Language Support in Android

What is Kotlin?
Kotlin is programming language developed by Jet brains. 

How to set up Kotlin in Android Studio?
Kotlin plugin is bundled with Android Studio 3.0 version. To support kotlin for previous Android studio versions, you need configure Kotlin plugin manually.

How to program Kotlin?
Create new project in Android studio the way you created for Android project.
You can select "Include kotlin support" to create kotlin files by default.




Configure Kotlin in application build.gradle files. Check below 2 screen shot for kotlin configuration.





Open any .java file and Ctrl + Shift + A (Find Action), type "convert java to kotlin file". After this action, java file will be converted to kotlin file. See below screenshot.




you can also have .java and .kt files in same application project.

Android: Autosizing TextViews

Android O introduced new feature to autosize the Textview based on its layout. This setting makes it easier to optimize the text size on different screens with dynamic content.

The Support Library 26.0 Beta provides full support to the autosizing TextView feature on devices running Android versions prior to Android O. The library provides support to Android 4.0 (API level 14) and higher. The android.support.v4.widget package contains the TextViewCompat class to access features in a backward-compatible fashion

There are three ways you can set up the autosizing of TextView:

  • Default
  • Granularity
  • Preset Sizes
Default:

To define the default setting in XML, use the android namespace and set the autoSizeTextType attribute to none or uniform.

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:autoSizeTextType="uniform"
  />
Granularity:

You can define a range of minimum and maximum text sizes and a dimension that specifies the size of each step. The TextView scales uniformly in a range between the minimum and maximum size attributes. Each increment occurs as per the step size set in the granularity attribute.

<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:autoSizeTextType="uniform"
  android:autoSizeMinTextSize="12sp"
  android:autoSizeMaxTextSize="100sp"
  android:autoSizeStepGranularity="2sp"
/>

Preset Sizes:

Preset sizes lets you specify all the values that the TextView picks when automatically auto-sizing text.

To use preset sizes to set up the autosizing of TextView in XML, use the android namespace and set the following attributes:
  • Set the autoSizeText attribute to either none or uniform. none is a default value and uniform lets TextView scale uniformly on horizontal and vertical axes.
  • Set the autoSizePresetSizes attribute to an array of preset sizes. To access the array as a resource, define the array in the res/values/arrays.xml file.

    <resources>
      <array
        name="autosize_text_sizes">
        <item>10sp</item>
        <item>12sp</item>
        <item>20sp</item>
        <item>40sp</item>
        <item>100sp</item>
      </array>
    </resources>
    <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:autoSizeTextType="uniform"
      android:autoSizePresetSizes="@array/autosize_text_sizes"
    />

More information will be available in official Android developer documentation:
https://developer.android.com/preview/features/autosizing-textview.html#setting-textview-autosize

Android: Working with Fonts

Fonts feature can be used from Android O version. You can create "font" folder in resources folder of your application. Below Font features are providing in Android O release.

  • Fonts in XML
  • System Fonts

Fonder name in Android application: res/font/

Adding the font files in the resource directory

Using Fonts in XML file:
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/lobster"/>

Using Fonts Programatically:
Typeface typeface = getResources().getFont(R.font.myfont);
textView.setTypeface(typeface);
Retrieving System Fonts:
FontManager fontManager = context.getSystemService(FontManager.class);
FontConfig systemFontsData = fontManager.getSystemFonts();

For more information, Check in official Android developer documentation.
https://developer.android.com/preview/features/working-with-fonts.html

Android: runOnUiThread ()

runOnUiThread is useful whenever you want to update UI from background thread.

Activity_Name.this.runOnUiThread (new Runnable () {
       @override
        public void run () {
               //add code to update UI 
        }
});

Chrome Custom Tabs in Android

Chrome custom tabs give apps more control over their web experience.
CustomTabs is part of chromium platform.

Chrome Custom Tabs is now generally available to all users of Chrome, on all of Chrome's supported Android versions (Jellybean onwards).

Chrome Custom Tabs allow an app to customize how Chrome looks and feels. An app can change things like:
  • Toolbar color
  • Enter and exit animations
  • Add custom actions to the Chrome toolbar, overflow menu and bottom toolbar
Launching links in custom tabs more faster than chrome and webview.



Implementation:

First, add custom tab library in build.gradle file.


dependencies { 
        ... 
        compile 'com.android.support:customtabs:23.3.0' 
}

Then, start url with custom tabs

String url = ¨https://coderinsight.blogspot.com/¨; 
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(url));

after this you can customize tabs based on your needs.

More information will be available in below official Chrome page:
https://developer.chrome.com/multidevice/android/customtabs


Android Intent Service

What is Intent Service:

Intent Service is a base class for service. This handles asynchronous requests on demand. Clients send requests through startService(Intent) calls;
the service is started as needed, handles each Intent in turn using a worker thread, and It will stop by itself as soon as it is done performing the task.


All requests are handled on a single worker thread.


How to create Intent Service:

For using IntentService, create a class which extends IntentService and implement onHandleIntent(Intent). It will receive requests and handle them in background thread.


A single background thread is used to handle all the requests and requests are processed one by one. It might take time to process one request and other one has to wait for that time. When all requests have been handled, the IntentService stops itself.

public class MyService extends IntentService {
    MyService () 
    {
       super("MyService");
    }
    @Override
    protected void onHandleIntent(Intent intent) {
     //This method is invoked on the worker thread with a request to process 
    }
}

Limitations:
  • At a time it will process only one request
  • Can not be interrupted
  • The IntentService cannot run tasks in parallel. Hence all the consecutive intents will go into the message queue for the worker thread and will execute sequentially.the request waits until the first operation is finished.
  • It can't interact directly with your user interface.


Differences between Service & IntentService:
  • Service uses application main thread. Intent Service create worker thread to perform operations.
  • The Service can be used in tasks with no UI, but shouldn't be too long. If you need to perform long tasks, you must use threads within Service.The IntentService can be used in long tasks usually with no communication to Main Thread. If communication is required, can use Main Thread handler or broadcast intents. 
  • The Service is triggered calling to method onStartService(). The IntentService is triggered using an Intent.
  • The Service runs in background but it runs on the Main Thread of the application. The IntentService runs on a separate worker thread.