onMeasure custom view explanation

onMeasure() is your opportunity to tell Android how big you want your custom view to be dependent the layout constraints provided by the parent; it is also your custom view’s opportunity to learn what those layout constraints are (in case you want to behave differently in a match_parent situation than a wrap_content situation). These constraints … Read more

Animate change of view background color on Android

You can use new Property Animation Api for color animation: int colorFrom = getResources().getColor(R.color.red); int colorTo = getResources().getColor(R.color.blue); ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), colorFrom, colorTo); colorAnimation.setDuration(250); // milliseconds colorAnimation.addUpdateListener(new AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animator) { textView.setBackgroundColor((int) animator.getAnimatedValue()); } }); colorAnimation.start(); For backward compatibility with Android 2.x use Nine Old Androids library from Jake … Read more

Get root view from current activity

If you need root view of your activity (so you can add your contents there) use findViewById(android.R.id.content).getRootView() Also it was reported that on some devices you have to use getWindow().getDecorView().findViewById(android.R.id.content) instead. Please note that as Booger reported, this may be behind navigation bar (with back button etc.) on some devices (but it seems on most … Read more

Android: How do I prevent the soft keyboard from pushing my view up?

You can simply switch your Activity’s windowSoftInputModeflag to adjustPan in your AndroidMainfest.xml file inside your activity tag. Check the official documentation for more info. <activity … android:windowSoftInputMode=”adjustPan”> </activity> If your container is not changing size, then you likely have the height set to “match parent”. If possible, set the parent to “Wrap Content”, or a … Read more

Android – Dynamically Add Views into View

Use the LayoutInflater to create a view based on your layout template, and then inject it into the view where you need it. LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = vi.inflate(R.layout.your_layout, null); // fill in any details dynamically here TextView textView = (TextView) v.findViewById(R.id.a_text_view); textView.setText(“your text”); // insert into main view ViewGroup insertPoint = … Read more

What is the main purpose of setTag() getTag() methods of View?

Let’s say you generate a bunch of views that are similar. You could set an OnClickListener for each view individually: button1.setOnClickListener(new OnClickListener … ); button2.setOnClickListener(new OnClickListener … ); … Then you have to create a unique onClick method for each view even if they do the similar things, like: public void onClick(View v) { doAction(1); … Read more

Android ImageView Scaling and translating issue

this is a working example of two fingers move/scale/rotate (note: the code is quite short due to smart detector used – see MatrixGestureDetector): class ViewPort extends View { List<Layer> layers = new LinkedList<Layer>(); int[] ids = {R.drawable.layer0, R.drawable.layer1, R.drawable.layer2}; public ViewPort(Context context) { super(context); Resources res = getResources(); for (int i = 0; i < … Read more

How to make a swipe strobe..?

You may follow the Wheel View library in github: Use it like this: <it.sephiroth.android.wheel.view.Wheel android:id=”@+id/wheel” xmlns:sephiroth=”http://schemas.android.com/apk/res-auto” android:layout_width=”match_parent” android:layout_height=”match_parent” sephiroth:numRotations=”6″ sephiroth:ticks=”28″ /> and it looks like this: