I’m from iOS world. Its quite simple to add a subview in iOS : [mySuperView addSubview:myCustomView]. I can do that anytime anywhere. Android seems more complicated.
I have a custom view. I simply want to programmatically load as many of my custom views whenever I want.
So I do MyCustomView view = new MyCustomView(this).
What do I do now?
I’ve been doing Android apps for a year now (business apps), but really want to start getting into the graphics side of Android, but am not sure where to start. It seems like there are always 10 ways to accomplish a single task in Android. I have read through 4 Android books and they all give the same basic beginner hand-holding stuff. (create layout in xml, then find the id in code, etc, etc).
-
How do I solve my current problem?
-
How can I better learn about layout parameters, canvases & graphics drawing, and more advanced Android topics in general.
I want to know the “Why’s” behind things. For instance, why is it that I see most people writing their own custom listviews and adapters and not using the provided Android ones, same thing for tab host. I feel like I’m missing some great obscure gem of knowledge here.
MY CUSTOM VIEW CLASS:
package com.spentaklabs.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
public class MAView extends View
{
Drawable image;
private float mPosX;
private float mPosY;
private float mLastTouchX;
private float mLastTouchY;
private static final int INVALID_POINTER_ID = -1;
private int mActivePointerId = INVALID_POINTER_ID;
public MAView(Context context)
{
super(context);
image = context.getResources().getDrawable(R.drawable.hatshairears0);
image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
}
public MAView(Context context, AttributeSet attrs, int defStyle)
{
super(context,attrs,defStyle);
}
@Override
public void onDraw(Canvas canvas)
{
canvas.save();
canvas.translate(mPosX, mPosY);
image.draw(canvas);
canvas.restore();
}
@Override
public boolean onTouchEvent(MotionEvent ev)
{
final int action = ev.getAction();
switch (action)
{
case MotionEvent.ACTION_DOWN:
{
final float x = ev.getX();
final float y = ev.getY();
mLastTouchX = x;
mLastTouchY = y;
mActivePointerId = ev.getPointerId(0);
break;
}
case MotionEvent.ACTION_MOVE:
{
final int pointerIndex = ev.findPointerIndex(mActivePointerId);
final float x = ev.getX(pointerIndex);
final float y = ev.getY(pointerIndex);
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
mPosX += dx;
mPosY += dy;
invalidate();
break;
}
case MotionEvent.ACTION_UP:
{
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_CANCEL:
{
mActivePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_POINTER_UP:
{
// Extract the index of the pointer that left the touch sensor
final int pointerIndex = (action & MotionEvent.ACTION_POINTER_ID_MASK) >> MotionEvent.ACTION_POINTER_ID_SHIFT;
final int pointerId = ev.getPointerId(pointerIndex);
if (pointerId == mActivePointerId)
{
// This was our active pointer going up. Choose a new
// active pointer and adjust accordingly.
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mLastTouchX = ev.getX(newPointerIndex);
mLastTouchY = ev.getY(newPointerIndex);
mActivePointerId = ev.getPointerId(newPointerIndex);
}
break;
}
}
return true;
}
}
Android provides convenience classes that work well for a lot of uses cases, e.g ListActivity, etc. When people need to do things that won’t work with the out-of-the-box classes, they write their own adapters, etc. I say “write their own”, but in reality it’s much simpler, at least in the case of adapters. In the case of adapters, you simply extend BaseAdapter and then override some methods and provide your own custom implementation. This might be because you have some logic you’d like to incorporate into getView before returning the actual view for the list item, etc.
As far as loading custom views programmatically, I’m afraid you’re a bit unclear in your question. But if you’d like to simply display custom views, then all you need to is get an instance, like you did, and then call setContentView(view) inside of your Activity. If you have a more specific question or explanation of what you’re trying to do, I’d be happy to help.
If you’re just trying to add your custom view as a subview of some other view, then you need to call addView(myCustomView) on your parent view. Generally, you’ll need to also provide LayoutParams to get everything looking correctly.