Skip to content

Instantly share code, notes, and snippets.

@nahba
nahba / Util
Last active June 9, 2016 04:46
General Util Methods
-------Write To File In Append Mode----------
String logDirPath = getApplicationContext().getFilesDir().getAbsolutePath() + File.separator + "Logs"
+ File.separator + "Log.txt";
private void writeToFile(String logFilePath, String whatToWrite) {
try {
if(logFilePath != null && logFilePath.trim().length() > 0) {
SimpleDateFormat format = new SimpleDateFormat("EEE dd/MM/yyyy HH:mm:ss", Locale.US);
String passing = format.format(new Date()) + " " + whatToWrite + "\n";
FileWriter writer = new FileWriter(logFilePath, true);//true FOR APPEND MODE
writer.append(passing);
@nahba
nahba / LocationFetching
Last active June 9, 2016 04:48
Location Fetching Using GooglePlayServices LocationClient
Add GooglePlayService Library To Your Project
Manifest::
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application ....>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
</application>
@nahba
nahba / HowToGCM
Last active June 9, 2016 04:48
GCM Integration
1. Get Sender Id (And APIKeyFor ServerSide) From Google Console Project
-------------------Using GCMRegistrar----------------------------------
2. Add GCM.jar To Your Project
3. Add Required Permission Into Manifest
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<permission
android:name="(Your_Package).permission.C2D_MESSAGE"
@nahba
nahba / ConversionUtils
Last active June 9, 2016 04:49
Conversions
public class ConversionUtils {
public static int convertHexToDecimal(String hexCode) {
return Integer.parseInt(hexCode, 16);
}
public static String convertDecimalToHex(int decCode) {
return Integer.toHexString(decCode);
}
@nahba
nahba / CompareArrayListActivity
Last active June 9, 2016 04:49
Compare Two List - If Any Element Changed Or Not (Record Updated Or Not)
private List<Employee> firstArrayList;
private List<Employee> getFirstArrayList() {
List<Employee> arrayList = new ArrayList<Employee>();
arrayList.add(new Employee(1, "Donut"));
arrayList.add(new Employee(2, "Froyo"));
arrayList.add(new Employee(3, "Eclair"));
arrayList.add(new Employee(4, "Gingerbread"));
arrayList.add(new Employee(5, "HoneyComb"));
arrayList.add(new Employee(6, "IceCream Sandwich"));
arrayList.add(new Employee(7, "Jelly Bean"));
@nahba
nahba / RadioGrpCheckAdapter
Last active June 9, 2016 04:49
ListView With RadioGroup OnCheckChangedListener
viewHolder.radioGroup.setOnCheckedChangeListener(null); //Most Important Thing To Write O/W Won't Work
if(radioModel.getCheckedId() == 0) {
viewHolder.radioGroup.check(R.id.radioOne);
} else if(radioModel.getCheckedId() == 1) {
viewHolder.radioGroup.check(R.id.radioTwo);
} else if(radioModel.getCheckedId() == 2) {
viewHolder.radioGroup.check(R.id.radioThree);
}
viewHolder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
@nahba
nahba / CheckRunningComponent
Last active June 9, 2016 04:50
Check Running Component
@Override
protected void onStart() {
super.onStart();
boolean isRunning = isServiceRunning(BackgroundService.class);
if(isRunning) {
Intent serviceIntent = new Intent(this, BackgroundService.class);
stopService(serviceIntent);
}
}
@nahba
nahba / BackKeyActivity
Last active June 9, 2016 04:50
Back Key Handling
@Override
public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
if ((CompatibilityUtil.getSdkVersion() <
android.os.Build.VERSION_CODES.ECLAIR)
&& keyCode == KeyEvent.KEYCODE_BACK
&& keyEvent.getRepeatCount() == 0) {
onBackPressed();
}
return super.onKeyDown(keyCode, keyEvent);
@nahba
nahba / CustomDialog
Last active June 9, 2016 04:51
Dialog Using Custom Theme
<style name="PauseDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowAnimationStyle">@style/PauseDialogAnimation</item>
</style>
<style name="PauseDialogAnimation">
<item name="android:windowEnterAnimation">@anim/bounce_scale</item>
<item name="android:windowExitAnimation">@anim/bounce_minimize</item>
</style>
<style name="DialogActivity" parent="android:Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
@nahba
nahba / Key
Created July 27, 2014 17:17
Hash Map Custom Key
package moc.nahba.diord.utils;
public class Key {
private final int x;//Row
private final int y;//Column
public Key(int x, int y) {
this.x = x;
this.y = y;
}