What is the difference between the states selected, checked and activated in Android?

The difference between Checked and Activated is actually quite interesting. Even the Google documentation is apologetic (emphasis below added): … For example, in a list view with single or multiple selection enabled, the views in the current selection set are activated. (Um, yeah, we are deeply sorry about the terminology here.) The activated state is … Read more

Double tap: zoom on Android MapView?

I’ve also been searching for an answer/example, but found nowhere working code. Finally, here’s the code that’s working for me: MyMapActivity.java public class MyMapActivity extends MapActivity implements OnGestureListener, OnDoubleTapListener { private MapView mapView; @Override public void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); setContentView(R.layout.main); mapView = (MapView)findViewById(R.id.mapView); } @Override public boolean onDoubleTap(MotionEvent e) { int x = … Read more

Nexus 5x reverse landscape sensor fix in a android camera preview app

The 5X camera is not “reverse”; it does report the correct camera orientation in its parameters, so no special workarounds are needed, just make sure you’re setting the display orientation correctly: public static void setCameraDisplayOrientation(Activity activity, int cameraId, android.hardware.Camera camera) { android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo(); android.hardware.Camera.getCameraInfo(cameraId, info); int rotation = activity.getWindowManager().getDefaultDisplay() .getRotation(); int degrees … Read more

How to Measure TextView Height Based on Device Width and Font Size?

public static int getHeight(Context context, String text, int textSize, int deviceWidth) { TextView textView = new TextView(context); textView.setText(text); textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); int widthMeasureSpec = MeasureSpec.makeMeasureSpec(deviceWidth, MeasureSpec.AT_MOST); int heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); textView.measure(widthMeasureSpec, heightMeasureSpec); return textView.getMeasuredHeight(); } If textSize is not given in pixels, change the first paramter of setTextSize().

What is the benefit of using Fragments in Android, rather than Views?

The main reason to use Fragments are for the backstack and lifecycle features. Otherwise, custom views are more light weight and simpler to implement. At first, I actually tried to build a phone/tablet app using custom views. Everything appeared to work across phones AND tablets, even switching from single panel to split panel. Where I … Read more

Disable the touch events for all the views

Here is a function for disabling all child views of some view group: /** * Enables/Disables all child views in a view group. * * @param viewGroup the view group * @param enabled <code>true</code> to enable, <code>false</code> to disable * the views. */ public static void enableDisableViewGroup(ViewGroup viewGroup, boolean enabled) { int childCount = viewGroup.getChildCount(); … Read more

Android custom view group delegate addView

Views inflated from a layout – like your example TextView – are not added to their parent ViewGroup with addView(View child), which is why overriding just that method didn’t work for you. You want to override addView(View child, int index, ViewGroup.LayoutParams params), which all of the other addView() overloads end up calling. In that method, … Read more

Inflate a view / layout into another layout?

There is ViewStub but I never used it and I think it can’t be used more than once. You can inflate the menu layout and attach it to the main layout: AbsoluteLayout mainLayout = (AbsoluteLayout) findViewById(R.id.your_main_layout); LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); View menuLayout = inflater.inflate(R.layout.your_menu_layout, mainLayout, true); then when you want to change you can remove … Read more