Sunday, August 26, 2012

Set Button Visibility By clicking with only one Button ID


package com.demo.button;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;

public class DemoButtonVisibilityActivity extends Activity
{

Button mButton;
Button mButton2;
Boolean mBoolean=true;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mButton=(Button)findViewById(R.id.btn1);
        mButton.setClickable(false);

        mButton2=(Button)findViewById(R.id.btn2);
        mButton2.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event)
{
if(event.getAction()==MotionEvent.ACTION_DOWN)
{
       mButton.setClickable(true);
       mButton.setOnClickListener(new View.OnClickListener()
       {

@Override
public void onClick(View v)
{
if(mBoolean)
{
mBoolean=false;
mButton.setBackgroundResource(R.drawable.icon2);
}
else if (mBoolean==false)
{
mBoolean=true;
mButton.setBackgroundResource(R.drawable.icon);
}

}
});
}
return false;
}
});
     
    }
}

Thursday, August 23, 2012

Open Website in mobile


Intent mIntent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.perceptionsystem.com/"));

startActivity(mIntent);

To kill all process


android.os.Process.killProcess(android.os.Process.myPid());

AudioPalyer Class


package androidexperts.apps.jigsaw;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.graphics.AvoidXfermode;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnVideoSizeChangedListener;
import android.net.Uri;
import android.view.SurfaceHolder;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.MediaController.MediaPlayerControl;

@SuppressWarnings("unused")
public class AudioPlayer
{
String fileName;
Context contex;
Uri path;
MediaPlayer mp;
public AudioPlayer( String name, Context context)
{
fileName=name;
contex=context;
AssetManager am = contex.getAssets();

path = Uri.fromFile(new File("assets/"));

playAudio();

}

public void playAudio()
{
mp = new MediaPlayer();

try {
AssetFileDescriptor descriptor = contex.getAssets().openFd(fileName);
mp.setDataSource( descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength() );
descriptor.close();
mp.prepare();
   mp.start();
   mp.setVolume(5, 5);
 
       
   }
catch (IllegalArgumentException e)
{
  e.printStackTrace();


}
catch (IllegalStateException e)
{

e.printStackTrace();


   }
catch (IOException e)
{
e.printStackTrace();

}
}
public void stop()
{
mp.stop();
}


}

Clearing Bitmap Method


private void unbindDrawables(View view)
{
if (view.getBackground() != null)
{
view.getBackground().setCallback(null);
}
try
{
((ViewGroup) view).removeAllViews();
}
catch (Exception e)
{
e.printStackTrace();
}

}

Close Dialog After Some Second


final Timer mtimer_dialog_dismiss = new Timer();
mtimer_dialog_dismiss.schedule(new TimerTask() {
public void run() {
mDialog.dismiss();
mtimer_dialog_dismiss.cancel();
}
}, 7000);

Animation to Control


Animation anim_text_congrate = AnimationUtils.loadAnimation(ClassName, R.anim.winnermessage);
dialog_text_congreats.startAnimation(anim_text_congrate);

Saturday, August 18, 2012

Parser


package androidexperts.app.realestate;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import android.content.Context;
import android.graphics.BitmapFactory;

import com.google.gson.Gson;

