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

  • Home
  • SEARCH
  • 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 8206581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:43:30+00:00 2026-06-07T08:43:30+00:00

I have a custom drawing drawn on a custom view .I would like to

  • 0

I have a custom drawing drawn on a custom view .I would like to bring this view to the bottom half of the screen without changing the co-ordinates of the ondraw function. I know this can be done but I am not able to do it. Please assist me.

This is my XML code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="0px"
        android:layout_weight="1" >

        <your.com.learn.game
            android:id="@+id/game1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="bottom" />
    </ScrollView>

</RelativeLayout>

game view code:

public class game extends View{
    private Bitmap image;
    private Paint paint;
    private int x=0;private int baseY = 250;

    public game(Context context,AttributeSet attr) {
        super(context,attr);

        //TODO Auto-generated constructor stub
        //LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //layoutInflater.inflate(R.layout.example,this);

        //this.inflate(context, R.layout.example, null);
        image=Bitmap.createBitmap(BitmapFactory.decodeResource(this.getResources(),R.drawable.ic_launcher));
        paint =new Paint();
        paint.setColor(Color.BLUE);

    }

    public void onDraw(Canvas canvas)
        {
        super.onDraw(canvas);
        canvas.drawLine(90, baseY,200,baseY, paint);
        canvas.drawLine(125,baseY,125,baseY-100,paint);
        canvas.drawLine(110,baseY,125,baseY-15,paint);
        canvas.drawLine(140,baseY,125,baseY-15,paint);
        canvas.drawLine(125,baseY-100,175,baseY-100,paint);
        canvas.drawLine(125,baseY-85,140,baseY-100,paint);
        canvas.drawLine(175,baseY-100,175,baseY-75,paint);
        canvas.drawOval(new RectF(170,(baseY-75),10,12),paint);
        canvas.drawOval(new RectF(170,baseY-65,15,25), paint);
        canvas.drawLine(160,baseY-65,170,baseY-60,paint);
        canvas.drawLine(183,baseY-60,193,baseY-65,paint);
        canvas.drawLine(165,baseY-30,170,baseY-45,paint);
        canvas.drawLine(183,baseY-45,193,baseY-30,paint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        final int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        boolean resizeWidth = widthSpecMode != MeasureSpec.UNSPECIFIED;
        boolean resizeHeight = heightSpecMode != MeasureSpec.UNSPECIFIED;
    }

    public void update()
    {
         if(x < 200)
                x++;
            else
                x=0;
    }
}

Logcat when game view is added to nested Scrollview(as described by Sam below) :

07-05 18:53:09.638: W/dalvikvm(858): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
07-05 18:53:09.649: E/AndroidRuntime(858): FATAL EXCEPTION: main 
07-05 18:53:09.649: E/AndroidRuntime(858): java.lang.RuntimeException: Unable to start activity ComponentInfo{your.com.learn/your.com.learn.LearningActivity}: java.lang.NullPointerException 
07-05 18:53:09.649: E/AndroidRuntime(858): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
07-05 18:53:09.649: E/AndroidRuntime(858): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
07-05 18:53:09.649: E/AndroidRuntime(858): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
07-05 18:53:09.649: E/AndroidRuntime(858): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
07-05 18:53:09.649: E/AndroidRuntime(858): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-05 18:53:09.649: E/AndroidRuntime(858): at android.os.Looper.loop(Looper.java:123) 
07-05 18:53:09.649: E/AndroidRuntime(858): at android.app.ActivityThread.main(ActivityThread.java:3683) 
07-05 18:53:09.649: E/AndroidRuntime(858): at java.lang.reflect.Method.invokeNative(Native Method) 
07-05 18:53:09.649: E/AndroidRuntime(858): at java.lang.reflect.Method.invoke(Method.java:507) 
07-05 18:53:09.649: E/AndroidRuntime(858): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
07-05 18:53:09.649: E/AndroidRuntime(858): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
07-05 18:53:09.649: E/AndroidRuntime(858): at dalvik.system.NativeStart.main(Native Method) 
07-05 18:53:09.649: E/AndroidRuntime(858): Caused by: java.lang.NullPointerException 
07-05 18:53:09.649: E/AndroidRuntime(858): at your.com.learn.LearningActivity.onCreate(LearningActivity.java:42) 
07-05 18:53:09.649: E/AndroidRuntime(858): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
07-05 18:53:09.649: E/AndroidRuntime(858): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
07-05 18:53:09.649: E/AndroidRuntime(858): ... 11 more


public class LearningActivity extends Activity  {
/** Called when the activity is first created. */

private game g;



int i=0;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    //For my button class
    /*
    MyButton myb = (MyButton)findViewById(R.id.mybutton1);
    myb.setText("Hello Students");
    myb.setTextSize(40);
   **/

    //For custom view
    setContentView(R.layout.main);
    //RelativeLayout r=(RelativeLayout) findViewById(R.id.relative1);
    g=new game(this,null);
    ViewGroup vgroup=(ViewGroup) findViewById(R.id.main);
     vgroup.addView(g);
    Thread mythread=new Thread(new UpdateThread());
    mythread.start();


}


public Handler updatehandler=new Handler(){
    /** Gets called on every message that is received */
    // @Override
    public void handleMessage(Message msg) {
       // g.update();

        g.invalidate();
         super.handleMessage(msg);

    }
};






public class UpdateThread implements Runnable
{

    public void run() {
        // TODO Auto-generated method stub
        while(true){
            //calling the handler
            LearningActivity.this.updatehandler.sendEmptyMessage(0);
        }

    }

}

}

  • 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-07T08:43:33+00:00Added an answer on June 7, 2026 at 8:43 am

    Rewrite

    I haven’t done much drawing on the canvas myself, but here is a way to make your current class work:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/text"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    
        <org.whatever.example.game
            android:id="@+id/game1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
    
    </LinearLayout>
    

    But when I tried nesting your Game View in another View (say a ScrollView), it wouldn’t draw anything. I don’t know why at this time, however the above code will get you moving the your next step. Hope that helps!

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

Sidebar

Related Questions

I have a layer-hosting view set up like this in a custom NSView subclass:
I have a custom UIView that contains an interactive drawing that is drawn in
I have a JPanel that implements custom drawing to draw a background. Over this,
I have a custom view that fills my entire screen. (A piano keyboard) When
I have a map custom view that inherit from MKOverlayPathView. I need this custom
I have a view that I want to add some custom drawing to. I
I have seen that in Cocoa I can create a custom view using drawing
I have a drawing activity that creates a custom 'canvas' View that occupies the
I have a custom-drawn view that I include in a UIScrollView that scrolls horizontally.
I have a pretty annoying problem. I would like to create a drawing program,

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.