31 Oct 2013

Android 4.4, KitKat

Break off a piece of KitKat, the latest version of Android.



Beautiful & Immersive
Faster multitasking
Android 4.4 takes system performance to an all-time high by optimising memory usage in every major component. That means you can run more of your favourite apps and switch between them faster than ever.
Smart & Simple
The future is calling
The new phone app automatically prioritises your contacts based on the people that you talk to the most. You can also search for nearby places and businesses, your contacts or people in your Google Apps domain.
A smarter caller ID
Whenever you get a call from a phone number that's not in your contacts, your phone will look for matches from businesses with a local listing on Google Maps.
All your messages in the same place
Never miss a message, no matter how your friend sends it. With the new Hangouts app, all of your SMS and MMS messages are together in the same app, alongside your other conversations and video calls. And with the new Hangouts, you can even share your location and send animated GIFs.
Grab & Go
Print wherever, whenever
Now you can print photos, documents and web pages from your phone or tablet. You can print to any printer connected to Google Cloud Print, to HP ePrint printers and to other printers that have apps in the Google Play Store.
Check out android.com

Google Nexus 5 Officially Announced with Android 4.4 [On Sale in USA for $349, priced in India]

The Google Nexus 5 was finally announced today, after numerous leaks over the last few weeks, some of which from the company itself.
Nexus 5
The LG-made smartphone is already on sale in the USA, and boasts Android 4.4 KitKat, and is priced at a slightly higher point than last year’s Nexus 4. But that’s no suprise seeing as how the specs have been souped up as well, boasting a 4.95 inch 1080p Gorilla Glass 3 display, 2.2Ghz quad-core Snapdragon 800, 2GB of RAM, wireless charging (Qi) to charge the 2300 mAh battery, Slimport, LTE (in certain regions) Bluetooth 4.0, WiFi and NFC. There’s also a 8 Megapixel camera at the back which features OIS as well as a gyro, and a 1.3 Megapixel camera at the front. Phew.
Nexus 5
The design is back to plastic again sadly, letting go of the glass construction used last year. There’s also 16 or 32GB of internal memory, but no microSD card slot or removable battery. Android 4.4 brings about a couple changes as well, including the always-listening ‘OK Google’ command from the Moto X, as you can see in the new Nexus 5 commercial below:
On sale in the USA in both black and white colors for $349 (16GB) or $399 (32GB). Coming to India soon at a price of Rs 28,999 for the 16GB version, and Rs 32,999 for the 32GB version.
I gotta say, I am a big fan of the LG G2, and seeing as the Nexus 5 borrows so much from it spec-wise, this thing is going to be a beast!

29 Oct 2013

Custom Listview in Android

Now we tend to produce a custom list view example with custom layout of list row and knowledge to bind custom list row in list view. 

So, we tend to begin with Activity.

* produce new android Application Project.

* Add list view in activity_main.xml
After adding list view activity_main seems like this.

<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"
    tools:context=".MainActivity" >

<ListView
       android:id="@+id/lv_items"
        android:layout_width="match_parent"
         android:layout_height="match_parent"
        android:divider="#000"
        android:dividerHeight="1dp" >
  </ListView>
</RelativeLayout>

* Now we will declare how each row in this ListView should be displayed by creating a new xml file – custom_row.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp" >

    <TextView
        android:id="@+id/txt_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dp"
        android:textColor="#000" />

    <TextView
        android:id="@+id/txt_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_toLeftOf="@id/txt_category_money"
        android:textColor="#000" />

</RelativeLayout>

Now create a Contact class. and add this code.

public class Contact {
private String name;
private String contact_no;

public Contact (String name, String contact_no){
this.name = name;
this.contact_no = contact_no;
}

public String getName(){
return this.name;
}
public String getContact(){
return this.contact_no;
}
}

So, now, how do I tie all of this together? The MainActivity class, the listview layout and the row layout. we need an Adpater object.
So create CustomAdapter class that extends from ArrayAdapter<Contact>
Add following code in CustomAdapter class.

