Current Android Release version:
Version Code: Pie
Version: 9
API level: 28

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.

Understanding Android Broadcast Receivers

A broadcast receiver is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android run time once this event happens.

Create Broadcast Receiver:

Create Receiver class by extending BroadcastReceiver class.
Below onReceive() method will execute whenever your receiver gets notifications whenever the system event, for which it is registered, occurs.

public class MyReceiver extends BroadcastReceiver 
{
     @Override
     public void onReceive(Context context, Intent intent) 
     {
        // Implement code here to be performed when broadcast is detected
     }
}

The Broadcast Receiver object is active only for the duration of onReceive (Context, Intent). Once your code returns from this function, 
the system considers the object to be finished and no longer active.

When a matching broadcast is detected, the onReceive() method of the broadcast receiver is called, at which point the method has 10 seconds time within which to perform any necessary tasks before returning. So, Don't perform any long operations on onReceive() method. To perform long running operations inside onReceive() method, always start Services or use goAsync() method.

you can use goAsync() method to create another thread to perform long operations. This can be called by an application in onReceive(Context, Intent) to allow it to keep the broadcast active after returning from that function. This does not change the expectation of being relatively responsive to the broadcast (finishing it within 10s), but does allow the implementation to move work related to it over to another thread to avoid glitching the main UI thread due to disk IO.

You cannot launch a popup dialog in your implementation of onReceive() and also you can not bind to existing service.For the former, you should instead use the NotificationManager API. For the latter, you can use Context.startService() to send a command to the service.

Register Broadcast Receiver:

Broadcast receivers can be registered in two ways.
  • Static: With this approach, your receiver will be active to listen for events even if your application is not launched.
       Use <receiver> tag to register broadcasts in manifest file.
       <receiver
               android:name="com.example.techchai
               android:exported="true" >
               <intent-filter>
                     <action android:name="com.example.broadcast" />
               </intent-filter>
       </receiver>

  • Dynamic: Receivers are tightly coupled with Activity or Fragment life cycles. This receiver will listen for events only when your application is active only.
       IntentFilter filter = new IntentFilter("com.example.Broadcast");
       MyReceiver receiver = new MyReceiver();
       registerReceiver(receiver, filter);

    • When a broadcast receiver registered in code is no longer required, it may be unregistered via a call to the unregisterReceiver(receiver) method of the activity class. 
    • Dynamically registered receivers are called on the UI thread. Dynamically registered receivers blocks any UI handling and thus the onReceive() method should be as fast as possible.

Types of Broadcast Receivers:
  • Normal BroadcastReceiver: (sent with Context.sendBroadcast) are completely asynchronous. All receivers of the broadcast are run in an undefined order, often at the same time.This is more efficient, but means that receivers cannot get the results.
  • Ordered BroadcastReceiver: (sent with Context.sendOrderedBroadcast) are delivered to one receiver at a time..are completely synchronous. These will follow a specific order.The order is defined using android:priority attribute in Manifest file. The receivers with greater priority would receive the broadcast first. In case there are receivers with same priority levels, the broadcast would not follow an order. Sometimes to avoid system overload, run time system delivers the broadcasts one at a time, even in case of normal broadcasts. However, the receivers still cannot use the results.
  • Sticky BroadcastReceiver: This method is deprecated in API level 21 due to security concerns.

Security:

As the broadcast receivers have a global work-space, security is very important concern here. If you do not define the limitations and filters for the registered receivers, other applications can abuse them.
  • When you use registerReceiver(BroadcastReceiver, IntentFilter), any application may send broadcasts to that registered receiver. You can control who can send broadcasts to it through permissions.
  • When you use sendBroadcast(Intent) normally any other application can receive these broadcasts. You can control who can receive such broadcasts through permissions. Alternatively, you can also safely restrict the broadcast to a single application with Intent.setPackage.
  • Whenever you publish a receiver in your application’s manifest, make it unavailable to external applications by using android: exported=”false”. 

Conclusion:

If you don't need to send broadcasts across applications, consider using LocalBroadcastManager. This will give you a much more efficient implementation (no cross-process communication needed) and allow you to avoid thinking about any security issues related to other applications being able to receive or send your broadcasts.

Refer usage of LocalBroadcastManager.

APK Analyzer with Android Studio 2.2

Apk analyzer tool is added in Android Studio 2.2 version.The Apk Analyzer will help you to understand contents and sizes of components in your apk. 

Below are major features of Apk analyzer:
  • View absolute and relative size of files in the APK (such as DEX and Android resource files).
  • Side by Side Comparison of two apks
  • Quick view of resources and its sizes
To use this new feature, Go to Build menu and select Analyze APK. Then, select any APK that you want to analyze.