Wednesday, 15 February 2017

Email Intent with Attachment Android Example | Attach File in Email Intent

Hello Friends, Today I m going to share Email Intent with Attachment in Android Example. In this Intent you will be able to send an attachment in your default Email app with a choose and a check to verify if any Email App exits or not. Detail Explanation is given in comments. 

In this example we had used everything which are needed in Email. If you have any Suggestion then leave in Comment. To Attach file in Email in Android you have send the URI of the File, as you can't send the whole file. Android recommend to use URI as sending file from One Activity to Another increase the Size of App and can cause crash, if it crosses the limit provided by Android to the App.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public static void sendFileToMailIntent(Context context, final String filePath, String
            subject) {
        // For More Android Methods www.androidwarriors.com
        Uri path = Uri.fromFile(new File(filePath));

        //This is added to show only Email apps - part 1
        Intent emailIntent = new Intent(Intent.ACTION_SENDTO);

        // set the type to 'email'
        emailIntent.setType("vnd.android.cursor.dir/email");

        //This is added to show only Email apps - part 2
        String recepientEmail = ""; // either set to destination email or leave empty

//        String recepientEmail = "name@domain.com"; // name@domain.com will send mail to Self email

        emailIntent.setData(Uri.parse("mailto:" + recepientEmail));


        String emailsCC = "emailsCC@gmail.com"; // Enter emails to send CC(Carbon Copy) or array
        // to send Multiple

        String emailsBCC = "emailsBCC@gmail.com"; // Enter emails to send BCC(Blind Carbon Copy)
        // or array to send Multiple

        // Define the email cc and bcc
        emailIntent.putExtra(Intent.EXTRA_CC, emailsCC);
        emailIntent.putExtra(Intent.EXTRA_BCC, emailsBCC);

        //To add Multiple Sender in Email
//        String to[] = {"asd@gmail.com"};
//        emailIntent.putExtra(Intent.EXTRA_EMAIL, to);

        // the attachment
        emailIntent.putExtra(Intent.EXTRA_STREAM, path);
        // the mail subject
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "" + subject);
        if (emailIntent.resolveActivityInfo(context.getPackageManager(), 0) != null) {
//            context.startActivity(emailIntent);
            context.startActivity(Intent.createChooser(emailIntent, "Send email..."));

        } else {
            // if you reach this place, it means there is no any Email Applicaition Found
            Utility.showToast(context, "No Email application found! Please download one");
        }
    }

Snapshot Image :



Contact Us in comment if you need HELP. 

Thursday, 26 January 2017

firebase cloud messaging android example | firebase cloud messaging tutorial

Push notification using firebase cloud messaging in android

Hello warriors whats up guys...., hope you all are doing well..so  today i am going to share with you latest ,fastest and easiest tutorial of firebase cloud messaging android. earlier we were using google cloud messaging service for push notification in android but now everything is updated and time to use firebase cloud messaging for push notification in android. Since there is a lot of tutorial over internet for firebase cloud messaging but trust me this is going to be easiest one for you.

 firebase cloud messaging

So like every tutorial i am going to create a list of steps and we will follow these steps to complete this tutorial.

Firebase cloud messaging tutorial android using Android studio

  • Create a new project in Android studio
  • Open firebase assistant in Android studio
  • Connect your project with firebase
  • Set up firebase cloud messaging 
  • Create MyInstanceIdService extending FirebaseInstanceIdService
  • Create MyMessagingService extending FirebaseMessagingService
  • Create notification from message 
  • Send message from firebase console for testing
So lets start with our steps:-

1.Create a new project in your android studio like below image go to File>New>New project:

 Create a new project in android studio with your project name .

 firebase cloud messaging

2.In this step we are going to open firebase assistant in Android studio:- 

         to do this just go to Tools>Firebase and click on firebase like below image-

 firebase cloud messaging

after clicking on firebase a new side window will appear in right side of your Android studio like below image-

 firebase cloud messaging
click on Cloud messaging and a link will appear below the Cloud Messaging text setup firebase cloud messaging click on that it will open below screen -

 firebase cloud messaging