public class CustomAdapter extends ArrayAdapter<Contact>{

private Context context;
public CustomAdapter(Context context, ArrayList<Contact> list) {
super(context, R.layout.custom_row, list);
this.context = context;
}
class Holder{
TextView name,contact_no;
}
public View getView(int position, View convertView, ViewGroup parent){
Holder holder;
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.custom_row, null);
holder = new Holder();
holder.name = (TextView)convertView.findViewById(R.id.txt_1);
holder.contact_no = (TextView)convertView.findViewById(R.id.txt_2);
convertView.setTag(holder);
} else {
holder = (Holder)convertView.getTag();
}
if(getItem(position) != null){
holder.name.setText(getItem(position).getName());
holder.contact_no.setText(getItem(position).getContact());
}
return convertView;
}
}

Now come to MainActivity.java file. Add following code in onCreate() method after setContentView(R.layout.activity_main);

ArrayList<Contact> list = new ArrayList<Contact>();
list.add(new Contact("Abc", "123"));
list.add(new Contact("Bcd", "234"));
list.add(new Contact("Cde", "345"));
list.add(new Contact("Def", "456"));
list.add(new Contact("Efg", "567"));
list.add(new Contact("Fgh", "678"));
list.add(new Contact("Ghi", "789"));
list.add(new Contact("Hij", "890"));

final CustomAdapter adapter = new CustomAdapter(this, list);
ListView lv = (ListView)findViewById(R.id.lv_items);
lv.setAdapter(adapter);

//set click listener of ListView
lv.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> list, View v, int position, long id) {
String name = adapter.getItem(position).getName();
String number = adapter.getItem(position). getContact();
Toast.makeToast(MainActivity.this , "Name: "+ name + "\nNumber: "+ number, Toast.LENGTH_SHORT).show();
}

});

How to use ExpandableListView

Working with ExpandableListView is very complex, so to make things simpler I will explain you how to use this ListView in few simple steps. This example will display 3 category of vehicles in a ExpandableList.

Firstly we will create the Layout files for Main Activity, GroupView and ChildView Layout.
MainActivity: main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <ExpandableListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" >
    </ExpandableListView>
</LinearLayout>


GroupView group_layout.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
     <ImageView android:id="@+id/ImageView01" 
                android:layout_height="40dip" android:layout_width="40dip" android:layout_marginLeft="40dip"></ImageView>
        <TextView android:id="@+id/tvGroup" android:layout_width="fill_parent"
                android:layout_height="45dip" android:text="Groups" android:gravity="center_vertical|right"
                android:paddingLeft="5dip" android:paddingRight="5dip"
                android:textColor="#ffffffff" android:textStyle="bold"
                android:textSize="17dip"></TextView>
</LinearLayout>

ChildView: child_layout.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ImageView android:id="@+id/ImageView01" 
                android:layout_height="40dip" android:layout_width="40dip" android:layout_marginLeft="40dip"></ImageView>
        <TextView android:layout_width="fill_parent"
                android:layout_height="45dip" android:paddingLeft="5dip"
                android:paddingRight="5dip" android:textStyle="bold" android:textSize="17dip"
                android:gravity="center_vertical" android:id="@+id/tvChild"
                android:text="Children" android:textColor="#ffCCCC22"></TextView>
</LinearLayout>


MainActivity: ExpandableListViewAppActivity.java


import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.Toast;

public class ExpandableListViewAppActivity extends Activity implements Runnable {
private ExpandableListAdapter adapter;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     // Retrive the ExpandableListView from the layout
        ExpandableListView listView = (ExpandableListView) findViewById(R.id.listView);
        
        listView.setOnChildClickListener(new OnChildClickListener()
        {
            
            @Override
            public boolean onChildClick(ExpandableListView arg0, View arg1, int arg2, int arg3, long arg4)
            {
                Toast.makeText(getBaseContext(), "Child clicked", Toast.LENGTH_LONG).show();
                return false;
            }
        });
        
