Saturday, October 4, 2014

Share on Facebook, gmail, twitter via Intent in android

Share via Facebook

 Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
   shareIntent.setType("text/plain");
   shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String) v.getTag(R.string.app_name));
   shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String) v.getTag(R.drawable.ic_launcher));

   PackageManager pm = v.getContext().getPackageManager();
   List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
     for (final ResolveInfo app : activityList) 
     {
         if ((app.activityInfo.name).contains("facebook")) 
         {
           final ActivityInfo activity = app.activityInfo;
           final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
          shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
          shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
          shareIntent.setComponent(name);
          v.getContext().startActivity(shareIntent);
          break;
        }
      }
 
 
 Share via Twitter
 
  Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
   shareIntent.setType("text/plain");
   shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String) v.getTag(R.string.app_name));
   shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String) v.getTag(R.drawable.ic_launcher));

   PackageManager pm = v.getContext().getPackageManager();
   List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
     for (final ResolveInfo app : activityList) 
      {
        if ("com.twitter.android.PostActivity".equals(app.activityInfo.name))
          {
             final ActivityInfo activity = app.activityInfo;
             final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
             shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
             shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
             shareIntent.setComponent(name);
             v.getContext().startActivity(shareIntent);
            break;
          }
        }
  Share via Gmail
 
 
 Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
  shareIntent.setType("text/plain");
  shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String) v.getTag(R.string.app_name));
  shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String) v.getTag(R.drawable.ic_launcher));

   PackageManager pm = v.getContext().getPackageManager();
   List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
       for (final ResolveInfo app : activityList) 
        {
          if ((app.activityInfo.name).contains("gmail")) 
           {
             final ActivityInfo activity = app.activityInfo;
             final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
            shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
             shareIntent.setComponent(name);
             v.getContext().startActivity(shareIntent);
             break;
           }
       } 
Attaching image on Gmail,Facebook,Twitter with Text  
 
 File filePath = context.getFileStreamPath("your.jpg");
 share("gmail",filePath.toString(),"text");
 share("facebook",filePath.toString(),"text");
 share("twitter",filePath.toString(),"text");


void share(String nameApp, String imagePath,String message) 
 { 
 try  
        { 
         List<Intent> targetedShareIntents = new ArrayList<Intent>(); 
 
 Intent share = new Intent(android.content.Intent.ACTION_SEND); 
     share.setType("image/jpeg"); List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0); 
 if (!resInfo.isEmpty()){
  for (ResolveInfo info : resInfo) 
 { Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND); 
 
         targetedShare.setType("image/jpeg");(info.activityInfo.packageName.toLowerCase().contains(nameApp) || info.activityInfo.name.toLowerCase().contains(nameApp)) { 
 
targetedShare.putExtra(Intent.EXTRA_SUBJECT, "Sample Photo"); targetedShare.putExtra(Intent.EXTRA_TEXT,message); targetedShare.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(imagePath)) ); targetedShare.setPackage(info.activityInfo.packageName); targetedShareIntents.add(targetedShare); } } Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share"); chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{})); startActivity(chooserIntent);  
} 
 
 } 
 catch(Exception e){ 
 Log.v("VM","Exception while sending image on" + nameApp + " "+ e.getMessage());    
  }
}

Saturday, April 5, 2014

Save & Display image in Gallery in Android

Save & Display image in Gallery

This method can use any where in your save image.
Just call  this image and pass params image name and Bitmap,
Then after,call this method addImageToGallery(file path, mcontex);
  
public static String saveImage(String image_name, Bitmap image_) {
        FileOutputStream image = null;
        // External sdcard location
        File mediaStorageDir = new File(
                Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                "Your dir");
        File mediaStorageDir = new File(
                Environment.getExternalStorageDirectory()
                        + "/Pictures/Your dir");

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {   
                mediaStorageDir.mkdir();               
            }
        }
        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                Locale.getDefault()).format(new Date());
        File mediaFile;
        mediaFile = new File(
                          mediaStorageDir, ""
                      + image_name + timeStamp  + ".png");
        try {        
            image = new FileOutputStream(mediaFile);
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        image_.compress(CompressFormat.PNG, 100, image);
        return mediaFile.getPath();
    }