now we have complete first step after this everything is there but i will complete this tutorial with all the steps.

3.In this step we are going to connect our project with firebase :- 

to do this just click on Connect to firebase, if you are logged in inside your android studio then it will open a window of project configuration like below image-

 firebase cloud messaging

else it will take you to authentication page inside your browser,so make sure you are connected to internet. and after authentication above window will appear inside your android studio click on connect to firebase it will take sometime because it creates project in background and adds configuration file to your project automatically.

4.In this step we will add firebase cloud messaging to our project to do this just click on Add fcm to your app button in Assistant window of android studio like below image :


 firebase cloud messaging

after clicking on this a popup will appear to ask  Accept changes like below picture

 firebase cloud messaging

 click on Accept Changes and everything will be settled after some moment. now we are done with configuration part no need to go to Developer console everything is set inside our Android studio.

5. Create a java class name it MyInstanceIdService and extend FirebaseInstanceIdService in the class:- 

On initial startup of your app, the FCM SDK generates a registration token for the client app instance. If you want to target single devices, or create device groups, you'll need to access this token.so to access fcm registration token override onTokenRefresh inside the class. here is my MyInstanceIdService  class:


6.Create one more java class name it MyMessagingService and extend        FirebaseMessagingService in the class:-

In this class we will override onMessageReceived method to retrieve message sent from our server and then we need to write a method to show notification from retrieved message, here is my MyMessagingService class -




7. Add both Service MyInstanceIdService and MyMessagingSerice we had created in previous steps inside our manifest:-

Here is my manifest file-


everything is set now but in some case if you are not getting your registration token inside MyInstanceIdService then try to get it inside MainActivity using below code it will retrieve registration token and print it inside your log --


 String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d("fcmToken", "Refreshed token: " + refreshedToken);

8.Send message from firebase console for testing : 

Since we have completed this tutorial so now its time to test its working or not, to do this open firebase console inside your browser by clicking below link

Firebase developer console

click on Notification in left bottom of page like below image  

 firebase cloud messaging

click on new message button fill message body with your message and don't forget to select your project from drop down and then click on send i am sure you will receive your message.

Finally we have completed this tutorial i hope it was helpful for you if you are still facing problem comment below and don't forget to give your feedback. 

Saturday, 14 January 2017

Recyclerview with checkbox android example | checkbox in recyclerview android

Checkbox in Recyclerview Android | Recyclerview with checkbox android

Hello warriors..how are you guys ?? I hope you all are doing something great..So i m back after a long time with a cool and very helpful topic of internet Recyclerview with checkbox android. few month ago i was working on a project and i faced a very weird problem in recyclerview with checkbox ,checkbox unchecked when i scroll recyclerview and i know a lot of developer facing this problem at initial stage so in this post i am going to share with you solution of this problem.


I hope all of you know how to create Recyclerview with checkbox in android i am going to discuss only about android recyclerview checkbox scroll problem for this i have created a simple recyclerview with checkbox with 500 item.

So here is my activity_main.xml :-

here is my MainActivity.java :-

Finally here is my adapter class :-


To solve this problem you need to do follow three steps:
  • Create a HashMap in your adapter class of <Integer,Boolean> type.
  • Update HashMap inside checkChangeListener of Checkbox inside your ViewHolder.
  • Check whether checkbox is already checked or not inside onBindViewHolder.

All these steps are included in my adapter class. i hope this post will help you to solve android recyclerview  checkbox scroll problem if this is helpful for you don't forget to give your feedback in comment.you can download complete code from below download button.

https://codeload.github.com/kundansviet/Recyclerview-with-checkbox/zip/master


Friday, 24 June 2016

Android Image loading using Library Universal Image loader (UIL) | Image loading from Internet

Hello guys in this post i am going to share with you best library for image loading in android called Universal Image Loader , a lot libraries are available like Picasso,glide..for image loading from internet and SD card but I always prefer to use universal image loader.


So in this post i will make a Recyclerview of images and all images will be downloaded from  internet for that i will use ten static image url to download image and will show it in Recyclerview.