        listView.setOnGroupClickListener(new OnGroupClickListener()
        {
            
           
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Group clicked", Toast.LENGTH_LONG).show();
return false;
}
        });

        // Initialize the adapter with blank groups and children
        // We will be adding children on a thread, and then update the ListView
        adapter = new ExpandableListAdapter(this, new ArrayList<String>(),
                new ArrayList<ArrayList<Vehicle>>());

        // Set this blank adapter to the list view
        listView.setAdapter(adapter);
        Thread thread = new Thread(this);
        thread.start();
    }
@Override
public void run() {
// TODO Auto-generated method stub
 final int ITEMS = 15;
        int count = 0;
        while (count != ITEMS)
        {
            count++;
            adapter.addItem(MockDataProvider.getRandomVehicle("Vehicle no. " + count));

            // Notify the adapter
            handler.sendEmptyMessage(1);
            try
            {
                // Sleep for two seconds
                Thread.sleep(2000);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
}
 private Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            adapter.notifyDataSetChanged();
            super.handleMessage(msg);
        }
    };
}

BaseExpandableListAdapter: ExpandableListAdapter.java


import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter{

@Override
    public boolean areAllItemsEnabled()
    {
        return true;
    }

    private Context context;

    private ArrayList<String> groups;
    private ArrayList<ArrayList<Vehicle>> children;
    public ExpandableListAdapter(Context context, ArrayList<String> groups,
            ArrayList<ArrayList<Vehicle>> children) {
        this.context = context;
        this.groups = groups;
        this.children = children;
    }

    /**

     * A general add method, that allows you to add a Vehicle to this list
     * 
     * Depending on if the category opf the vehicle is present or not,
     * the corresponding item will either be added to an existing group if it 
     * exists, else the group will be created and then the item will be added
     * @param vehicle
     */
    public void addItem(Vehicle vehicle) {
        if (!groups.contains(vehicle.getGroup())) {
            groups.add(vehicle.getGroup());
        }
        int index = groups.indexOf(vehicle.getGroup());
        if (children.size() < index + 1) {
            children.add(new ArrayList<Vehicle>());
        }
        children.get(index).add(vehicle);
    }

    @Override

    public Object getChild(int groupPosition, int childPosition) {
        return children.get(groupPosition).get(childPosition);
    }

    @Override

    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    
    // Return a child view. You can load your custom layout here.
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
        Vehicle vehicle = (Vehicle) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.child_layout, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
        tv.setText("   " + vehicle.getName());

        // Depending upon the child type, set the imageTextView01

        tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        /*if (vehicle instanceof Car) {
            tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.car, 0, 0, 0);
        } else if (vehicle instanceof Bus) {
            tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.bus, 0, 0, 0);
        } else if (vehicle instanceof Bike) {
            tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.bike, 0, 0, 0);
        }*/
        return convertView;
    }

    @Override

    public int getChildrenCount(int groupPosition) {
        return children.get(groupPosition).size();
    }

    @Override

    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    @Override

    public int getGroupCount() {
        return groups.size();
    }

    @Override

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    // Return a group view. You can load your custom layout here.

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
            ViewGroup parent) {
        String group = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.group_layout, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.tvGroup);
        tv.setText(group);
        return convertView;
    }

    @Override

    public boolean hasStableIds() {
        return true;
    }

    @Override

    public boolean isChildSelectable(int arg0, int arg1) {
        return true;
    }
}

Vehicle.java

public class Vehicle {
private String name;
    private String group;
    public String getGroup() {
        return group;
    }
    public void setGroup(String group) {
        this.group = group;
    }
    public Vehicle(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

MockDataProvider.java

import java.util.Random;
public class MockDataProvider {
// A utility method that generates random Vehicles
    public static Vehicle getRandomVehicle(String name) {
        Vehicle vehicle = null;
        Random random = new Random();
        int type = random.nextInt(3);
        switch (type) {
            case 0:
                vehicle = new Car(name);
                break;
            case 1:
                vehicle = new Bus(name);
                break;
            case 2:
                vehicle = new Bike(name);
                break;
        }
        return vehicle;
    }
}

Car.java,Bus.java, Bike.java

public class Bike extends Vehicle{
public Bike(String name) {
        super(name);
        setGroup("Bikes");
    }
}