Android – (visually) clone a View

While there’s nothing in the Android framework can create a duplicate of an existing View, it’s trivial do duplicate one visually. Just call `someView.draw(canvas)` in the `onDraw` of the View you want to serve as the duplicate. E.g., package org.upshots.views; import android.content.Context; import; import; import android.util.AttributeSet; import android.view.View; /** * Created by michaeldunn […]

Android – confirm preference change

If you’re using the Preferences API in Android, and want to confirm (show a dialog, perform a test, etc) before a Preference is updated, you need to find that Preference item and call setOnPreferenceChangeListener to a custom listener. This can work in conjunction with PreferenceManager.registerOnSharedPreferenceChangeListener, you just need to return `false` to have the Preference […]

Android – isTaskRoot returning false incorrectly

Some Launchers will launch an app at it’s LAUNCH activity on top of the last point the user was at. Imagine: 1. User opens app. 2. The Launcher Activity is designated at Activity A, so that’s what opens. 3. User navigates several Activities deep and ends up at Activity D (A > B > C […]

Android – Restart App

The title isn’t exactly accurate – you can’t “restart” an app per se – but you can get pretty close: // presumably in an Activity; otherwise a Context instance will be needed for several methods Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName()); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intent); This starts the Activity specified in your manifest as the “LAUNCHER”, in […]

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; import; import; 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(, 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 – […]