Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6868775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:28:33+00:00 2026-05-27T03:28:33+00:00

I’m from iOS world. Its quite simple to add a subview in iOS :

  • 0

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

  1. How do I solve my current problem?

  2. 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;
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:28:34+00:00Added an answer on May 27, 2026 at 3:28 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.