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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:21:54+00:00 2026-05-30T15:21:54+00:00

I have a simple class, BoundedView that extends View . I’m doing this primarily

  • 0

I have a simple class, BoundedView that extends View. I’m doing this primarily to mess with the onTouchEvent callback function.

Is there a way to draw a border around each instance of this view, from the class itself? If not, what is the easiest way to implement this?

Implementation:

public class BoundedView extends View
{
  public String cellName = "no name";

  // constructors are here.

  @Override
  public void onDraw( Canvas canvas )
  {
    // maybe here? Right now it's invisible, used only for touch detection
  }

  @Override
  public boolean onTouchEvent( MotionEvent event )
  {
    Intent i = new Intent( getContext(), TabCellDetails.class );
    i.putExtra( "cellName", this.cellName );
    getContext().startActivity( i );

    return false;
  }
}

Using:

<com.lifecoderdev.android.drawing1.BoundedView
        android:id="@+id/boundedView1"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="78dp"
        android:layout_marginRight="96dp" 
        android:tag="Smooth Endoplasmic Reticulum"/>

EDIT: This gets me close:

public void onDraw( Canvas canvas )
{
  int[] colors = { 0xFF000000, 0xCC000000 };
  float[] radii = { 5, 5, 5, 5, 5, 5, 5, 5 };
  GradientDrawable drawable = new GradientDrawable( GradientDrawable.Orientation.TOP_BOTTOM, colors );
  drawable.setCornerRadii( radii );
  drawable.setStroke( 1, 0xFF000000 );
  this.setBackgroundDrawable( drawable );
}

However, it is drawing a fully filled in black box, not a transparent one with a black border.

EDIT 2: Got it:

Paint paint = new Paint();

paint.setColor( Color.RED );
paint.setStrokeWidth( 1.0f );

canvas.drawRect( 0, 0, getWidth(), 1.0f, paint );
canvas.drawRect( 0, 0, 1.0f, getHeight(), paint );
canvas.drawRect( 0, getHeight()-1.0f, getWidth(), getHeight(), paint );
canvas.drawRect( getWidth()-1.0f, 0, getHeight(), getWidth(), paint );

EDIT 3: Andreas and Warren’s solution was much nicer:

@Override
public void onDraw( Canvas canvas )
{
  Paint paint = new Paint();
  paint.setColor( Color.RED );
  paint.setStrokeWidth( 1.5f );
  paint.setStyle( Style.STROKE );

  canvas.drawRect( 0, 0, getWidth(), getHeight(), paint );
}
  • 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-30T15:21:55+00:00Added an answer on May 30, 2026 at 3:21 pm

    Yes inside your onDraw() is the right place.

    canvas.drawRect(0, 0, getWidth(), getHeight(), paint);
    

    That should work. If you set the paint variable correctly (stroke width, color) you should see your border.

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

Sidebar

Related Questions

I have a simple class in python that has a __str__() function. Like this:
Let's say I have a simple class called WebsterDictionary that has a function that
I have a simple class that provides state codes like this: public class StateProvider
i have this simple class: class A { var $children=array(); function &__get($name) { if($name===firstChild)
I have a simple class that looks a bit like this: @protocol Recorder @property(BOOL)
I have a simple class that essentially just holds some values. I have overridden
I have a simple Class Hierarchy that I am trying to get to work
I have a simple class that contains some general information about the current web
If I have a simple class setup like this: class MyClass { private string
I have a simple class that holds skeleton for a much larger, bulkier class.

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.