Sunday 21 July 2013

android listview adapter getview() called multiple times

Hello Droid Guys,
Today , I found a strange issue in android custom listView adapter. I found that
the adapter getview() method called multiple times.

adapter getview() called multiple times

On my listview I am showing following Item:
1. User facebook image
2. His|Her Name
3. Some description

As, I told you that getview() method calls multiple time, due to this behavior the Image on
list item some times remain unchanged or same profile picture will be attach on different
user , in listview.
First of all I think that this was an issue of lazy image loader library But I was wronged.
After spent few times on google for searching this issue , I found this link

http://stackoverflow.com/questions/2618272/custom-listview-adapter-getview-method-being-called-multiple-times-and-in-no-co

Then After I checked my xml file and I found I am using height = wrap_content in listview.

<ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="3dp"
        android:layout_marginRight="3dp"
        android:padding="5dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
         android:layout_below="@+id/facebook_friend_list_header" >
    </ListView>

And when I changed this to fill_parent

        android:layout_height="fill_parent"

then all problem is resolved and now the getview() method will be called only once.
Also the user profile picture issue will be resolved.

Android Guys , still I was not cleared about the above tricks. Please let me know why
getview() method called multiple times when I am using
android:layout_height="wrap_content" . If some one know the reason then
please let me know.

Thanks in advance.
Enjoy Coding :)

Friday 12 July 2013

ActionBarSherlock with custom View | Changing sherlock action bar menu item background

Hello Friends,
Today , I am going to share my another android tutorial which is based on the
Sherlock Action Bar. ABS(Action Bar Sherlock) is an support library which helps
to use action bar design pattern over all android devices.

Initially , Action bar is supportable over android version 3.0. But if you want to
provide the interactive user interface on lower version of device too, then
ABS comes in a role.

 
sherlock action barandroid action bar
       

1. MainActivity.java

package com.example.actionbar;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.SubMenu;

public class MainActivity extends SherlockActivity {

 private SubMenu mGoItem;

 private static final int GO_ITEM_ID = 1;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  ActionBar ab = getSupportActionBar();
  ab.setBackgroundDrawable(getApplicationContext().getResources()
    .getDrawable(R.drawable.bg_titlebar_tile));
  ab.setLogo(getResources().getDrawable(R.drawable.logo));
  ab.setDisplayShowTitleEnabled(false);
 }

 @SuppressLint("InlinedApi")
 @Override
 public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {

  mGoItem = menu.addSubMenu(0, GO_ITEM_ID, 0, null);
  mGoItem.setIcon(R.drawable.setting_icon);
  // SubMenu sub = menu.addSubMenu("Theme");
  mGoItem.add(0, R.style.Theme_Sherlock, 0, "View Profile");
  mGoItem.add(0, R.style.Theme_Sherlock_Light, 0, "Account Privacy");
  mGoItem.add(0, R.style.Theme_Sherlock_Light_DarkActionBar, 0,
    "Logout");
  mGoItem.getItem().setShowAsAction(
    MenuItem.SHOW_AS_ACTION_ALWAYS
      | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

  return true;
 }

 @SuppressLint("InlinedApi")
 @Override
 public boolean onOptionsItemSelected(
   com.actionbarsherlock.view.MenuItem item) {

  switch (item.getItemId()) {
  case GO_ITEM_ID:
   Toast.makeText(MainActivity.this,
     "You have Pressed 'Setting' Menu Button", Toast.LENGTH_LONG)
     .show();
   return true;
  }

  return false;
 }

}

2.activity_main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textColor="@android:color/black"
        android:textStyle="bold"
        android:text="@string/hello_action_bar" />

</RelativeLayout>


Download the complete source code: Here

 

Copyright @ 2013 Android Developers Blog.