Android – get system defined ActionBar size (height)

In xml, you can use ?attr/actionBarSize, but if you need access to that value in Java… public int getActionBarSize() { TypedArray styledAttributes = getTheme().obtainStyledAttributes(new int[] { android.R.attr.actionBarSize }); int actionBarSize = (int) styledAttributes.getDimension(0, 0); styledAttributes.recycle(); return actionBarSize; }

Android – View traversal / walker / finder utilities

package com.qozix.view; import android.view.View; import android.view.ViewGroup; import android.view.ViewParent; import java.util.ArrayList; import java.util.Collection; /** * Collection of utility functions for finding Views in a hierarchy. */ public class Finder { /** * Interface with method to determine if a particular View meets arbitrary criteria for inclusion in a * ViewFinder method’s result set. */ public interface […]

Android – LinearLayout with dividers (separators)

Add (or amend) your values/attrs.xml to include: Some layout: And the class: package com.qozix.widgets; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.view.View; import android.widget.LinearLayout; import com.qozix.widgets.R; /** * Created by michaeldunn on 8/15/16. */ public class SeparatorLinearLayout extends LinearLayout { private Drawable mSeparator; private Rect mSeparatorBounds = new Rect(); […]

Android – launch modes and multiple intents

The behavior here isn’t quite what I expected, or gather from the documentation. If you have <activity android:name=”.SomeActivity” android:launchMode=”singleTop” /> Then you start SomeActivity multiple times: private void doIt(){ startSomeActivity(); startSomeActivity(); } private void startSomeActivity(){ Intent intent = new Intent(this, SomeActivity.class); startActivity(intent); } You’ll get only one instance of SomeActivity at the top of the […]

Android – get default text size

Possibly useful in precomputing layout requirements… public static int getDefaultTextSize(Context context) { TypedValue typedValue = new TypedValue(); context.getTheme().resolveAttribute(android.R.attr.textAppearance, typedValue, true); int[] textSizeAttr = new int[] { android.R.attr.textSize }; TypedArray typedArray = context.obtainStyledAttributes(typedValue.data, textSizeAttr); int textSize = typedArray.getDimensionPixelSize(0, -1); typedArray.recycle(); return textSize; }

Java – Unexpected Bounds when extending a typed Class

When extending a typed class without specifying the type, just pass the type to the subclass’s erasure and specify that type in the reference to the superclass. E.g., public class MyClass {} public class MySubClass extends MyClass {} // E is already declared by the time we get to MyClass, so just reference it – […]

Java – Super Class constructor fired before SubClass members are initialized

Probably unexpected behavior: public class Main { public static void main(String[] args) { mew SomeSub(); } public abstract static class SomeSuper { public SomeSuper(){ doInit(); } public abstract void doInit(); } public static class SomeSub extends SomeSuper { public int someIntMember = 3; public void doInit(){ System.out.println(someIntMember); // prints 0, not 3 } } } […]

Android – SteakKnife – simple ViewBinding with annotations

Butterknife, like most things by Square and/or Jake Wharton, is a very handy, very popular library. One thing that was really nice was how small and simple it was. The latest version, however, is a lot less simple, and requires multiple gradle dependencies and the apt plugin. Not a huge lift, but not exactly as […]

Android – get removable SD card location

This is a little tricky, and isn’t precisely a version limitation. Here’s why, and how I’ve found the most reliable way to get it. 1. Environment.isExternalStorageRemovable (without parameters) is API 9+, so has broad support. If your devices primary external storage location is removable, then this will return the location you want – done. 2. […]

Android – RecyclerView with smooth infinite / endless scroll

/EDIT: I’ve created a library (and demo): https://github.com/moagrius/EndlessRecyclerView Most “endless” RecyclerViews wait until the last item is visible, then load more data, which is often fine and probably very more efficient strategy. However, sometimes we need to be a little more responsive, and try not to let the scroll come to a complete stop. Here’s […]