Like every post i will make module of steps and then we will follow that to complete this tutorial.

  1. Create a new project in your Android Studio. Go to File>>New>>New project.
  2. Add Universal Image Loader library and Recyclerview to build.gradle of app module.
  3. Add Internet permission in manifest file.
  4. Initialize Universal Image Loader inside MainActivity.
  5. Add Recyclerview in your activity_main.xml file.
  6. Create single row ui for your recyclerview.
  7. Create Adapter for Recyclerview if you are not familiar with recyclerview than must follow my post of Recyclerview.
  8. Set image into image of Recyclerview using Universal image loader.
  9.  Set Layout manager and adapter to Recyclerview inside MainActivity.

1.Open your android studio and create a new project. Go to File>>New>>New project like below image.



2.Open build.gradle file of app module and add two dependency to it one for Universal Image Loader and another for Recyclerview in simple way just copy and paste these two dependency.


compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'
    compile 'com.android.support:recyclerview-v7:24.0.0'

3.Since app will download image from internet so we need to add permission in manifest file here is manifest file.

4. To use Universal Image Loader in application we need to initialize it in our MainActivity so here is the code to Initialize Universal Image Loader in MainActivity. copy and paste below code in onCreate() of MainActivity.


1
2
 ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).build();
        ImageLoader.getInstance().init(config);


5.Since we are listing all image in Recyclerview so we need to add Recyclerview in our activity_main.xmkl file here is my activity_main.xml.

6.Create single row of Recyclerview ,since we are going to show only image so its very simple layout here is my single_row.xml.
7.Create Adapter for Recyclerview if you are not familiar with recyclerview than must follow my post of Recyclerview. So I am listing some steps follow these to complete your adapter class.

  • Create a java class name it anything in my case this is ListAdapter
  • Create another class inside ListAdapter class name it ListHolder.
  • extends RecyclerView.Adapter<ListAdapter.ListHolder> to ListAdapter where Adapter is of ListHolder type.
  • Implement all method of Recyclerview.Adapter .
  • Create a Constructor wtih single parameter of Context type.
  • Create four instance, one for Context,one for ImageLoader,one for DisplayImageOptions and one for LayoutInflater like below code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
    private ImageLoader imageLoader;
    private DisplayImageOptions options;
    private LayoutInflater inflater;
    private Context context;
    String[] images = {"https://2.bp.blogspot.com/-qdVGBrZpNEo/Vzs7Dyba8eI/AAAAAAAANXg/oQGukisNydIcTA733Q8qNX1xryCNrP1-gCLcB/s640/AndroidNotification.png",
            "https://2.bp.blogspot.com/-MtTDhb_9QGw/Vr9OXfE0hqI/AAAAAAAAMZo/e9Advd4xPmY/s1600/Googleplus.png",
            "http://3.bp.blogspot.com/-hEsy8I3DVrA/ViFFalr14kI/AAAAAAAAL74/nfZd4behDE0/s1600/material-tabs.png",
            "https://4.bp.blogspot.com/-fzCN1en9BXY/VslDs985vvI/AAAAAAAAMbU/JycBEflEJcI/s1600/sqlitedemo.png",
            "https://3.bp.blogspot.com/-0CvhPeUCDes/VtGVTINSNhI/AAAAAAAAMc8/CzGr-nlqgS8/s1600/FacebookIntegration.png",
            "http://2.bp.blogspot.com/-DpC34QctSsI/Vh6JZjWY-0I/AAAAAAAAL5c/-UgJEDKYays/s1600/pushNotification.png",
            "http://4.bp.blogspot.com/-8u_hqbGb30g/VjR9WflhCNI/AAAAAAAAMFs/namqhGH1AE0/s1600/material_mcs1_original.png",
            "http://1.bp.blogspot.com/-5nkpiqWiPkg/Vj9jWk0IyiI/AAAAAAAAMIQ/uEBdjXr3jCY/s1600/TwitterloginAndroid.png",
            "http://3.bp.blogspot.com/-oY46UYNjKFk/Vike7jlX05I/AAAAAAAAMBA/OjeJp6BAio8/s640/studio-hero.jpg",
            "http://4.bp.blogspot.com/-UgboLypyx9s/Vq5LeA35rbI/AAAAAAAAMXs/4fGJQPURYwg/s1600/seekbarinandroid.png"};
