Advantage of ViewHolder pattern in Android!!

Every time when the adapter calls getView() method, the findViewById() method is also called. This is a very intensive work for the mobile CPU and so affects the performance of the application and the battery consumption increases. ViewHolder is a design pattern which can be applied as a way around repeated use of findViewById().

A ViewHolder holds the reference to the id of the view resource and calls to the resource will not be required after you “find” them: Thus performance of the application increases.

ViewHolder Class:

private static class ViewHolder {
  final TextView text;
  final TextView timestamp;
  final ImageView icon;
  final ProgressBar progress;

  ViewHolder(TextView text, TextView timestamp, ImageView icon, ProgressBar progress) {
    this.text = text;
    this.timestamp = timestamp;
    this.icon = icon;
    this.progress = progress;
  }
}

GetView to use ViewHolder object using view.getTag to avoid the use of findviewById again and again to improve performance.

public View getView(int position, View convertView, ViewGroup parent) {
  View view = convertView;
  if (view == null) {
    view = // inflate new view
    ViewHolder holder = createViewHolderFrom(view);
    view.setTag(holder);  
  }
  ViewHolder holder = view.getTag();
  // TODO: set correct data for this list item
  // holder.icon.setImageDrawable(...)
  // holder.text.setText(...)
  // holder.timestamp.setText(...)
  // holder.progress.setProgress(...)
  return view;
}

create ViewHolder object :

private ViewHolder createViewHolderFrom(View view) {
    ImageView icon = (ImageView) view.findViewById(R.id.listitem_image);
    TextView text = (TextView) view.findViewById(R.id.listitem_text);
    TextView timestamp = (TextView) view.findViewById(R.id.listitem_timestamp);
    ProgressBar progress = (ProgressBar) view.findViewById(R.id.progress_spinner);

    return new ViewHolder(text, timestamp, icon, progress);
}

View.setTag(Object) allows you to tell the View to hold an arbitrary object. If we use it to hold an instance of our ViewHolder after we do our findViewById(int) calls, then we can use View.getTag() on recycled views to avoid having to make the calls again and again.

source: stackoverflow

How do you handle Bitmaps in Android as it takes too much memory?

There are a number of reasons why loading bitmaps in your Android app is tricky:

  • Bitmaps can very easily exhaust an app’s memory budget.
  • Loading bitmaps on the UI thread can degrade your app’s performance, causing slow responsiveness or even ANR messages.
  • If your app is loading multiple bitmaps into memory, you need to skillfully manage memory and disk caching.

For most cases, Android recommend that you use the Glide library to fetch, decode, and display bitmaps in your app. Glide abstracts out most of the complexity in handling these and other tasks related to working with bitmaps and other images on Android.

source: developer.android.com

Refactor Package name in Android studio

For Example, if you want to change package from com.example.test.myapplication to com.example.animation.myapplication  .

Do below steps:

  1. Select below option from project pane.

    Capture1

    fig 1

  2. Toggle Compact Empty Middle Packages/ Hide Empty Middle Packages as below

Untitled1

fig 2

3. When Toggle compact package name to Hide Empty packages package name will broken up in individual name like below.

Capture2

fig 3

4. Now Click on individual directories and select Refactor using right click on directory name which you want to change or use Shift + F6 then select rename package.

Capture3

Fig 4

5. Now Rename your package

Capture4

fig 5

6. Select Refactor and do refactor from Find refactoring Preview  in bottom of Android studio.

Capture5

Now your package will rename in all places no need to worry!

Thanks !!