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

Saturday, September 6, 2014

Android L: UI Widgets : RecyclerView

RecyclerView is a 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. Use the RecyclerView widget when you have lists with elements that change dynamically.

RecyclerView is easy to use, because it provides:
  • A layout manager for positioning items
  • Default animations for common item operations
You also have the flexibility to define custom layout managers and animations for this widget.

To use the RecyclerView widget, we have to specify an adapter and a layout manager.

LayoutManager

The LayoutManager is probably the most interesting part of the RecyclerView. This class is responsible for the layout of all child views. There is one default implementation available: LinearLayoutManager which you can use for vertical as well as horizontal lists. To create a custom layout, you extend the RecyclerView.LayoutManager class.

// use a linear layout manager
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);


// improve performance if you know that changes in content
// do not change the size of the RecyclerView
mRecyclerView.setHasFixedSize(true);

Animations

Animations for adding and removing items are enabled by default in RecyclerView.
use the RecyclerView.setItemAnimator method for animations, to customize just extend the RecyclerView.ItemAnimator class

Android provide DefaultItemAnimator and we can customize according to our needs.
mRecyclerView.setItemAnimator(new DefaultItemAnimator());

Adapter:

To create an adapter, you extend the RecyclerView.Adapter class.
The RecyclerView.Adapter class contains few notifyItemXXXX()methods. Which help us in modifying the View.

Two methods used in the example are insert and remove and they are
notifyItemInserted(position);
notifyItemRemoved(position);

mRecyclerView.setAdapter(mAdapter);

// insert or remove item from the view. This methods are in the adapter class.

public void addToList(String name) {
int position = 0;
if (mDataset.size() > 1) {
  position = new Random().nextInt(mDataset.size() - 1);
}
mDataset.add(position, name);
notifyItemInserted(position);
}

public void removeItemFromList(String name) {
int position = mDataset.indexOf(name);
if (position != -1) {
  mDataset.remove(position);
  notifyItemRemoved(position);
}
}



Source Code
You can download the source code by clicking here: Sample RecyclerView .  This project is built using eclipse IDE. Unzip and import the project into Eclipse, it’s a good idea to use the Project by clean and rebuild from the project menu.

Screenshot:
Initial screen with four items.

Added two rows to the recyclerView
Removed one item which is added from RecyclerView
Thanks for reading :) 
Whether this post is helpful?


Have something to add to this post? If you have any other quick thoughts/hints that you think people will find useful? Share it in the comments and feedback are welcome

Friday, September 5, 2014

Android: RecyclerView cannot be resolved to a type

RecyclerView cannot be resolved to a type? How to resolve it?

We need recyclerview-v7 Support Library to solve the issue. but how to get? letc check...

Check whether Recyclerview is existing in the SDK or not because it is a part of the support library.

If you're not find the folder “recyclerview-v7” in the following sdk directory "..\sdk\extras\android\m2repository\com\android\support" then follow the steps otherwise follow from step 3.

Steps to achieve:
  1. Select the latest Android SDK Tools, Platform-tools, and Build-tools.
  2. Select latest Android Support Library & Android Support Repository.
  3. After updating Android Support Repository from SDK Manager go to..\sdk\extras\android\m2repository\com\android\support\recyclerview-v7\21.0.0-rc1
  4. Unzip recyclerview-v7-21.0.0-rc1.aar
  5. After unzipping recyclerview-v7-21.0.0-rc1.aar you will get classes.jar
  6. rename classes.jar to recyclerView.jar for feature reference and add to your Android app under /lib folder
Overview:
RecyclerView updates and replaces ListView. It's claimed to be easier to use and feature performance improvements in dynamic views.

RecyclerView is easy to use, because it provides:
  • A layout manager for positioning items
  • Default animations for common item operations

The most important classes of the RecyclerView API.

Class
Usage
Adapter
The Adapter is also responsible for making a View for each item in the data set.
LayoutManager
A layout manager positions item views inside a RecyclerView and determines when to reuse item views that are no longer visible to the user.
ItemAnimator
Animations for adding and removing items are enabled by default in RecyclerView



Thanks for reading :)
Any suggestions can be added in comments. feedback are welcome.

Thursday, September 4, 2014

Android: Notification in L

Notification in Android L:

In L, notifications receive an important structural visual and functional update:
  • Visual changes to notifications as part of material design
  • Notifications are now available on the device lock screen, yet sensitive content can still be hidden behind it
  • A new presentation format called Heads-up for receiving high priority notifications while using the device
  • Cloud-synced notifications - act on a notification on your Android tablet and it is also dismissed on your phone.
  • And starting now (in Android 4.4W, API Level 20, the platform release for Android Wear), your notifications will bridge to Android Wear devices. You can extend the functionality of notifications on Wear in two different ways. First, you can add speech input and canned responses to Actions on Wear, allowing users to complete tasks from their wrists. Second, you can write Wear apps that hook into your notifications to go even further in terms of creating interactive experiences for users.

For more information of notifications ref android notifications.

Sample Code for showing notification

      /**  
       * Send a sample notification using the NotificationCompat API.  
       */  
      public void sendNotification(View view) {  
           Intent intent = new Intent(  
                     Intent.ACTION_VIEW,  
                     Uri.parse("http://developer.android.com/reference/android/app/Notification.html"));  
           PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,  
                     intent, 0);  
           NotificationCompat.Builder builder = new NotificationCompat.Builder(  
                     this);  
           builder.setSmallIcon(R.drawable.ic_stat_notification);  
           // Set the intent that will fire when the user taps the notification.  
           builder.setContentIntent(pendingIntent);  
           builder.setAutoCancel(true);  
           builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),  
                     R.drawable.ic_launcher));  
           builder.setContentTitle("BasicNotifications Sample");  
           builder.setContentText("Time to learn about notifications!");  
           builder.setSubText("Tap to view documentation about notifications.");  
           NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);  
           notificationManager.notify(NOTIFICATION_ID, builder.build());  
      }  

Output:



In first pic, we can see the notification received in the status bar.
On drag of screen from top. We can see, how the basic notification appears.


Wednesday, September 3, 2014

Quick glance of Android L features

Android L

Android L is a next version of kitkat, Coming with new designs, features and updates...

Android L is Faster, better looking and more efficient.

Android L is going to change the way it looks with material design- These projects extend Android to the TV (Android TV), to the car (Android Auto) and to wearables (Android Wear), among others.

See the API overview for more information on the rest of the new and updated features. Lets look a quick view of all the API's.