25 April, 2014

Image Crop

Crop Options: From Camera or Gallery

builder.setTitle("Select Image");     // This is Button and its adapterClickListener
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);
}
}

} );



Crop Method

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();
        }
        }
}


java Files
CropOption.java
CropOptionAdapter.java
MainActivity.java

xml Files

crop_selector.xml
main.xml
















Download

24 July, 2013

Animation and External Fonts

External Fonts
You can add your favorite fonts using this code

Syntax:
Typeface typeface_name = Typeface.createFromAsset(getAssets(), "path");
obj_name.setTypeface(typeface_name);
obj_name.setTextSize(text_size);
obj_name.setText("Your Text");

Example:
Typeface font0 = Typeface.createFromAsset(getAssets(), "fonts/GOTHIC.TTF");
TextView txtMessage = (TextView) findViewById(R.id.txtMessage);
txtMessage.setTypeface(font0);
txtMessage.setTextSize(50.f);
txtMessage.setText("hello android");

From where you fetch the Font file?
Understand this screen-shot

Animation List
- Bink
- Bounce
- Cross Fade
- Fade In
- Fade Out
- Move
- Rotate
- Sequential Animation
- Slide Down
- Slide Up
- Zoom In
- Zoom Out
- Together Animation


Download

Different InputTypes

activity_main.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".MainActivity" >

        <EditText
            android:id="@+id/et1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_margin="10dp"
            android:hint="text|textCapCharacters"
            android:inputType="text|textCapCharacters" />

        <EditText
            android:id="@+id/et2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/et1"
            android:layout_margin="10dp"
            android:hint="text|textEmailAddress"
            android:inputType="text|textEmailAddress" />

        <EditText
            android:id="@+id/et3"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/et2"
            android:layout_margin="10dp"
            android:hint="text|textPassword"
            android:inputType="text|textPassword" />

        <EditText
            android:id="@+id/et4"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/et3"
            android:layout_margin="10dp"
            android:hint="number"
            android:inputType="number" />

        <EditText
            android:id="@+id/et5"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/et4"
            android:layout_margin="10dp"
            android:hint="phone"
            android:inputType="phone" />

        <EditText
            android:id="@+id/et6"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/et5"
            android:layout_margin="10dp"
            android:hint="datetime"
            android:inputType="datetime" />

        <EditText
            android:id="@+id/et7"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/et6"
            android:layout_margin="10dp"
            android:hint="text|textCapSentences|textMultiline"
            android:inputType="text|textCapSentences|textMultiLine" />

        <EditText
            android:id="@+id/et8"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/et7"
            android:layout_margin="10dp"
            android:hint="text|textUri"
            android:inputType="text|textUri" />
    </RelativeLayout>

</ScrollView>

Screen-Shots



Download

19 July, 2013

Validation PopUp

activity_main.xml
<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_margin="20dp"
        android:layout_marginTop="50dp"
        android:background="@color/grey" >
        
        <TextView
            android:id="@+id/tvsign"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_margin="10dp"
            android:text="@string/sign"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/black" />
        
        <TextView
            android:id="@+id/tvusername"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/tvsign"
            android:layout_margin="10dp"
            android:text="@string/username"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/red"
            android:textStyle="bold" />
        
        <EditText
            android:id="@+id/etusername"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tvusername"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:inputType="none" />
        
         <TextView
            android:id="@+id/tvpassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/etusername"
            android:layout_margin="10dp"
            android:text="@string/password"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/red"
            android:textStyle="bold" />
         
         <EditText
            android:id="@+id/etpass"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tvpassword"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:inputType="textPassword" />
         
          <Button
            android:id="@+id/btnsignin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/etpass"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="30dp"
            android:background="@drawable/btn"
            android:text="@string/sign"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/white" >
        </Button>
        
          <Button
            android:id="@+id/btnclear"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/etpass"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="30dp"
            android:layout_toRightOf="@+id/btnsignin"
            android:background="@drawable/btn"
            android:text="@string/cleardata"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/white" >
        </Button>
    </RelativeLayout>


</RelativeLayout>

MainActivity.java
package ums.androideasycode.validation;

import ums.androideasycode.validation.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity 
{
Button btnSignedIn, btnClearData;
EditText etUsername, etPassword;

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

etUsername = (EditText) findViewById(R.id.etusername);
etPassword = (EditText) findViewById(R.id.etpass);

btnSignedIn = (Button) findViewById(R.id.btnsignin);
btnSignedIn.setOnClickListener(new OnClickListener() 
{

@Override
public void onClick(View v) 
{
// TODO Auto-generated method stub
String strName = etUsername.getText().toString();
String strPass = etPassword.getText().toString();

if (null == strName || strName.trim().length() == 0)
{
etUsername.setError("Enter Your Name");
etUsername.requestFocus();

else if (null == strPass || strPass.trim().length() == 0)
{
etPassword.setError("Enter Your Password");
etPassword.requestFocus();

else
{
Toast.makeText(getApplicationContext(), "Username: " + strName + "\nPassword: " + strPass, Toast.LENGTH_LONG).show();
}
}
});

btnClearData = (Button) findViewById(R.id.btnclear);
btnClearData.setOnClickListener(new OnClickListener() 
{

@Override
public void onClick(View v) 
{
etUsername.setText("");
etPassword.setText("");
etUsername.requestFocus();

}
});
}

}

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

    <item android:drawable="@drawable/btn_pressed" android:state_focused="true" android:state_pressed="true"></item>
    <item android:drawable="@drawable/btn_default" android:state_focused="true" android:state_pressed="false"></item>
    <item android:drawable="@drawable/btn_pressed" android:state_focused="false" android:state_pressed="true"></item>
    <item android:drawable="@drawable/btn_default"></item>


</selector>

res/values/color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="black">#000000</color>
    <color name="grey">#DCDCDC</color>
    <color name="white">#FFFFFF</color>
    <color name="red">#FF0000</color>


</resources>

res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="action_settings">Settings</string>
    <string name="sign">Sign in</string>
    <string name="app_name">Validation PopUp</string>
    <string name="username">Username</string>
    <string name="password">Password</string>
    <string name="keepme">Keep me logged in</string>
    <string name="cleardata">Clear</string>


</resources>

Screen-Shots
Sign In
Successfully Sign In

popup on username edittext
Username Popup

popup on password edittext
Password Popup