public static void addImageToGallery(final String filePath, final Context context) {
        ContentValues values = new ContentValues();
        values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());
        values.put(Images.Media.MIME_TYPE, "image/jpeg");
        values.put(MediaStore.MediaColumns.DATA, filePath);
        context.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI, values);
    }



Thursday, February 20, 2014

Check Internet and also wifi network is workig in device

Check Internet and also wifi network is workig in device 


Step 1: Create Method


public static boolean isNetworkAvaliable(Context context ) {
        ConnectivityManager connectivityManager = (ConnectivityManager) ctx
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if ((connectivityManager
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE) != null && connectivityManager
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED)
                || (connectivityManager
                        .getNetworkInfo(ConnectivityManager.TYPE_WIFI) != null && connectivityManager
                        .getNetworkInfo(ConnectivityManager.TYPE_WIFI)
                        .getState() == NetworkInfo.State.CONNECTED)) {
            return true;
        } else {
            return false;
        }
    }

Step 2:  Get Value True or false in Bool value

             Boolean isinternetON = Utility.isNetworkAvaliable(context );  

Step 3:  give permission in your AndroidManifest.xml file.
         
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


Sunday, January 12, 2014

Bouncy Scrollview

Bouncy Scrollview Like Facebook Scrollview.


Create one class  BounceScrollView.java in src Folder.
- Basically That is custom view and that is extends to ScrollView 




package com.valkesh.utility;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ScrollView;

public class BounceScrollView extends ScrollView {
    private View inner;

    private float y;
    private Rect normal = new Rect();

    private boolean isCount = false;

    public BounceScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        if (getChildCount() > 0) {
            inner = getChildAt(0);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (inner != null) {
            commOnTouchEvent(ev);
        }

        return super.onTouchEvent(ev);
    }

    public void commOnTouchEvent(MotionEvent e) {
        int action = e.getAction();
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            break;
        case MotionEvent.ACTION_UP:
            if (isNeedAnimation()) {
                animation();
                isCount = false;
            }
            break;

        case MotionEvent.ACTION_MOVE:
            final float preY = y;
            float nowY = e.getY();
            int deltaY = (int) (preY - nowY);
            if (!isCount) {
                deltaY = 0;
            }

            y = nowY;
            if (isNeedMove()) {
                if (normal.isEmpty()) {
                    normal.set(inner.getLeft(), inner.getTop(),
                            inner.getRight(), inner.getBottom());
                }
                inner.layout(inner.getLeft(), inner.getTop() - deltaY / 2,
                        inner.getRight(), inner.getBottom() - deltaY / 2);
            }
            isCount = true;
            break;

        default:
            break;
        }
    }

    public void animation() {
        TranslateAnimation ta = new TranslateAnimation(0, 0, inner.getTop(),
                normal.top);
        ta.setDuration(200);
        inner.startAnimation(ta);
        inner.layout(normal.left, normal.top, normal.right, normal.bottom);

        normal.setEmpty();

    }

    public boolean isNeedAnimation() {
        return !normal.isEmpty();
    }

    public boolean isNeedMove() {
        int offset = inner.getMeasuredHeight() - getHeight();
        int scrollY = getScrollY();
        if (scrollY == 0 || scrollY == offset) {
            return true;
        }
        return false;
    }

}


Now, open your xml file as your view file.


  <com.valkesh.utility.BounceScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

                 Your view

 </com.valkesh.utility.BounceScrollView>


Wednesday, January 8, 2014

Opacity use with any ColorCode in persontage

Opacity with any ColorCode

Ex: Black color and white color with opacity code.

Below code for black:-
Black color [<color name="black">#000000</color>]
White color [<color name="white">#ffffff</color>]
  
Now if i want to use opacity than you can use below code :-
 
60% opacity with black color
<color name="black">#99000000</color>
 
50% opacity with white color 
<color name="white">#80ffffff</color>

YOU CAN USE ANY COLOR WITH OPACITY CODE.

and below for opacity code:-

Hex Opacity Values
100%  FF
95%  F2
90%  E6
85%  D9
80%  CC
75%  BF
70%  B3
65%  A6
60%  99
55%  8C
50%  80
45%  73
40%  66
35%  59
30%  4D
25%  40
20%  33
15%  26
10%  1A
5%  0D
0%  00