How to programmatically round corners and set random background colors

Instead of setBackgroundColor, retrieve the background drawable and set its color: v.setBackgroundResource(R.drawable.tags_rounded_corners); GradientDrawable drawable = (GradientDrawable) v.getBackground(); if (i % 2 == 0) { drawable.setColor(Color.RED); } else { drawable.setColor(Color.BLUE); } Also, you can define the padding within your tags_rounded_corners.xml: <?xml version=”1.0″ encoding=”utf-8″?> <shape xmlns:android=”http://schemas.android.com/apk/res/android”> <corners android:radius=”4dp” /> <padding android:top=”2dp” android:left=”2dp” android:bottom=”2dp” android:right=”2dp” /> </shape>

Why does calling getWidth() on a View in onResume() return 0?

A view still hasn’t been drawn when onResume() is called, so its width and height are 0. You can “catch” when its size changes using OnGlobalLayoutListener(): yourView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { // Removing layout listener to avoid multiple calls if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) { yourView.getViewTreeObserver().removeGlobalOnLayoutListener(this); } else { yourView.getViewTreeObserver().removeOnGlobalLayoutListener(this); } populateData(); } }); … Read more

No resource identifier found for attribute ‘…’ in package ‘com.app….’

I just changed: xmlns:app=”http://schemas.android.com/apk/res-auto” to: xmlns:app=”http://schemas.android.com/apk/lib/com.app.chasebank” and it stopped generating the errors, com.app.chasebank is the name of the package. It should work according to this Stack Overflow : No resource identifier found for attribute ‘adSize’ in package ‘com.google.example’ main.xml

Android: why setVisibility(View.GONE); or setVisibility(View.INVISIBLE); do not work

I see quite a few things wrong. For starters, you don’t have your magic button defined and there is no event handler for it. Also you shouldn’t use: dp2.setVisibility(View.GONE); dp2.setVisibility(View.INVISIBLE); Use only one of the two. From Android documentation: View.GONE This view is invisible, and it doesn’t take any space for layout purposes. View.INVISIBLE This … Read more

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().