public class PropertyPostParse {

public Context mContext;
public boolean isNetError = false;

public boolean isNetError() {
return isNetError;
}

public void setNetError(boolean isNetError) {
this.isNetError = isNetError;
}

public PropertyPostParse() {

}

public PropertyPostParse(Context context) {
mContext = context;
}

/**
* Function checks internet if avail, Post data to particular Url and filled
* json object relating to the response.
*
* @param string
* @param nameValuePairs
* @param object
* @param context
* @return object
* @throws CustomException
* */
public Object postHttpURL(String Url, List<NameValuePair> nameValuePairs,
Object mObject) {

HttpPost httppost;
HttpParams httpParameters;
int timeoutConnection = 30000;
HttpClient httpclient = null;
HttpResponse response = null;
String data = "";
Gson mGson = new Gson();

Object mFillObject = null;

try {
mFillObject = mObject.getClass().newInstance();
isNetError = false;
httppost = new HttpPost(Url);
httpParameters = new BasicHttpParams();

HttpConnectionParams
.setSoTimeout(httpParameters, timeoutConnection);
httpclient = new DefaultHttpClient(httpParameters);

if (nameValuePairs != null) {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
}
// Execute HTTP Post Request
response = httpclient.execute(httppost);
data = EntityUtils.toString(response.getEntity());
System.out.println("Final Data :" + data);
mFillObject = mGson.fromJson(data, mFillObject.getClass());

} catch (IllegalAccessException e) {
isNetError = true;
System.out.println("IllegalAccessException : " + e.toString());
e.printStackTrace();
} catch (InstantiationException e) {
isNetError = true;
System.out.println("InstantiationException : " + e.toString());
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
isNetError = true;
System.out.println("Unsupported EncodingException : "
+ e.toString());
e.printStackTrace();
} catch (ClientProtocolException e) {
isNetError = true;
System.out.println("ClientProtocolException : " + e.toString());
e.printStackTrace();
} catch (IOException e) {
isNetError = true;
System.out.println("IOException : " + e.toString());
e.printStackTrace();
}

return mFillObject;
}

public String postHttpURL(String Url, List<NameValuePair> nameValuePairs) {

HttpPost httppost;
HttpParams httpParameters;
int timeoutConnection = 30000;
HttpClient httpclient = null;
HttpResponse response = null;
String data = "";
try {

httppost = new HttpPost(Url);
httpParameters = new BasicHttpParams();

HttpConnectionParams
.setSoTimeout(httpParameters, timeoutConnection);
httpclient = new DefaultHttpClient(httpParameters);

if (nameValuePairs != null) {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
}
// Execute HTTP Post Request
response = httpclient.execute(httppost);
data = EntityUtils.toString(response.getEntity());
System.out.println("Final Data :" + data);
} catch (Exception e) {
isNetError = true;
System.out.println("Exception : " + e.toString());
}

return data;
}

private String getResizedBitmap(String path, int REQUIRED_SIZE) {
try {
File f = new File(path);
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(new FileInputStream(f).getFD(),
null, o);
int scale = 1;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
if (width_tmp > REQUIRED_SIZE) {
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
} else
scale = 1;
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
BitmapFactory.decodeFileDescriptor(new FileInputStream(f).getFD(),
null, o2);
return path;
} catch (Exception e) {
isNetError = true;
}
return null;
}

/**
* method for send images to webservice
* @param nameValuePairs
* @param mObject
* @return
*/
public Object postHttpURL(List<NameValuePair> nameValuePairs, Object mObject) {
HttpPost httppost;
HttpParams httpParameters;
int timeoutConnection = 30000;
HttpClient httpclient = null;
HttpResponse response = null;
Object filledObject = null;
Gson gson;

try {
isNetError = false;

try {
filledObject = mObject.getClass().newInstance();
} catch (IllegalAccessException e) {
isNetError = true;
} catch (InstantiationException e) {
isNetError = true;
}
String Url = "http://192.168.0.225/real_estate/customerpropertys/add";

httppost = new HttpPost(Url);
httpParameters = new BasicHttpParams();

HttpConnectionParams
.setSoTimeout(httpParameters, timeoutConnection);
httpclient = new DefaultHttpClient(httpParameters);

MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);

if (nameValuePairs != null) {
for (int index = 0; index < nameValuePairs.size(); index++) {
String paramName = nameValuePairs.get(index).getName();
String paramValue = nameValuePairs.get(index).getValue();
System.out.println("paramName :" + paramName
+ " paramValue :" + paramValue);

String image = (String) paramName.subSequence(0,
paramName.length() - 1);

if (image.equalsIgnoreCase("image")) {
// If the key equals to "image", we use FileBody to
// transfer the data
if (paramValue.length() > 0) {
paramValue = getResizedBitmap(paramValue, 400);
entity.addPart(paramName, new FileBody(new File(
paramValue)));
}
} else {
// Normal string data
entity.addPart(nameValuePairs.get(index).getName(),
new StringBody(nameValuePairs.get(index)
.getValue()));
}
}
httppost.setEntity(entity);
}
// Execute HTTP Post Request
response = httpclient.execute(httppost);
String data = EntityUtils.toString(response.getEntity());

if (data.startsWith("[")) {
data = "{\"resources\": " + data + "}";
}
System.out.println("Final Data :" + data);
gson = new Gson();
filledObject = gson.fromJson(data, mObject.getClass());

} catch (Exception e) {
e.printStackTrace();
isNetError = true;
}

return filledObject;
}
}

Friday, August 17, 2012

Play Video Online In Android

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("http://www.yourvideo.mp4"), Req_code);
startActivityForResult(intent);