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

Showing posts with label Android L. Show all posts
Showing posts with label Android L. Show all posts

How to start activity from another application in Android

Below is the code to launch activity from another application.

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example", "com.example.MyActivity"));
startActivity(intent);

Android: How to change Drawable color

With Android 5.0 (API level 21) and above, you can tint bitmaps and nine-patches defined as alpha masks. You can tint them with color resources. This makes it possible for you to create assets once and color them in your layout to match your theme.

You no longer have to create multiple versions of your assets to simply change their colors.
There's two problems with ImageView's tint, though, which is why you should avoid it:

  • ImageView's tint mixes the tint color with the original asset. What you want is for the tint color to take over entirely; instead it applies the tint on top of the existing color. So, for example, if the source asset is black, and you want it to be #77FFFFFF (a translucent shade of white), you'll actually end up getting that shade of white with a black background beneath it. 
  • android:tint is limited to ImageView.

To avoid above problems, we can use setColorFilter() method.

Resources res = getResources(); 
Drawable image= res.getDrawable(R.drawable.image); 
int color = res.getColor(R.color.primary); 
image.setColorFilter(color, PorterDuff.Mode.SRC_IN); 
view.setBackgroundDrawable(image);


Sample code using tint attribute:

<LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:gravity="center"
   android:id="@+id/sample_main_layout">
<ImageView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:src="@drawable/unnamed"/>
<ImageView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:src="@drawable/unnamed"
       android:tint="@color/red"/>
<ImageView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:src="@drawable/unnamed"
       android:tint="@color/green"/>
</LinearLayout>

If you observe in below screen, with single image created two different images with different colors.

Android L Features

The following features are available only in Android 5.0 (API level 21) and above:
  • Activity transitions
  • Touch feedback
  • Reveal animations
  • Path-based animations
  • Vector drawables
  • Drawable tinting

The v7 Support Libraries r21 and above includes the following material design features:
  • Material design styles for some system widgets when you apply one of the Theme.AppCompat themes.
  • Color palette theme attributes in the Theme.AppCompat themes.
  • The RecyclerView widget to display data collections.
  • The CardView widget to create cards.
  • The Palette class to extract prominent colors from images.

Android: Material Theme

Material design is a complete design philosophy for visual, motion, and interaction design across platforms and devices.

Android provides the following elements for you to build material design apps:

  • A new theme
  • New widgets for complex views
  • New APIs for custom shadows and animations

Android 5.0 provides three Material Theme flavors:
  • Theme.Material – Dark version of Material Theme; this is the default flavor in Android 5.0.

  • Theme.Material.Light – Light version of Material Theme.

  • Theme.Material.Light.DarkActionBar – Light version of Material Theme, but with a dark action bar.
To get started with Material design in your Android app, update your targetSdkVersion to "21" and apply the new Material theme. 

For example, when creating a custom theme for your app, open your project'sres/values/styles.xml file and extend the Material theme:

<resources>
         <style name="AppTheme" parent="android:Theme.Material">
         <!-- Customize the Material elements here -->
         </style>
</resources>


Then apply your custom theme to your application in the manifest file:<application android:theme="@style/AppTheme">


The v7 support library has been significantly updated in revision 21 to make many of the Material design elements available when running on older versions of the platform.

The Material Theme adds 5 new color values that can be easily set in your application.



<?xml version="1.0" encoding="utf-8"?> 

<resources> 
<style name="AppTheme" parent="android:Theme.Material.Light"> 
<item name="android:colorPrimary">@color/primary</item> 
<item name="android:colorPrimaryDark">@color/primary_dark</item>
<item name="android:colorAccent">@color/accent</item>
<item name="android:textColorPrimary">@color/text_primary</item>
<item name="android:textColor">@color/text_secondary</item>
<item name="android:navigationBarColor">@color/primary_dark</item></style> 
</resources>

Floating Action Button (FAB) in Android L

Floating action buttons (via Google) are:

“A special case of promoted actions. They are distinguished by a circled icon floating above the UI and have special motion behaviors, related to morphing, launching, and its transferring anchor point.”

They are actions that are directly visible in the UI instead of using action bar button.


Example Code:
1. Fab button layout

Android L: Tool Bar widget

Toolbar widget introduced in Android 5.0(API 21).This widget also available in support library for older versions.

A Toolbar is a generalization of action bars.

Toolbar supports a more focused feature set than ActionBar. The new Toolbar contains the following elements:

  • A navigation button:This may be an Up arrow, navigation menu toggle, close, collapse, done or another glyph of the app's choosing. This button should always be used to access other navigational destinations within the container of the Toolbar and its signified content or otherwise leave the current context signified by the Toolbar.
  • A branded logo image: This may extend to the height of the bar and can be arbitrarily wide.
  • A title and subtitle: The title should be a signpost for the Toolbar's current position in the navigation hierarchy and the content contained there. The subtitle, if present should indicate any extended information about the current content. If an app uses a logo image it should strongly consider omitting a title and subtitle.
  • One or more custom views: The application may add arbitrary child views to the Toolbar. They will appear at this position within the layout. If a child view's Toolbar.LayoutParams indicates a Gravity value of CENTER_HORIZONTAL the view will attempt to center within the available space remaining in the Toolbar after all other elements have been measured.
  • An action menu: The menu of actions will pin to the end of the Toolbar offering a few frequent, important or typical actions along with an optional overflow menu for additional actions.

