Skip to content

Instantly share code, notes, and snippets.

@granoeste
granoeste / EachDirectoryPath.md
Last active April 4, 2024 22:32
[Android] How to get the each directory path.

System directories

Method Result
Environment.getDataDirectory() /data
Environment.getDownloadCacheDirectory() /cache
Environment.getRootDirectory() /system

External storage directories

@granoeste
granoeste / RecursiveSearchAndDeleteDuplicate.java
Last active December 17, 2015 01:49
[java] Recursive search and delete duplicate for a list
public static <T> void recursiveSearchAndDeleteDuplicate(List<T> source, List<T> target) {
T str = source.get(0);
Iterator<T> it = source.iterator();
while (it.hasNext()) {
T e = it.next();
if (str.equals(e)) {
it.remove();
}
}
@granoeste
granoeste / FacebookLoginActivity.java
Last active December 17, 2015 00:10
[Android] Login Facebook with Parse
public class FacebookLoginActivity extends Activity {
private Button mLoginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_facebook_login);
mLoginButton = findViewById(R.id.login_button);
mLoginButton.setOnClickListener(new View.OnClickListener() {
@granoeste
granoeste / ActivityWithinWeakHandler.java
Created April 10, 2013 09:25
Define a handler for weak references to inner classes of activity
public class MyActivity extends Activity {
protected Handler mInternalHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mInternalHandler = new InternalHandler(this);
}
@granoeste
granoeste / activity_main.xml
Created March 23, 2013 02:48
[Android] What's " xmlns:tools="http://schemas.android.com/tools" " ?
<!--
xmlns:tools="http://schemas.android.com/tools" for Graphical Layout View.
-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
@granoeste
granoeste / gist:4631370
Created January 25, 2013 03:00
Java Templates for Android logging
// makeLogTag
${:importStatic(net.granoeste.commons.util.LogUtils.makeLogTag)}private static final String TAG = makeLogTag(${enclosing_type}.class);
// TRACE
${:importStatic(net.granoeste.commons.util.LogUtils.LOGV)}LOGV(TAG, "${enclosing_method}()"
// LOGV
${:importStatic(net.granoeste.commons.util.LogUtils.LOGV)}LOGV(TAG, ${cursor});
// LOGD
@granoeste
granoeste / CheckLauncherCategory.java
Last active December 11, 2015 06:48
[android] check Category is CATEGORY_LAUNCHER in an Activity
boolean isLauncher = intent.hasCategory(Intent.CATEGORY_LAUNCHER);
if (!isLauncher) {
Intent intent = new Intent(context, TopActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK
@granoeste
granoeste / gist:4214069
Created December 5, 2012 09:05
[Android] getBroadcast
public String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface
.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
@granoeste
granoeste / TextView.java
Created November 8, 2012 08:00
[Android] To implement state saving and restoration in the custom view.
// To implement state saving and restoration in the custom view...
//
// You should implement the SavedState extend the BaseSavedState.
// You should be implemented to override the onSaveInstanceState and onRestoreInstanceState.
// TextView will be helpful.
// TextView
public class TextView extends View implements ViewTreeObserver.OnPreDrawListener {
/**
@granoeste
granoeste / attrs.xml
Created November 8, 2012 07:16
[Android] Attributes define in the Enum or Flag
<!-- Default text typeface. -->
<attr name="typeface">
<enum name="normal" value="0" />
<enum name="sans" value="1" />
<enum name="serif" value="2" />
<enum name="monospace" value="3" />
</attr>
<!-- Default text typeface style. -->
<attr name="textStyle">