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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:34:02+00:00 2026-06-18T07:34:02+00:00

This is my xml file <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android xmlns:tools=http://schemas.android.com/tools android:id=@+id/LinearLayout0 android:layout_width=match_parent android:layout_height=match_parent

  • 0

This is my xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout0"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CanvasActivity"
android:baselineAligned="false">

<LinearLayout
    android:id="@+id/LinearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="match_parent" 
    android:layout_weight="1"
    android:background="#f9dfcb">  
</LinearLayout>

<LinearLayout
    android:id="@+id/LinearLayout2"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="3"
    android:background="#000000" 
    >


</LinearLayout>

</LinearLayout>

My activity code

import android.app.Activity;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.Toast;

public class CanvasActivity extends Activity {

private GestureDetector gestureDetector;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_canvas);
    gestureDetector = new GestureDetector(this, new GestureListener());



    LinearLayout LinearLayout1=(LinearLayout)findViewById(R.id.LinearLayout1);
    LinearLayout1.setOnTouchListener(new OnTouchListener(){
        public boolean onTouch(View v, MotionEvent event){
            if (gestureDetector.onTouchEvent(event)) 
            {

                return true;
            }
            return false;
        }
    });


/*********EDITED*******/

    LinearLayout LinearLayout2=(LinearLayout)findViewById(R.id.LinearLayout2);
    int width=LinearLayout2.getWidth();
    int height=LinearLayout2.getHeight();
    LinearLayout2.addView(new Canvasview(this), width, height)  ;

    LinearLayout2.setOnTouchListener(new OnTouchListener(){
        public boolean onTouch(View v, MotionEvent event){

            Canvasview cv= new Canvasview(v.getContext());
            cv.onTouchEvent(event);

            return true;
        }
    });
}


//discerning swipes
    class GestureListener extends SimpleOnGestureListener
    {

        private static final int SWIPE_MIN_DISTANCE = 120;
        private static final int SWIPE_THRESHOLD_VELOCITY = 200;

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2,
                                        float velocityX, float velocityY) 
        {
             if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE &&
                     Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            //From Right to Left
             Toast.makeText(CanvasActivity.this, "Left Swipe", Toast.LENGTH_SHORT).show();


                  return true;           


        } 
             else if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE &&
                    Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
            //From Bottom to Top
            Toast.makeText(CanvasActivity.this, "Top Swipe", Toast.LENGTH_SHORT).show();


            return true;
        }

            return false;
        }
        @Override
        public boolean onDown(MotionEvent e) 
        {

            return true;
        }
    }

}

My Canvasview looks like this

public class Canvasview extends SurfaceView implements SurfaceHolder.Callback{

private int width, height;
private Paint textPaint = new Paint();
private Paint touchPaint = new Paint();
private int colors[] = new int[10];

public Canvasview(Context context) {
    super(context);
    SurfaceHolder holder = getHolder();
    holder.addCallback(this);

    setFocusable(true); // make sure we get key events
    setFocusableInTouchMode(true); // make sure we get touch events
    init();
}

private void init() 
{
    textPaint.setColor(Color.WHITE);
    colors[0] = Color.RED;


    touchPaint = new Paint();
    touchPaint.setColor(colors[0]);

}

public boolean onTouchEvent(MotionEvent event) 
{
    Canvas c = getHolder().lockCanvas();

    if (c != null) 
    {

        if (event.getAction() == MotionEvent.ACTION_UP) 
        {
            // clear everything

        }
        else 
        {
            int xval = (int)event.getX();
            int yval = (int)event.getY();
            drawCircle(xval,yval,touchPaint,c);
        }

        getHolder().unlockCanvasAndPost(c);
    }



    return true;

}


public void clear(Canvas c){
    c = getHolder().lockCanvas();
    c.drawColor(Color.BLACK);
    getHolder().unlockCanvasAndPost(c);

}


private void drawCircle(int x, int y, Paint paint, Canvas c) 
{
    c.drawCircle(x, y, 2, paint);

}



@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){

    Canvas c = getHolder().lockCanvas();




    if (c != null) 
    {
        // clear screen
        c.drawColor(Color.BLACK);

        getHolder().unlockCanvasAndPost(c);
    }
}

@Override
public void surfaceCreated(SurfaceHolder arg0) {
    // TODO Auto-generated method stub

}

@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
    // TODO Auto-generated method stub

}

}

I need the canvas only in linearlayout2, but the canvas occupies the entire screen. I am not sure where I am going wrong.

EDIT:
Well I did few changes and I get the Canvasview in linearlayout2, the ontouchevents of Canvasview detects the touch events but in Canvasview public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) method does not initialize a canvas and therefore it is always null. To draw I need the canvas not to be null. Any help is appreciated

  • 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-18T07:34:03+00:00Added an answer on June 18, 2026 at 7:34 am

    I changed my xml file

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout0"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".CanvasActivity"
    android:baselineAligned="false">
    
    
     <View
        android:id="@+id/view0"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" 
        android:layout_weight="3"
        android:background="#f9dfcb"
    
    />
    <com.multitel.testwidget.Canvasview
                    xmlns:android="http://schemas.android.com/apk/res/android" 
                    android:id="@+id/surfaceView1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1">                  
    </com.multitel.testwidget.Canvasview>
    
    
    </LinearLayout>
    

    and changed the constructor in Canvasview to

    public Canvasview(Context context, AttributeSet attrs) 
    {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        SurfaceHolder holder = getHolder();
        holder.addCallback(this);
        setFocusable(true); // make sure we get key events
        setFocusableInTouchMode(true); // make sure we get touch events
        init();
        holder.getSurface();
    }
    

    And i could draw on the canvas!!

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

Sidebar

Related Questions

This is my xml file: <?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:background=@color/background android:orientation=vertical
I have this xml file <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=match_parent android:layout_height=match_parent android:orientation=vertical >
I have this XML file: <?xml version=1.0 encoding=UTF-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation=vertical android:layout_width=fill_parent android:layout_height=fill_parent> <Button
I have this for an XML file: <?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
this is my main .xml file : <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation=vertical android:layout_width=fill_parent
This is the basic XML file <?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
I've got the following xml file: <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:id=@+id/linearLayout1 android:background=@android:color/transparent android:layout_marginTop=0px
This is the manifest file <?xml version=1.0 encoding=utf-8?> <manifest xmlns:android=http://schemas.android.com/apk/res/android package=net.learn2develop.SMSMessaging android:versionCode=1 android:versionName=1.0> <uses-sdk
I have this main.xml file: <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation=vertical android:layout_width=fill_parent android:layout_height=fill_parent android:background=@drawable/background
There is the following xml: <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=match_parent android:layout_height=match_parent android:orientation=vertical >

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.