Different types of Toolbar examples:

Android: Ripple Touch Effect

Introduced Ripple touch effect with Material design in Android 5.0 (API level 21).

Touch feedback in material design provides an instantaneous visual confirmation at the point of contact when users interact with UI elements. The default touch feedback animations for buttons use the new RippleDrawable class, which transitions between different states with a ripple effect.




So in general, ripple effect for regular buttons will work by default and for other touchable views can be achieved by specifying 


android:background="?android/attr:selectableItemBackground"

But if we have custom button with selected state, our text color changes depending on the state, etc. So default button background is not going to work for us here. We 
can add this feedback for own drawables and for custome buttons by simply wrapping them in a ripple element:

For Example:

<ImageButton
         android:width="wrap_content"
         android:height="wrap_content"
         android:background="ripple_selector"
</ImageButton>


ripple_selector.xml

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
            android:color="@color/accent_dark">
    <item>
         <shape
                   android:shape="oval">
                  <solid android:color="?android:colorAccent" />
         </shape>
    </item>
</ripple>



Android Lollipop Changes

Applications targeting this or a later release will get these new changes in behavior:
  • Context.bindService now requires an explicit Intent, and will throw an exception if given an implicit Intent.
  • Notification.Builder will not have the colors of their various notification elements adjusted to better match the new material design look.
  • Message will validate that a message is not currently in use when it is recycled.
  • Hardware accelerated drawing in windows will be enabled automatically in most places.
  • Spinner throws an exception if attaching an adapter with more than one item type.
  • If the app is a launcher, the launcher will be available to the user even when they are using corporate profiles (which requires that the app use LauncherApps to correctly populate its apps UI).

Android 5.0 Introduction

Lollipop (5.0) is the Latest version of the Android.API level is 21

Major Changes:

Material Design:
You can create apps with material design that are visually dynamic and have UI element transitions that feel natural to users.

This support includes:
  • Material theme
  • View shadows
  • Recycle View and Card View widgets
  • Custom activity animation transitions
Notifications:
Android 5.0 supports notification details to display on the lock screen if desired by the user.

Connectivity:
Android 5.0 adds new APIs that allow apps to perform concurrent operations with Bluetooth Low Energy (BLE), allowing both scanning (central mode) and advertising (peripheral mode).

Graphics:
Android 5.0 also introduces the Android Extension Pack (AEP), a set of OpenGL ES extensions that give you access to features like tessellation shaders, geometry shaders, ASTC texture compression, per-sample interpolation and shading, and other advanced rendering capabilities. With AEP you can deliver high-performance graphics across a range of GPUs.


Multimedia:
Advanced Camera , audio and video apis

Sensors:Added tilt detector sensor will help to improve activity recognition and
heart rate sensor reports the heart rate of the device touching the device.

Browser:
Android 5.0 updates the WebView implementation to Chromium M37, bringing security and stability enhancements, as well as bug fixes.

Tools:
Developer tools for battery usage and Scheduling jobs

Android L: RecyclerView tutorial

RecyclerView is the new UI widget available in Android L Preview support library.

  • It is more advanced and flexible version of ListView
  • This widget is a container for large sets of views that can be recycled and scrolled very efficiently
  • It enforces the implementation of ViewHolder pattern
  • RecyclerView includes a new kind of adapter RecyclerView.Adapter class.

To use RecyclerView, you have to use

  • RecyclerView.Adapter : To create an adapter, you extend the RecyclerView.Adapter class
  • LayoutManager : Recycling views in this manner improves performance by avoiding the creation of unnecessary views or performing expensive findViewById lookups
  • ItemAnimator: Animates items when they are added, removed or reordered
  • ItemDecoration: Draws decorations around or on top of each item’s view

Android L Preview changes

Android L Preview Features

  1. A New Design - Material Design
  2. A New Runtime - ART (Android Runtime)
  3. Enhanced Notifications
  4. Project Volta- Increased Efficiency
  5. Support for OpenGL ES 3.1
  6. Android Extension Pack
  7. Camera API for advanced camera capabilities
  8. Storage - Directory selection
  9. Multiple network connections
  10. Bluetooth broadcasting
  11. NFC enhancements
  12. Scheduling jobs
  13. Enterprise - Managed provisioning
  14. Render PDF as bitmap
  15. Testing and accessibility improvements
  16. Easier switching between input languages
New Packages
android.app.job
android.bluetooth.le
android.hardware.camera2
android.hardware.camera2.params
android.media.session
android.media.tv
android.service.voice
android.system