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






No comments:

Post a Comment

How to do text writing animation?  In Android Development we do not have the option to use any other custom font in our default Tex...