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

The Archive Base Latest Questions

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

I have a custom RelativeLayout, where i want to dynamically create Rectangles. They arent

  • 0

I have a custom RelativeLayout, where i want to dynamically create Rectangles.

They arent shown in my current code, but i dont know whats the reason.

Custom RelativeLayout:

public class DrawView extends RelativeLayout {

    public DrawView(Context context) {
        super(context);
        setFocusable(true);

        this.addView(new Rectangle(this.getContext()));
    }

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

    }

Custom Rectangle:

public class Rectangle extends View {

    public Rectangle(Context context) {
        super(context);
    }

    public Rectangle(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public Rectangle(Context context, AttributeSet attrs, int defStyle) { 
        super(context, attrs, defStyle); 
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);

        canvas.drawRect(new Rect(), paint);

    }

}

EDIT:
Solution was:

public class Rectangle extends View {

    public Rectangle(Context context) {
        super(context);
        init();
    }

    public Rectangle(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public Rectangle(Context context, AttributeSet attrs, int defStyle) { 
        super(context, attrs, defStyle); 
        init();
    }

    private void init() {
        this.setBackgroundResource(R.drawable.rectangle);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int width = 150;
        int height = 50;

        setMeasuredDimension(width, height);
    }

}


<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners
        android:radius="5sp"/>
    <gradient
        android:angle="315"
        android:startColor="#FFFF6666"
        android:endColor="#FFFF1111"
        android:type="linear"
        />
</shape>
  • 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-27T15:53:30+00:00Added an answer on May 27, 2026 at 3:53 pm

    I think you are missing to set your Shape’s LayoutParams. Try tho create a Rectangle object, set its width + height and then add it to your RelativeLayout 😀

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

Sidebar

Related Questions

I have a custom RelativeLayout and inside it i have a function that create
I have custom errors configured in my web.config, but IIS 6.0 is returning the
I have custom classes that I currently instantiate within App.xaml as resources. I want
hi i would like to create a custom calendar, this calendar will have custom
I have created a custom tab layout which look like this,I want to make
I have a custom Title Bar so I want that when the user types
I want to create a table layout with custom table rows. Those rows should
Actually I have created a custom ListView . I want to add items to
I create the custom title using the style.I code some from http://labs.makemachine.net/2010/03/custom-android-window-title/ I have
it's my first question. I have built a custom component: a RelativeLayout with 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.