Monday, 19 May 2014

Some Useful Common functions :-)


 Change data  on UI Thread [runOnUiThread(Runnable)]


(new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                while (!Thread.interrupted())
                    try
                    {
                        Thread.sleep(1000);
                        runOnUiThread(new Runnable() // start actions in UI thread
                        {
                            @Override
                            public void run()
                            {
                                displayData(); // this action have to be in UI thread
                            }
                        });
                    }
                    catch (InterruptedException e)
                    {
                    }
            }
        })).start();



Check whether Device is mobile or Tab :-


public static String finddevice(Context ctx){
String userAgent = new WebView(ctx).getSettings().getUserAgentString();
if(userAgent.contains("Mobile")){
return "mobile";
}else{
return "tab";

}
}


Set Device Orientation Pragmatically :- 


public static void landscapeview(Context ctx){
((Activity) ctx).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}


Set Device Keyboard on hidden state :-


public static void hiddenkeybord(Context ctx){
((Activity) ctx).getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
}



Udust Device Keyboard for easy type :-

// Inside activity tag on particular activity declaration  - Manifest file

 android:windowSoftInputMode="adjustPan"

Handling back button pragmatically  :-


 public public void onBackPressed() {

// TODO Auto-generated method stub
// super.onBackPressed();
AlertDialog dialog = new AlertDialog.Builder(Current_Activity.this)

.setTitle("Warning")
.setIcon(R.drawable.i_img)
.setMessage("Exit Application?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
// Current_Activity.this.finish();
}
})
.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
}
}).create();
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
WindowManager.LayoutParams WMLP = dialog.getWindow().getAttributes();
WMLP.gravity = Gravity.CENTER_VERTICAL | Gravity.LEFT;
dialog.getWindow().setAttributes(WMLP);
dialog.show();
}


Check Available Memory :-


public boolean checkMemory() {
Log.i("getExternalStorageState "
+ Environment.getExternalStorageState(), "Checking availability");
StatFs stats;
double bytes;
double megaBytes;
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
stats = new StatFs(Environment.getExternalStorageDirectory()
.getPath());
bytes = (double) stats.getAvailableBlocks()
* (double) stats.getBlockSize();
megaBytes = bytes / 1048576;
Log.i("getExternalStorageState MOUNTED", megaBytes + "MB available");
if (megaBytes < 40) {
AlertDialog ad = new AlertDialog.Builder(Current_Activity.this)
.setTitle("Attention!")
.setMessage(
"There is not enough memory in SD Card, please free some space.")
.setIcon(R.drawable.app_icon)
.setCancelable(true)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0,
int arg1) {
memoryFlag = true;
}
}).create();
ad.requestWindowFeature(Window.FEATURE_NO_TITLE);
WindowManager.LayoutParams WMLP = ad.getWindow()
.getAttributes();
WMLP.gravity = Gravity.CENTER_VERTICAL | Gravity.RIGHT;
ad.getWindow().setAttributes(WMLP);
ad.show();
} else {
memoryFlag = true;
}
} else {
stats = new StatFs(Environment.getDataDirectory().getPath());
bytes = (double) stats.getAvailableBlocks()
* (double) stats.getBlockSize();
megaBytes = bytes / 1048576;
Log.i("getDataDirectory", megaBytes + "MB available");
if (megaBytes < 40) {
AlertDialog ad = new AlertDialog.Builder(Current_Activity.this)
.setTitle("Attention!")
.setMessage(
"There is not enough space in Internal memory, please free some space.")
.setIcon(R.drawable.app_icon)
.setCancelable(true)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0,
int arg1) {
memoryFlag = false;
}
}).create();
ad.requestWindowFeature(Window.FEATURE_NO_TITLE);
WindowManager.LayoutParams WMLP = ad.getWindow()
.getAttributes();
WMLP.gravity = Gravity.CENTER_VERTICAL | Gravity.RIGHT;
ad.getWindow().setAttributes(WMLP);
ad.show();
} else {
memoryFlag = true;
}
}
return memoryFlag;
}






Wednesday, 14 May 2014

Animated layouts In Android :-)


Pre-Requirements 
=============
1 . Create one folder inside res folder - "anim"
2. Add animated-xml files in to anim folder.


Changes in Activity 
=============

1. class has to extend Activity as well as implements AnimationListener

      Eg :   public class Activity_Classname extends Activity implements AnimationListener

2.Declare Animation  variable . 


       Eg :   Animation   slide_up ;


3.Assign Action into that variable .


      Eg :    slide_up=AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.slide_down);
slide_up.setAnimationListener(this);

4.call  this animation variable where ever u want 

          Eg : layout_name.startAnimation(slide_up);




   



1. dash.xml
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="800"
    android:fromAlpha="0.0"
    android:toAlpha="1.0" />

2.fade_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >
    <alpha
        android:duration="4000"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" >
    </alpha>
</set>

3.fade_in2.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >
    <alpha
        android:duration="10000"
        android:fromAlpha="0.0"
        android:startOffset="2500"
        android:toAlpha="1.0" >
    </alpha>
</set>


4.login.x
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXDelta="0%"
    android:fromYDelta="150%"
    android:toXDelta="0%"
    android:toYDelta="0%"
    android:zAdjustment="top" >
</translate>

5.zoomexit.xmlml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator" >

    <alpha
        android:duration="@android:integer/config_longAnimTime"
        android:fromAlpha="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="0.0" />
