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 8216271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:04:13+00:00 2026-06-07T12:04:13+00:00

Basically my question is: how to move a complete view around, instead of just

  • 0

Basically my question is: how to move a complete view around, instead of just moving the canvas? *I have a custom view and I want to move the whole thing not just the canvas.*

This is what I have tried, but I get a NullPointerException (the code is not compleate, I have put what I think is the most relevant part, also the parent of the custom view is a LinearLayout):

ResistorView class:

LinearLayout root;

public class ResistorView extends View{

    public ResistorView(Context context){
        super(context);         
        root = (LinearLayout)findViewById(R.id.main);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {        

    final int X = (int) event.getRawX();
    final int Y = (int) event.getRawY();
    switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            LinearLayout.LayoutParams lParams = (LinearLayout.LayoutParams) this.getLayoutParams();
            _xDelta = X - lParams.leftMargin;
            _yDelta = Y - lParams.topMargin;
            break;
        case MotionEvent.ACTION_UP:
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            break;
        case MotionEvent.ACTION_POINTER_UP:
            break;
        case MotionEvent.ACTION_MOVE:
            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) this.getLayoutParams();
            layoutParams.leftMargin = X - _xDelta;
            layoutParams.topMargin = Y - _yDelta;
            layoutParams.rightMargin = -250;
            layoutParams.bottomMargin = -250;
            this.setLayoutParams(layoutParams);
            break;
    }
    root.invalidate(); //There is a NullPointerException on this line.
    return true;
    }

main layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"        
    android:orientation="vertical"
    android:id="@+id/main">

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello" />

<Button
    android:id="@+id/bAdd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"        
    android:text="Add RES" />


</LinearLayout>

I don’t know what I am doing wrong, if someone can help I would really appreciated. Thanks.

  • 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-06-07T12:04:15+00:00Added an answer on June 7, 2026 at 12:04 pm

    Change

    root = (LinearLayout)findViewById(R.id.main);
    

    to

    root = (LinearLayout)getParent();
    

    You may even want to do this inside of onTouch instead and remove it from the constructor, something like:

    if(root == null) {
        root = (LinearLayout)getParent();
    }
    root.invalidate();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically my question is i have a website which was developed around 3 years
I have made CAlayer that contains an image. What i basically want to do
Basically the same as this question: How to make an infinitely long scroll view
I'm basically creating a trivia type of game. Where you have a question and
Basically the question is how do I run gpgpu code in a Metro app.
Basically my question is the exact same one as this: Simple client/server, TCP/IP encrypting
Basically the question could more probably be more accurately asked, how do I simply
Basically the question is, Why are NONE of the changes I've been making in
SHORT VERSION OF QUESTION: So basically my question is: How can I set the
This is basically the same question as Click items in select box, and then

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.