Pages

Wednesday, 30 January 2013

Android - Make ImageView's transparent area not clickable


ImageView :
ImageView imgView= (ImageView) findViewById(R.id.color_blue);
imgView.setDrawingCacheEnabled(true);
imgView.setOnTouchListener(changeColorListener); 


OnTouchListener:

private final OnTouchListener changeColorListener = new OnTouchListener() {

  @Override
  public boolean onTouch(final View v, MotionEvent event) {

   Bitmap bmp = Bitmap.createBitmap(v.getDrawingCache());
   int color = 0;
   try {
    color = bmp.getPixel((int) event.getX(), (int) event.getY());
   } catch (Exception e) {
    // e.printStackTrace();
   }
   if (color == Color.TRANSPARENT)
    return false;
   else {

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
     //do something here
     break;
    case MotionEvent.ACTION_OUTSIDE:
     break;
    case MotionEvent.ACTION_CANCEL:
     break;
    case MotionEvent.ACTION_MOVE:
     break;
    case MotionEvent.ACTION_SCROLL:
     break;
    case MotionEvent.ACTION_UP:
     //do something here
     break;
    default:
     break;
    }
    return true;

   }
  }
 };

Friday, 25 January 2013

Android rate your app functionality

use below code to launch rating page on google play.


mContext.startActivity(new Intent(
                                        Intent.ACTION_VIEW, Uri
                                                .parse("market://details?id="
                                                        + APP_PACKAGE_NAME)));

Thursday, 24 January 2013

android twitter login implementation

below is code to login twitter.

requirement:
lib need to use : using twitter 4j 
make twitter app at dev consolve of twitter

 private void parseUrl(String url) {
  try {
   if (url == null)
    return;
   if (url.startsWith(General.TWITTER_CALLBACK_URL)) {
    url += "&";

    String oauth_verifier = com.qtm.sign2learn.utils.Util.extractPattern(url,
      "oauth_verifier=(.*?)&");

    AccessToken accessToken = twitter.getOAuthAccessToken(
      requestToken, oauth_verifier);

    twitter.setOAuthAccessToken(accessToken);

    User user = twitter.verifyCredentials();

    String name = user.getName();

    String miniImage = user.getMiniProfileImageURL();
    String bigImage = user.getBiggerProfileImageURL();

    String fbUid = String.valueOf(user.getId());

    Editor edit = prefs.edit();
    edit.putString(General.pref_KEY_OAUTH_TOKEN,
      accessToken.getToken());
    edit.putString(General.pref_KEY_OAUTH_SECRET,
      accessToken.getTokenSecret());
    edit.putString(General.pref_TWITTER_ACCESS_TOKEN,
      accessToken.getToken());
//    edit.putString(General.pref_fb_uid, fbUid);
//    edit.putInt(General.pref_acctype, 2);
    prefs.getBoolean(General.pref_KEY_TWITTER_LOGIN, true);
    edit.commit();
    
    if (progressDialog != null && progressDialog.isShowing()) {
     progressDialog.dismiss();
    }
    loginDialog.dismiss();
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 private class TwitterWebViewClient extends WebViewClient {
  @Override
  public void onPageStarted(WebView view, String url, Bitmap favicon) {
   super.onPageStarted(view, url, favicon);
   parseUrl(url);

   progressDialog.show();

  }

  @Override
  public void onPageFinished(WebView view, String url) {
   super.onPageFinished(view, url);
   progressDialog.dismiss();
  }
 }

 public void loginToTwitter(View v) {

  TwitterFactory factory = new TwitterFactory();

  twitter = factory.getInstance();
  twitter.setOAuthConsumer(General.TWITTER_CONSUMER_KEY,
    General.TWITTER_CONSUMER_SECRET);
  try {
   requestToken = twitter
     .getOAuthRequestToken(General.TWITTER_CALLBACK_URL);

   loginDialog = new Dialog(this,
     android.R.style.Theme_Translucent_NoTitleBar);

   loginDialog.setContentView(R.layout.webview_popup);

   Button btnClose = (Button) loginDialog.findViewById(R.id.btnClose);

   btnClose.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
     // TODO Auto-generated method stub
     loginDialog.dismiss();
    }
   });

   WebView webview = (WebView) loginDialog
     .findViewById(R.id.webViewVKLogin);
   webview.getSettings().setJavaScriptEnabled(true);
   webview.clearCache(true);

   webview.setWebViewClient(new TwitterWebViewClient());

   // otherwise CookieManager will fall with
   // java.lang.IllegalStateException:
   // CookieSyncManager::createInstance()
   // needs to be called before CookieSyncManager::getInstance()
   CookieSyncManager.createInstance(this);

   CookieManager cookieManager = CookieManager.getInstance();
   cookieManager.removeAllCookie();

   webview.loadUrl(requestToken.getAuthenticationURL());

   loginDialog.show();

   // this.startActivity(new Intent(Intent.ACTION_VIEW, Uri
   // .parse(requestToken.getAuthenticationURL())));
  } catch (TwitterException e) {
   e.printStackTrace();
  }

 }

