Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to draw a color rectangle when I tap on android phone screen???????????????????

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to draw a color rectangle when I tap on android phone screen???????????????????

    I am trying to display a rectangle when i tap on my android phone screen after I run the android project. This is what I have tried so far . Could anyone give an idea what to use in onTouchListener method to actually make a rectangle display when i tap ??

    package example.rectangle;
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Paint;
    import android.graphics.Rect;
    import android.graphics.drawable.Drawable;
    import android.graphics.drawable.ShapeDrawable;
    import android.graphics.drawable.shapes.*;
    import android.os.Bundle;
    import android.os.Handler;
    import android.os.Message;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.KeyEvent;
    import android.view.MotionEvent;
    import android.view.View;
     
    import java.util.Timer;
    import java.util.TimerTask;
     
    public class RectView extends View
    {
    	ShapeDrawable rect ;
    	public Rect rect1 ;
    	public int argbColour ;
     
        public RectView(Context context , AttributeSet attrs) 
           {
    		super(context , attrs);
    		initRectangle();
    	}
     
        public RectView (Context context , AttributeSet attrs , int defStyle)
        {
        	super(context , attrs , defStyle);
        	initRectangle();
        }
     
         private void initRectangle()
         {
        	 rect = new ShapeDrawable(new RectShape());
        	 rect.getPaint().setColor(0xffff0000);
         }
    //Draws the rectangle on canvas with a ShapeDrawable object
     
    public void onDraw(Canvas canvas)
    {  
       super.onDraw(canvas);
     
       rect.setBounds(250, 25,450, 600);
       rect.draw(canvas);
     
      }
     
    //view.setOnTouchListener(new TouchListener());
     
    public class TouchListener implements View.OnTouchListener
    {
    	public boolean onTouch(View v, MotionEvent e) {
    		// TODO Auto-generated method stub
     
    		if (v.getId()==R.id.rect )
    		{
    			// how to draw rectangle while the user tap on screen???????
    		}
     
    		return true;
    	}
       }
    }
     
    // *******************Another activity class*****************
    package example.rectangle;
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
     
    public class RectActivity extends Activity {
     
    	private RectView rectView; 
     
    	public void onCreate(Bundle savedInstanceState)
    	{   super.onCreate(savedInstanceState);
    	  	setContentView(R.layout.activity_rect);  
    	  	rectView = (RectView)findViewById(R.id.rect);
    	  	if (savedInstanceState != null)
    	  	{  // activity being restored from earlier launch
     
    	  	}	
    	}
     
    }


  2. #2
    Junior Member
    Join Date
    Sep 2014
    Posts
    9
    My Mood
    Dead
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to draw a color rectangle when I tap on android phone screen???????????????????

    what if you just create a large transparent button that covers whole screen and when it is clicked then it draws rectangle.

    --- Update ---

    I think if you insert

    mSquare = new Square();

    it might work

Similar Threads

  1. Replies: 2
    Last Post: June 9th, 2014, 04:06 PM
  2. How to draw a rectangle on a JPanel, from a different class?
    By AvivC in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 25th, 2013, 08:31 AM
  3. draw line to edge of rectangle
    By Bc1151 in forum Java Theory & Questions
    Replies: 1
    Last Post: April 11th, 2013, 02:33 PM
  4. Writing a method to draw a rotatable rectangle using Graphics2D
    By johnpooley3 in forum AWT / Java Swing
    Replies: 3
    Last Post: May 18th, 2012, 09:28 AM
  5. Unable to draw a rectangle
    By n00b in forum AWT / Java Swing
    Replies: 3
    Last Post: November 1st, 2011, 07:32 AM