in above code i have also created an array of static image urls.
  • Now inside inside constructor initialize all instances created above.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
 public ListAdapter(Context context) {

        this.context=context;
        inflater=LayoutInflater.from(context);

        imageLoader = ImageLoader.getInstance();

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.place_holder)
                .showImageForEmptyUri(R.drawable.place_holder)
                .showImageOnFail(R.drawable.place_holder)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();


    }
  • Inside onCreateViewHolder inflate single_row.xml and return ViewHolder object like below code.

1
2
3
4
5
6
7
 @Override
    public ListHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view=inflater.inflate(R.layout.single_row,parent,false);
        ListHolder holder=new ListHolder(view);
        return holder;
    }

  • Now inside ListHolder class Create instance of ImageView and initialize it inside Constructor of ListHolder class like below code.

1
2
3
4
5
6
7
 class ListHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        public ListHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.image);
        }
    }

  • In this step set image to ImageView using Universal Image Loader and also set size of list in getItemCount method.

1
2
3
4
5
6
7
8
9
 @Override
    public void onBindViewHolder(ListHolder holder, int position) {
        imageLoader.displayImage(images[position], holder.imageView, options);
    }

    @Override
    public int getItemCount() {
        return images.length;
    }

in above code you can see imageLoder is used to set image in ImageView. and we completed code for Adapter class here is my complete code of ListAdapter class.

7.Now this is our last step very simple initialize and Rycylerview inside MainActivity and set LayoutManager and Adapter to it. here is my MainActivity.java

we have complete this tutorial , still having any problem feel free to comment and don't forget to give your feedback because it always motivate us to write new things , you can download complete code from below download button. in our next post we are going to show how to set image in to list from sdcard using Universal Image Loader so keep visiting.

https://codeload.github.com/kundansviet/Image-Loading-in-List-Using-Universal-Image-Loader/zip/master

Saturday, 18 June 2016

Android Broadcastreceiver Tutorial | Broadcastreceiver in Android Example

Hey warriors whats going on guys , In this post I am going to share with you very cool functionality of android system called BroadcastReceiver in android. A broadcast receiver (short receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens. so without making more gossips I am going to start with coding.


I am going to create a sample for location broadcast ,it will generate a notification when user will turn on/off  GPS. you can check list of all system Broadcast here .So I am going to make module of steps to complete this tutorial.

  • Create new project in your Android studio. Go to File>>New>>New project
  • Add Broadcast Receiver to your manifest file.
  • Create Receiver class to receive your Broadcast.
  • Generate notification in onReceive of Receiver class you can do anything else. 
1.Create new project in your android studio Go to File>>New>>New project like below image.



2.Add Broadcast Receiver to your manifest file. Since I am making Broadcast for location so i am adding Intent filter for location. add below code to your manifest file inside application tag.

1
2
3
4
5
 <receiver android:name=".MyReciever">
            <intent-filter>
                <action android:name="android.location.PROVIDERS_CHANGED" />
            </intent-filter>
        </receiver>
In above code .MyReciver is java class i have created to listen broadcast. here is my manifest file.

3.Create a new java class to receive broadcast I am creating this class MyReciever and extend BroadcastReceiver class then it will ask you to implement method onReceive(). like below code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public class MyReciever extends BroadcastReceiver {

    
    @Override
    public void onReceive(Context context, Intent intent) {
       
    }

  

}

4. Inside onReceive() I am using code to check gps is on or off because broadcast only notify that location provider is changed so you to write code to check gps is on or off then generate notification. If you don't know How to create notification in android then check my tutorial notification in android. Here is my complete code of MyRciever class.

Finally we have completed this tutorial you can download complete demo code from below download button. Turn on/off you your gps and you will receive notification.if this post was helpful for your don't forget to comment below because its always motivate us to write new things.

https://codeload.github.com/kundansviet/BroadcastReceiverDemo/zip/master