Redirect Call in Android

Web Hosting
In this tutorial, I will show you the code on how to redirect from your android apps to mobile call. Just see the code below.

String phoneCallUri = "tel:911";
Intent phoneCallIntent = new Intent(Intent.ACTION_DIAL);
phoneCallIntent.setData(Uri.parse(phoneCallUri));
 startActivity(phoneCallIntent);


Web Hosting That's it, Happy Coding.

Crop Image

Web Hosting
In this tutorial i will show you how to crop an image in your android apps. I just refer this tutorial from this site. Just see the code below.


First thing you need to do is to create an Android Project. Second is create a two layouts named crop_selector.xml and main.xml.

crop_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:gravity="center_vertical">

<ImageView
android:id="@+id/iv_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""/>

</LinearLayout>

main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
<TextView  
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content"
   android:layout_marginTop="10dp"
   android:layout_marginBottom="10dp"
   android:gravity="center"
   android:textStyle="bold"
   android:textSize="17sp"
   android:text="How to select and crop image"/>
   
<Button 
android:id="@+id/btn_crop"
android:layout_width="match_parent"  
android:layout_height="wrap_content"
android:text="Select image" />

<ImageView
android:id="@+id/iv_photo"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:layout_width="fill_parent"  
android:layout_height="200dp"/>


</LinearLayout>

After you create the two layouts, you need to create 3 classes namely MainActivity.java, CropOption.java,  and CropOptionAdapter.

MainActivity.java

package net.londatiga.android;

import java.io.File;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;

import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ResolveInfo;

import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.view.View;

import android.net.Uri;

import android.os.Bundle;
import android.os.Environment;

import android.widget.Button;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.widget.ImageView;

public class MainActivity extends Activity {
private Uri mImageCaptureUri;
private ImageView mImageView;
private static final int PICK_FROM_CAMERA = 1;
private static final int CROP_FROM_CAMERA = 2;
private static final int PICK_FROM_FILE = 3;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main);
        
        final String [] items = new String [] {"Take from camera", "Select from gallery"};
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.select_dialog_item,items);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Image");
builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dialog, int item ) { //pick from camera
if (item == 0) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
  "tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));

intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);

try {
intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_FROM_CAMERA);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
} else { //pick from file
Intent intent = new Intent();
               intent.setType("image/*");
               intent.setAction(Intent.ACTION_GET_CONTENT);
               
               startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
}
}
} );
final AlertDialog dialog = builder.create();
Button button = (Button) findViewById(R.id.btn_crop);
mImageView = (ImageView) findViewById(R.id.iv_photo);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.show();
}
});
    }
    
    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   if (resultCode != RESULT_OK) return;
  
   switch (requestCode) {
   case PICK_FROM_CAMERA:
    doCrop();
   
    break;
   
   case PICK_FROM_FILE: 
    mImageCaptureUri = data.getData();
   
    doCrop();
   
    break;    
   
   case CROP_FROM_CAMERA:    
       Bundle extras = data.getExtras();
       if (extras != null) {        
           Bitmap photo = extras.getParcelable("data");
           
           mImageView.setImageBitmap(photo);
       }
       File f = new File(mImageCaptureUri.getPath());            
       
       if (f.exists()) f.delete();
       break;

   }
}
    
    private void doCrop() {
final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();
   
    Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setType("image/*");
        
        List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );
        
        int size = list.size();
        
        if (size == 0) {        
        Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();
       
            return;
        } else {
        intent.setData(mImageCaptureUri);
            
            intent.putExtra("outputX", 200);
            intent.putExtra("outputY", 200);
            intent.putExtra("aspectX", 1);
            intent.putExtra("aspectY", 1);
            intent.putExtra("scale", true);
            intent.putExtra("return-data", true);
            
        if (size == 1) {
        Intent i = new Intent(intent);
        ResolveInfo res = list.get(0);
       
        i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
       
        startActivityForResult(i, CROP_FROM_CAMERA);
        } else {
       for (ResolveInfo res : list) {
        final CropOption co = new CropOption();
       
        co.title = getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo);
        co.icon = getPackageManager().getApplicationIcon(res.activityInfo.applicationInfo);
        co.appIntent= new Intent(intent);
       
        co.appIntent.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
       
           cropOptions.add(co);
       }
       
       CropOptionAdapter adapter = new CropOptionAdapter(getApplicationContext(), cropOptions);
       
       AlertDialog.Builder builder = new AlertDialog.Builder(this);
       builder.setTitle("Choose Crop App");
       builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
           public void onClick( DialogInterface dialog, int item ) {
               startActivityForResult( cropOptions.get(item).appIntent, CROP_FROM_CAMERA);
           }
       });
       
       builder.setOnCancelListener( new DialogInterface.OnCancelListener() {
           @Override
           public void onCancel( DialogInterface dialog ) {
              
               if (mImageCaptureUri != null ) {
                   getContentResolver().delete(mImageCaptureUri, null, null );
                   mImageCaptureUri = null;
               }
           }
       } );
       
       AlertDialog alert = builder.create();
       
       alert.show();
        }
        }
}
}