</set>

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

    <alpha
        android:duration="@android:integer/config_longAnimTime"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0" />
</set>

7.transilateouttoright.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXDelta="0%"
    android:fromYDelta="0%"
    android:toXDelta="100%"
    android:toYDelta="0%" >
</translate>

8.trancelateout.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXDelta="0%"
    android:fromYDelta="0%"
    android:toXDelta="-100%"
    android:toYDelta="0%" >
</translate>

9.tansilateforword.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXDelta="100%"
    android:fromYDelta="0%"
    android:toXDelta="0%"
    android:toYDelta="0%" >
</translate>


10.translatebackword.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500"
    android:fromXDelta="-100%"
    android:fromYDelta="0%"
    android:toXDelta="0%"
    android:toYDelta="0%" >
</translate>


11.tranclate_y_axix.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fromXDelta="0%"
    android:fromYDelta="100%"
    android:toXDelta="0%"
    android:toYDelta="0%"
    android:zAdjustment="top" >
</translate>

12.slide_up.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" >
    <scale
        android:duration="500"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:interpolator="@android:anim/linear_interpolator"
        android:toXScale="1.0"
        android:toYScale="0.0" />
</set>

13.slide_down.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true">
    <scale
        android:duration="500"
        android:fromXScale="1.0"
        android:fromYScale="0.0"
        android:toXScale="1.0"
        android:toYScale="1.0" />
</set>

14.set.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
    <scale
        android:duration="1000"
        android:fillAfter="false"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.0"
        android:toYScale="1.0" />
    <set>
        <alpha
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:duration="2000"
            android:fromAlpha="0.2"
            android:toAlpha="1.0" />
        <rotate
            android:duration="2000"
            android:fromDegrees="0"
            android:pivotX="50%"
            android:pivotY="50%"
            android:startOffset="0"
            android:toDegrees="-360"
            android:toYScale="0.0" />
        <translate
            android:duration="2000"
            android:fromXDelta="100%"
            android:fromYDelta="60%"
            android:toXDelta="0%"
            android:toYDelta="0%"
            android:zAdjustment="bottom" />
    </set>
    <scale
        android:duration="1000"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.0"
        android:toYScale="1.0" />
</set>

15.sale_out.xml
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXScale="1"
    android:fromYScale="1"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="0"
    android:toYScale="0" >
</scale>

16.scale_in.xml
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXScale="0"
    android:fromYScale="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:startOffset="300"
    android:toXScale="1"
    android:toYScale="1" >
</scale>

17.rotate_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >
    <rotate
        android:duration="1000"
        android:fromDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="0" />
    <alpha
        android:duration="2000"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" >
    </alpha>
    <scale
        android:duration="2000"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="0%"
        android:pivotY="0%"
        android:toXScale="0"
        android:toYScale="0" />
</set>

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

    <rotate
        android:duration="1000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="360" />
    <alpha
        android:duration="2000"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" >
    </alpha>
    <scale
        android:duration="2000"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="100%"
        android:pivotY="100%"
        android:toXScale="0"
        android:toYScale="0" />
</set>

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

    <rotate
        android:duration="1000"
        android:fromDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="0" />
    <alpha
        android:duration="2000"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" >
    </alpha>
    <scale
        android:duration="2000"
        android:fromXScale=".1"
        android:fromYScale=".1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.0"
        android:toYScale="1.0" />
</set>


20.rotate_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false" >
    <rotate
        android:duration="1000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="360" />
    <alpha
        android:duration="2000"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" >
    </alpha>
    <scale
        android:duration="2000"
        android:fromXScale=".1"
        android:fromYScale=".1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.0"
        android:toYScale="1.0" />
</set>




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







Tuesday, 6 May 2014

Simple steps to create action bar icons with its events.

// Import all packages....Here


public class Activity_Leave_application extends Activity{



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



///Adding Action bar 
ActionBar actionbar =getActionBar();
ColorDrawable colorDrawable = new                                 ColorDrawable();
actionbar.setNavigationMode(ActionBar.NAVIGATION_                 MODE_LIST);
actionbar.setDisplayShowTitleEnabled(false);
colorDrawable.setColor(Color.parseColor("#1471a2"                 ));
actionbar.setBackgroundDrawable(colorDrawable);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
CreateMenu(menu);
return true;
}




@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
return MenuChoice(item);
}





private boolean MenuChoice(MenuItem item) {

switch (item.getItemId()) {
case 0:
Toast.makeText(getApplicationContext(),                           "first menu from actionbar", 100).show();
break;
case 1:
Toast.makeText(getApplicationContext(),                          "second menu from actionbar", 100).show();
break;
default:
Toast.makeText(getApplicationContext(),                         "Default menu from actionbar", 100).show();
break;
}
return false;
}



private boolean CreateMenu(Menu menu) {
MenuItem mnu3=menu.add(0, 0, 0, "About Us");{
mnu3.setIcon(R.drawable.ic_launcher);
mnu3.setShowAsAction(MenuItem.SHOW_AS_ACT                         ION_ALWAYS);

}
MenuItem mnu2=menu.add(0, 1, 1, "Update");{
mnu2.setIcon(R.drawable.ic_launcher);
mnu2.setShowAsAction(MenuItem.SHOW_AS_ACT                         ION_ALWAYS);
}
return false;
}



}




Set up android project with Open CV

Open CV is an open sourced computer vision library. It is a machine learning software library used for image processing and...