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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:15:09+00:00 2026-06-02T03:15:09+00:00

I am trying to make clickable rectangles. I’ve looked around and I created quite

  • 0

I am trying to make clickable rectangles. I’ve looked around and I created quite good code 🙂

I create constructor of my own rectangle class, then i set some values of it.
However, onDraw method looks like creates and draw rectangle but without constructor’s new variables’ values.
What do I do wrong?

This is default MyActivity class:

ll = (LinearLayout)findViewById(R.id.linearlayout);
List<MiniRectangle> miniRectangleList = new ArrayList<MiniRectangle>();

for(int i=0;i<8;i++)
{
    int numberRandom = r.nextInt(3);

    MiniRectangle miniRectangle = new MiniRectangle(this);
    miniRectangle.set_color(colors.get(numberRandom));
    miniRectangle.set_size(50);
    miniRectangle.set_id_color(numberRandom);
    miniRectangle.set_number(i);

    ll.addView(miniRectangle);

    miniRectangleList.add(miniRectangle);
}

setContentView(ll);

This is my own rectangle class

public class MiniRectangle extends View implements View.OnClickListener {

    Context context;
    int _size;
    int _color;
    int _id_color;
    int _number;

    public MiniRectangle(Context context) {
        super(context);
        this.context = context;
        setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        System.out.println(get_number());
        Toast.makeText(context, get_number(), Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Paint paint = new Paint();
        paint.setColor(get_color());
        paint.setStrokeWidth(1);

        int kleft = (get_number() % 8) * get_size();
        int kright = kleft + get_size() - 2;

        int ktop = 1 * get_size();
        int kbottom = ktop + get_size() - 2;

        canvas.drawRect(kleft, ktop, kright, kbottom, paint);
    }
}

I can say that after run project I get only one rectangle. No wonder if every rectangle has the same position. Please Help.


The result should shows chess of clickable rectangles.

  • 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-02T03:15:10+00:00Added an answer on June 2, 2026 at 3:15 am

    Try to use

    ll.addView(miniRectangle, new LayoutParams(50, 50));
    

    with you own parameters for LayoutParams instead of

    ll.addView(miniRectangle);
    

    and start in onDraw() with

    canvas.drawRect(0, 0, get_size(), get_size(), paint);
    

    with respect to your LayoutParams values.

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

Sidebar

Related Questions

I am trying to make an entire div tag clickable. The code I am
I am trying to dynamically make divs that are clickable. I have inserted a
I am trying to make the images clickable so that when they are pressed
I am trying to make each number displayed clickable. 1 should alert() 80, 2
I'm trying to make a navigation where there are 4 clickable links. Those links
Trying to make td elements clickable. Here's my HTML: <td id=A3 class= open-square><a href=/gameplay/A3></a></td>
I am trying to make my TextView clickable. I have set setMovementMethod() with LinkMovementMethod.
I'm trying to make a couple of DIVs clickable in its entirety. I tried
I am trying to make a link unclickable once it is clicked, then clickable
I'm trying to make a clickable region. <a style=display: block href=http://stackoverflow.com> StackOverflow </a> A

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.