Friday 18 January 2013

Get Display Size in Android

Get Display Size in Android


Hello Friends,

now a days android device are come with different resolution and different screen size and it very difficult to design android application for that design dynamically layout we need to get display size for that,
hear is code for get  display size of screen in android



DisplayMetrics metrics = this.getResources().getDisplayMetrics();
              int width = metrics.widthPixels;
              int height = metrics.heightPixels;




we get device Height and Width from this code

Tuesday 8 January 2013

Drag,Move ImageView in Android


Hello Friends Here is Code for image drag and move

move image view in android anywhere in device screen for that here some code for that move image view using single touch,
drag image view in android

java Code 

import android.app.Activity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;

public class MainActivity extends Activity {

       int windowwidth;
       int windowheight;

       private LayoutParams layoutParams;

       @Override
       public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);

              windowwidth = getWindowManager().getDefaultDisplay().getWidth();
              windowheight = getWindowManager().getDefaultDisplay().getHeight();
              final ImageView img = (ImageView) findViewById(R.id.imageView1);

              img.setOnTouchListener(new View.OnTouchListener() {

                     @Override
                     public boolean onTouch(View v, MotionEvent event) {
                           LayoutParams layoutParams = (LayoutParams) img
                                         .getLayoutParams();
                           switch (event.getAction()) {
                           case MotionEvent.ACTION_DOWN:
                                  break;
                           case MotionEvent.ACTION_MOVE:
                                  int x_cord = (int) event.getRawX();
                                  int y_cord = (int) event.getRawY();

                                  if (x_cord > windowwidth) {
                                         x_cord = windowwidth;
                                  }
                                  if (y_cord > windowheight) {
                                         y_cord = windowheight;
                                  }

                                  layoutParams.leftMargin = x_cord - 25;
                                  layoutParams.topMargin = y_cord - 75;

                                  img.setLayoutParams(layoutParams);
                                  break;
                           default:
                                  break;
                           }
                           return true;
                     }
              });
       }
}



========================================================
layout.xml



<LinearLayout 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"
     >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ball" />

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/space_icon" />

</LinearLayout>



thanks Enjoy...!

Monday 7 January 2013

Check Special Character Exist,Is Null,Is empty in android


Here Are Some Function for validation


if value has any special character in that function return value "true" otherwise "false"

       public boolean HasSpecialCharacter(String value)

       {
              for (int i = 0; i < value.length(); i++) {
                     if (!Character.isLetterOrDigit(value.charAt(i)))
                           return true;
              }
              return false;
       }


==============================================
if value has NULL function return value "true" otherwise "false"


        public boolean IsNull(Object value) {
         if (value != null)
          return true;

         return false;
        }



Note:- You can pass any object in this function (eg. TextView,EditText,....)
===================================================
if value has no data & null value in that  function return value "true" otherwise "false"

        public static boolean IsEmpty(String value)

        {
         if (IsNull(value)) {
          return true;
         } else {
          if (value.trim().length() <= 0) {
           return true;
          }
         }
         return false;
        }


Check Internet connectivity in Android

hello friends here is function that check your device is corrected to internet or not


       public boolean isOnline() {
              ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
              NetworkInfo netInfo = cm.getActiveNetworkInfo();

              if (netInfo != null && netInfo.isConnectedOrConnecting()
                           && cm.getActiveNetworkInfo().isAvailable()
                           && cm.getActiveNetworkInfo().isConnected()) {
                     return true;
              }
              return false;
       }


there another method you want to use 


       public static boolean haveNetworkConnection(final Context context) {
              boolean haveConnectedWifi = false;
              boolean haveConnectedMobile = false;

              final ConnectivityManager cm = (ConnectivityManager) context
                           .getSystemService(Context.CONNECTIVITY_SERVICE);
              if (cm != null) {
                     final NetworkInfo[] netInfo = cm.getAllNetworkInfo();
                     for (final NetworkInfo netInfoCheck : netInfo) {
                           if (netInfoCheck.getTypeName().equalsIgnoreCase("WIFI")) {
                                  if (netInfoCheck.isConnected()) {
                                         haveConnectedWifi = true;
                                  }
                           }
                           if (netInfoCheck.getTypeName().equalsIgnoreCase("MOBILE")) {
                                  if (netInfoCheck.isConnected()) {
                                         haveConnectedMobile = true;
                                  }
                           }
                     }
              }

              return haveConnectedWifi || haveConnectedMobile;
       }


=============================================================


         if (haveNetworkConnection(Activity.this))
           {
                //Conneced
           }
       else
           {
                //No Connected
           }





Note:- you also give User Permission
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Check Email Id Is Valid,Email-id Validation in Android


Here some method for check email id is valid or not
it will return "true" if email id is valid otherwise return "false",you can pass string on this function and get that string is valid email id or not
here some method that gives you passing string is contain valid email id or not

1).

        public static boolean isEmailValid(String email) {
              boolean isValid = false;
              String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
              CharSequence inputStr = email;
              Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
              Matcher matcher = pattern.matcher(inputStr);
              if (matcher.matches()) {
                     isValid = true;
              }
              return isValid;
       }



2).



boolean checkEmailCorrect(String Email) {
              String pttn = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
              Pattern p = Pattern.compile(pttn);
              Matcher m = p.matcher(Email);

              if (m.matches()) {
                     return true;
              }
              return false;
       }


3).


    public boolean isEmailValid(String email) {

              boolean flag;
              String expression ="[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
                           "\\@" +
                           "[a-zA-Z0-9][a-zA-Z0-9\\-]{1,64}" +
                           "(" +
                           "\\." +
                           "[a-zA-Z0-9][a-zA-Z0-9\\-]{1,25}" +
                           ")+" ;

              CharSequence inputStr = email.trim();
              Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
              Matcher matcher = pattern.matcher(inputStr);
             
              if (matcher.matches())
                     flag = true;
              else
              {
             
                     flag=false;
              }
              return flag;

       }
////////////////////////////////////////////////////////=====================
you can use this finction like below

if(isEmailValid("abc@abc.com"))
{
do some code for valid email id
}
else
{
code for invalid email ids
}






thanks 

Send Email With Attachment in Android (*** Working***)



Hello Friends,

send email from android device with attachment using intent here is the code for that

This code is perfectly working for me to send email with  image attachment

****+++++++++++++++++++++++++++++++++++++++++++*****


              Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
              emailIntent.setType("jpeg/image");
              emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
                           new String[] { "" });
              emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "subject");
              Date cal = Calendar.getInstance().getTime();
              emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "body");

              Uri uri = Uri.fromFile(new File(Environment
                           .getExternalStorageDirectory(), "/sunil.jpg"));

              emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
              emailIntent.setType("text/plain");

              startActivity(Intent.createChooser(emailIntent, "Send mail..."));