In general.java put below variables
 // Twitter oauth urls and details
 public static final String TWITTER_CALLBACK_URL = "url here";
 public static final String TWITTER_CONSUMER_KEY = "key here";
 public static final String TWITTER_CONSUMER_SECRET = "secreat here";

 public static final String URL_TWITTER_AUTH = "auth_url";
 public static final String URL_TWITTER_OAUTH_VERIFIER = "oauth_verifier";
 public static final String URL_TWITTER_OAUTH_TOKEN = "oauth_token";
 
 public static final String PREFERENCE_NAME = "twitter_oauth";
 public static final String pref_KEY_OAUTH_TOKEN = "sing2learn_oauth_token";
 public static final String pref_KEY_OAUTH_SECRET = "sing2learn_oauth_token_secret";
 public static final String pref_KEY_TWITTER_LOGIN = "sing2learn_isTwitterLogedIn";
 public static final String pref_TWITTER_ACCESS_TOKEN = "sing2learn_twitter_token";
 



android facebook login implementation

use below code for facebook login.

----- FOR NEW SDK-----

https://developers.facebook.com/docs/android/getting-started/facebook-sdk-for-android/

-----FOR OLD SDK------
you need to use facebook sdk. you can download from developer.facebook.com


private void facebookLogin() {

  facebook = new Facebook(General.FB_APPID);// ((GlobalVars)getApplicationContext()).facebook;

  String access_token = prefs.getString(General.PREFS_FB_token, null);
  Long expires = prefs.getLong(General.PREFS_FB_expires, 0);
  Log.d("MyTag", "token:" + access_token);
  if (access_token != null) {
   facebook.setAccessToken(access_token);
  }
  if (expires != 0) {
   facebook.setAccessExpires(expires);
  }
  /*
   * Only call authorize if the access_token has expired.
   */

  if (!facebook.isSessionValid()) {
   final Editor edit = prefs.edit();
   Log.d("MyTag", "In Authorize");
   facebook.authorize(this,
     new String[] { "publish_stream", "email" },
      new DialogListener() {
      public void onComplete(Bundle values) {
       Log.d("fb login complete",
         "fb login complete");
       Log.d("MyTag",
         "face token: " + facebook.getAccessToken());
       edit.putString(General.PREFS_FB_token,
         facebook.getAccessToken());
       
       edit.putLong(General.PREFS_FB_expires,
         facebook.getAccessExpires());
       edit.commit();
       Toast.makeText(SettingsActivity.this, "login success", Toast.LENGTH_LONG).show();
      }

      public void onFacebookError(FacebookError e) {
       Toast.makeText(getApplicationContext(),
         "onFacebookError", Toast.LENGTH_SHORT)
         .show();
      }

      public void onError(DialogError e) {
       Toast.makeText(getApplicationContext(), "onError",
         Toast.LENGTH_SHORT).show();
      }

      public void onCancel() {

      }
     });
  } else {
   Toast.makeText(this, "login success", Toast.LENGTH_LONG).show();
  }
 }

android play audio/video intent

Audio play intent


Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + filePath),
"audio/*");
startActivity(intent);

video play intent


Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + filePath),
"video/*");
startActivity(intent);