WELCOME

WELCOME TO TECH-VILLA.

SEARCH YOUR TECHNICAL STUFF

OR

BROWSE THROUGH LABELS IN SIDEBAR

HAPPY BLOGGING.

Saturday, July 16, 2011

How to send JSON data on server in Android.

Below is the link to my answer on stackoverflow.com. There is a code snippet which you can use directly to POST JSON data to webservice or remote server.
Here is the Link.: Click Here..

Thanks
Ninad.

Sunday, March 6, 2011

android- how to invoke email client in android and add multiple attachments to it.

This is very common task in android to invoke email client from your application. there are many tutorial available out there about how to invoke email client in apps and add attachment to email client....


but i faced the real prob when i had to attach multiple attachments in email. i had searched a lot and finally implemented it correctly.


So here is a short tutorial on how to add multiple attachments to email client in android


you will have to start with a Intent with Intent.ACTION_SEND_MULTIPLE
 //here is the intent
final Intent ei = new Intent(Intent.ACTION_SEND_MULTIPLE);
//set the type of data
//application/octet-stream gives only email options. messaging and bluetooth will not show up in the application chooser
ei.setType("application/octet-stream");
// set the email address, you want in "To" field
ei.putExtra(Intent.EXTRA_EMAIL, new String[] {"me@somewhere.nodomain"});
// add subject
ei.putExtra(Intent.EXTRA_SUBJECT, "That one works");
 
Now here comes the main part which will attach the multiple attachments in email
 
first we will make an array list of Uris...as follows .....
ArrayList<Uri> uris = new ArrayList<Uri>();
 suppose u have to attach two files then take there locations and parse them to Uris....File fileIn1 = new File(filepath1);
        Uri u1 = Uri.fromFile(fileIn1);
File fileIn2 = new File(filepath2);
        Uri u2 = Uri.fromFile(fileIn2);
 now we will add these Uri to array list
 
uris.add(u1);
uris.add(u2);
 // add these uris to intent.
ei.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); 
 
and just start the activity..... 
startActivityForResult(Intent.createChooser(ei, "Sending multiple attachment"), 12345);
 
So here's the complete code....
 
final Intent ei = new Intent(Intent.ACTION_SEND_MULTIPLE);
ei.setType("application/octet-stream");
ei.putExtra(Intent.EXTRA_EMAIL, new String[] {"me@somewhere.nodomain"});
ei.putExtra(Intent.EXTRA_SUBJECT, "That one works");
 
ArrayList<Uri> uris = new ArrayList<Uri>();

File fileIn1 = new File(filepath1);
        Uri u1 = Uri.fromFile(fileIn1);
File fileIn2 = new File(filepath2);
        Uri u2 = Uri.fromFile(fileIn2);
uris.add(u1);
ei.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivityForResult(Intent.createChooser(ei, "Sending multiple attachment"), 12345);
 
So done with the multiple attachments in the email clien from android app.... 
 
 
 
 
 
 

Saturday, February 26, 2011

how to make a list in android/ List View in android/ Adapters in android

ListView is one of the most commonly used View in android. I observed that most of the newbie in android find it very difficult to implement it.

So lets start our work with a simple code example...
here are the steps.
  1. Make a class that extends ListActivity.
  2. Make your own custom Adapter class. Now what this custom adapter class do is set our data and inflate layout on for listview.
  3. Override the required set of methods in your custom adapter class.
  4. that is it.....still have confusions? It seems there are lots of confusions. Then have a look at the code snippet. This might solve all your confusions......

public class ListExample extends ListActivity {//extend your class with ListActivity.
 
    private MyAdapter mAdapter;//Make an instance of your custom Adapter Class(MyAdapter).
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAdapter = new MyAdapter();
        for (int i = 0; i < 124; i++) {
            mAdapter.addItem("Data " + i);//Add some Data to your adapter.
        }
        setListAdapter(mAdapter);//now set this adapter to your list
    }
 /**this is your custom adapter class.
*/
    private class MyAdapter extends BaseAdapter {
 
        private ArrayList mData = new ArrayList();
        private LayoutInflater mInflater;
 
        public MyAdapter() {
            mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }
 
        public void addItem(final String item) {
            mData.add(item);
            notifyDataSetChanged();
        }
 
        @Override
        public int getCount() {//this defines the size of the list.
            return mData.size();
        }
 
        @Override
        public String getItem(int position) {
            return mData.get(position);
        }
 
        @Override
        public long getItemId(int position) {
            return position;
        }
       /**getView is called internally by adapter to generate the view of he rows of the list*/ 
 
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            System.out.println("getView " + position + " " + convertView);
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item1, null);
                holder = new ViewHolder();
                holder.textView = (TextView)convertView.findViewById(R.id.text);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder)convertView.getTag();
            }
            holder.textView.setText(mData.get(position));
            return convertView;
        }
 
    }
 
    public static class ViewHolder {
        public TextView textView;
    }
}
 now the most important method in this adapter class is getView method. if you want to make your own custom list containing some images, text etc then you will have to override this method properly.

What this method does is it generates the view of each row of list, as soon as you scroll the list. getView gets called each time you move through the list. It inflates the new layout from the layout resources or uses the recycled layout from the recycler to generate the view of list rows. 
Here comes the new word 'Recycler'. Actually what android does is it stores only those items in the memory which are visible+ one view in Recycler.
As we run our application, this getView method will be called and it  inflates layout from resources for all the visible items. But when  we scroll through our list, the item that scrolls out of the screen moves to Recyler.
then to display next item getView does not inflate layout  from resources but it uses the view that has already been stored in Recycler.

when there is no view available in the recycler then the parameter "convertView" in the getView method gets a null value. So that we can inflate layout on the basis of this value.

 I found an Image on Internet, i thought this would help you to visualize the  getView properly.


We did the similar thing in the above example......