CropOption.java

package net.londatiga.android;

import android.content.Intent;
import android.graphics.drawable.Drawable;

public class CropOption {
public CharSequence title;
public Drawable icon;
public Intent appIntent;
}

CropOptionAdapter

package net.londatiga.android;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import android.content.Context;

import java.util.ArrayList;

/**
 * Adapter for crop option list.
 * 
 * @author Lorensius W. L. T <lorenz@londatiga.net>
 *
 */
public class CropOptionAdapter extends ArrayAdapter<CropOption> {
private ArrayList<CropOption> mOptions;
private LayoutInflater mInflater;
public CropOptionAdapter(Context context, ArrayList<CropOption> options) {
super(context, R.layout.crop_selector, options);
mOptions = options;
mInflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup group) {
if (convertView == null)
convertView = mInflater.inflate(R.layout.crop_selector, null);
CropOption item = mOptions.get(position);
if (item != null) {
((ImageView) convertView.findViewById(R.id.iv_icon)).setImageDrawable(item.icon);
((TextView) convertView.findViewById(R.id.tv_name)).setText(item.title);
return convertView;
}
return null;
}
}
Web Hosting
That's it. Anyway there's a lot of tutorial around like this crop image and croping. Sorry if I didn't show the output because I can't upload the output image... Hope this simple tutorial may help you...

Change dot to Asterisk in Password

Web Hosting
In this tutorial, I will show you how to change the default value for password which is dot(.) to asterisk(*). See the code below.

//create a child class of your main class
class AsteriskPasswordTransformationMethod extends PasswordTransformationMethod {
    @Override
    public CharSequence getTransformation(CharSequence source, View view) {
        return new PasswordCharSequence(source);
    }

    private class PasswordCharSequence implements CharSequence {
        private CharSequence mSource;
        public PasswordCharSequence(CharSequence source) {
            mSource = source; // Store char sequence
        }
        public char charAt(int index) {
            return '*'; // This is the important part
        }
        public int length() {
            return mSource.length(); // Return default
        }
        public CharSequence subSequence(int start, int end) {
            return mSource.subSequence(start, end); // Return default
        }
    }
};


//then call this class wherever you need to change the dot to asterisk.
final EditText et_pin1 = (EditText)dl.findViewById(R.id.et_verify1);et_pin1.setTransformationMethod(new AsteriskPasswordTransformationMethod());


That's it, hope this simple tutorial will help you something. Happy Coding

Customized dialog within the Activity

Web Hosting
In this post, I will show you how to create a customized dialog in Android. I can't show you the output because I can't upload image at this time. I will just show you some codes.

//declare a dl variable for dialog at the top
private Dialog dl;


//user this code whereever you need it.
dl = new Dialog(Send_Money.this);
dl.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dl.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dl.setContentView(getLayoutInflater().inflate(R.layout.send_money_option_dialog, null));
dl.getWindow().getAttributes().windowAnimations = R.style.dialog_animation;

TextView tv_msg = (TextView)dl.findViewById(R.id.tv_message);
TextView tv_title = (TextView)dl.findViewById(R.id.tv_dialog_title);


tv_title.setText("TITLE");
tv_msg.setText("Message");

Button btn_one = (Button) dl.findViewById(R.id.btn_dialog_btn_one);
Button btn_two = (Button) dl.findViewById(R.id.btn_dialog_btn_two );
Button btn_three = (Button) dl.findViewById(R.id.btn_dialog_btn_three);

btn_one .setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

//do some code for button one

}
});


btn_two .setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

//do some code for button two

}
});

btn_three .setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

//do some code for button three

}
});

dl.show();
}


Web Hosting
That's it, hope this simple tutorial will help youo. Happy Coding.    

Redirect to SMS

Web Hosting
In this tutorial, I will show you some tutorial that redirecting or passing message to built in Messages Apps in your Android phone. From your Android Apps, you pass a message to it messages field. See the code below.

Intent smsIntent = new Intent(Intent.ACTION_VIEW);
smsIntent.setData(Uri.parse("sms:"));
smsIntent.putExtra("sms_body", "Your message"));
startActivity(smsIntent);


To only send a number, use this code.

startActivity(new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", "number", null)));

Web Hosting
That's it, hope this simple tutorial will help you. Happy Coding...