Wednesday, 24 September 2014

how to zoom_in animation in android

res/animation/zoom_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" >

    <scale
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:duration="2000"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="3"
        android:toYScale="3" >
    </scale>

</set>

res/layout/activity_zoom_in.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
   
    <ImageView android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher/>
   
    <Button android:id="@+id/btn_Start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start Animation/>
   
</RelativeLayout>

MainActivity.java

package example.animations;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements AnimationListener {

    ImageView image;
    Button btn_Start;
    Animation animationZoom_in;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zoom_in);

        image= (ImageView) findViewById(R.id.image);
        btn_Start= (Button) findViewById(R.id.btn_Start);

      
        animationRotate= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.zoom_in);
        animationRotate.setAnimationListener(this);

        btn_Start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            
               image.startAnimation(animationZoom_in);
            }
        });

    }

    @Override
    public void onAnimationEnd(Animation animation) {
             if (animation == animationZoom_in) {
        }

    }
    @Override
    public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub

    }
    @Override
    public void onAnimationStart(Animation animation) {
        // TODO Auto-generated method stub

    }

}

how to move animation in android

res/animation/move.xml

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:fillAfter="true">

   <translate
        android:fromXDelta="0%p"
        android:toXDelta="75%p"
        android:duration="800" />
</set>

res/layout/activity_move.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
   
    <ImageView android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher/>
   
    <Button android:id="@+id/btn_Start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start Animation/>
   
</RelativeLayout>

MainActivity.java

package example.animations;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements AnimationListener {

    ImageView image;
    Button btn_Start;
    Animation animationMove;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_move);

        image= (ImageView) findViewById(R.id.image);
        btn_Start= (Button) findViewById(R.id.btn_Start);

      
        animationRotate= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.move);
        animationRotate.setAnimationListener(this);

        btn_Start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            
               image.startAnimation(animationMove);
            }
        });

    }

    @Override
    public void onAnimationEnd(Animation animation) {
             if (animation == animationMove) {
        }

    }
    @Override
    public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub

    }
    @Override
    public void onAnimationStart(Animation animation) {
        // TODO Auto-generated method stub

    }

}

Sunday, 14 September 2014

how to rotate animation in android

res/animation/rotate.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <rotate
        android:duration="600"
        android:fromDegrees="0"
        android:interpolator="@android:anim/cycle_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:repeatMode="restart"
        android:toDegrees="90" />

</set>

res/layout/activity_rotate.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
   
    <ImageView android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher/>
   
    <Button android:id="@+id/btn_Start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start Animation/>
   
</RelativeLayout>

MainActivity.java

package example.animations;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity implements AnimationListener {

    ImageView image;
    Button btn_Start;
    Animation animationRotate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rotate);

        image= (ImageView) findViewById(R.id.image);
        btn_Start= (Button) findViewById(R.id.btn_Start);

      
        animationRotate= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.rotate);
        animationRotate.setAnimationListener(this);

        btn_Start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            
               image.startAnimation(animationRotate);
            }
        });

    }

    @Override
    public void onAnimationEnd(Animation animation) {
             if (animation == animationRotate) {
        }

    }
    @Override
    public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub

    }
    @Override
    public void onAnimationStart(Animation animation) {
        // TODO Auto-generated method stub

    }

}

Monday, 8 September 2014

how to get IP Address in android


IPAddress.java

package com.example.ip;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
 
  try {
   for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();)
   {
              NetworkInterface intf = en.nextElement();
              for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();)
              {
                  InetAddress inetAddress = enumIpAddr.nextElement();
                  if (!inetAddress.isLoopbackAddress())
                  {
                  TextView tv_ip= (TextView) findViewById(R.id.tv_ipaddress);
                  tv_ip.setText(inetAddress.getHostAddress());
                  }
              }
          }
  } catch (Exception e)
  {
   Log.e("******", e.toString());
  }
 }

AndroidManifest

    <uses-permission android:name="android.permission.INTERNET" />

Tuesday, 12 August 2014

how to use http post method in android


call following method

executeMultipartPost("username","password","https://your post url");

use this method

 public static void executeMultipartPost(String username, String password, String url) {

            try {

                HttpClient client = new DefaultHttpClient();
                HttpPost poster = new HttpPost(url);

                MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

                entity.addPart("username", new StringBody(username));

                entity.addPart("password", new StringBody(password));

                 poster.setEntity(entity);

                client.execute(poster, new ResponseHandler<Object>() {
                   
                public Object handleResponse(HttpResponse response)throws ClientProtocolException, IOException {

                        HttpEntity respEntity = response.getEntity();
                        String jsonData = EntityUtils.toString(respEntity);
                       
                        Log.d("Response:json", jsonData);
               
                       return null;
                    }
               
                });
            } catch (Exception e) {
                // do something with the error
                System.out.println("!!!!!!!!!!!!"+e);
            }
        }

Friday, 1 August 2014

how to post method request in android example


public void postRequest() {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("Your URL Type");
    try
{       
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "123"));
        nameValuePairs.add(new BasicNameValuePair("name", "Hiiii"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        Log.i("Response---",response );
    } catch (ClientProtocolException e)
 {
        // TODO Auto-generated catch block
    }
catch (IOException e)
{
        // TODO Auto-generated catch block
    }
}

Friday, 25 July 2014

how to check internet connection in android

ConnectionDetector.java

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class ConnectionDetector {

    private Context _context;

    public ConnectionDetector(Context context){
        this._context = context;
    }

  
    public boolean isConnectingToInternet(){
        ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
          if (connectivity != null)
          {
              NetworkInfo[] info = connectivity.getAllNetworkInfo();
              if (info != null)
                  for (int i = 0; i < info.length; i++)
                      if (info[i].getState() == NetworkInfo.State.CONNECTED)
                      {
                          return true;
                      }

          }
          return false;
    }
}

AlertDialogManager.java

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

public class AlertDialogManager {

    @SuppressWarnings("deprecation")
    public void showAlertDialog(Context context, String title, String message,
            Boolean status) {
        AlertDialog alertDialog = new AlertDialog.Builder(context).create();

        // Setting Dialog Title
        alertDialog.setTitle(title);

        // Setting Dialog Message
        alertDialog.setMessage(message);

        if (status != null)
            // Setting alert dialog icon
            alertDialog.setIcon((status) ? R.drawable.ic_launcher
                    : R.drawable.ic_launcher);

        // Setting OK Button
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

                // finish();

                dialog.cancel();

            }
        });

        // Showing Alert Message
        alertDialog.show();
    }
}

MainActivity.java

public class MainActivity extends Activity {

    public static Context con;
    static ConnectionDetector cd;
    static AlertDialogManager alert = new AlertDialogManager();

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

         con = MainActivity.this;

         cd = new ConnectionDetector(getApplicationContext());

        if (!cd.isConnectingToInternet()) {

            alert.showAlertDialog(con, "Internet Connection Error","Please connect to working Internet connection", false);

        } else {
                           -----your operation-----
        }
}