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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:05:05+00:00 2026-06-04T22:05:05+00:00

The documentation about this is really confusing. I simply need to add rectangels to

  • 0

The documentation about this is really confusing. I simply need to add rectangels to a View I defined in my main.xml layout file. It will be a small part of the layout.

What I want to achieve is, I want to add shelves to a room but since the room shape and shelves change, I need to add them programmatically.

Below is a little part of my main.xml file, you can see the View I defined:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/relativeLayout2"
    android:layout_width="600dp"
    android:layout_height="650dp"
    android:layout_marginTop="70dp"
    android:layout_marginLeft="30dp"
    android:layout_toRightOf="@+id/relativeLayout1" >

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/getDirections"
        android:textSize="20dp"
        android:textStyle="bold" />

    <View
        android:id="@+id/roomplan"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
            android:layout_below="@+id/textView3"
        android:layout_marginTop="40dp"
        android:background="@android:color/white" />

</RelativeLayout>

This is the custom View class I created to handle dynamic changes:

public class CustomView extends View {
    ShapeDrawable roomFrame;
    ArrayList<ShapeDrawable> shelfFrames;

    public CustomView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        roomFrame.draw(canvas);
        for (ShapeDrawable shelfFrame : shelfFrames){
            shelfFrame.draw(canvas);
        }
    }

    public void setRoom(Stage stage){
        roomFrame = new ShapeDrawable(new RectShape());
        roomFrame.getPaint().setColor(0xff74AC23);   
        roomFrame.setBounds(10, 10, stage.getWidth(), stage.getHeight());
    }

    public void setShelves(ArrayList<Shelf> shelves){
        shelfFrames = new ArrayList<ShapeDrawable>();
        for(int i = 0; i<shelves.size(); i++){
            ShapeDrawable shelfFrame = new ShapeDrawable(new RectShape());
            shelfFrame.getPaint().setColor(0xff74AC23);
            shelfFrame.setBounds(shelves.get(i).getXPosition(), shelves.get(i).getYPosition(), shelves.get(i).getWidth(), shelves.get(i).getHeight());
            shelfFrames.add(shelfFrame);
        }
    }
}

Now simply, when a new room plan is asked I am trying to assign this class to the View object in the xml layout:

public void loadRoomPlan(Room room, ArrayList<Shelf> shelves){
    CustomView asdsView = (CustomView)findViewById(R.id.roomplan);
    asdsView.setRoom(room);
    asdsView.setShelves(shelves);
    asdsView.invalidate();
}

I always get

Caused by: java.lang.ClassCastException: android.view.View cannot be cast to org.example.myproject.CustomView

error.

Probably I am doing this very very wrong, am I not?

  • 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-04T22:05:06+00:00Added an answer on June 4, 2026 at 10:05 pm

    The error seems to be in this line:

    CustomView asdsView = (CustomView)findViewById(R.id.shopplan);
    

    What is shopplan? In case it is a mistake and you meant R.id.roomplan try to subtitute the View in your layout for your Custom view:

    <org.example.myproject.CustomView
        android:id="@+id/roomplan"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="40dp"
        android:background="@android:color/white" />
    

    UPDATE:

    Try adding the other two constructors to your CustomView class:

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

    When you use a custom view in an xml layout, your view has to deal with the layout attributes (the constructor AttributeSet param).

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

Sidebar

Related Questions

The documentation is not talking really much about what this technically does. fadeInAnimation.fillMode =
I have looked, searched, and read documentation and can't really find anything about this.
This seems really simple, and I see a lot of documentation about it, but
I read a lot of documentation about this before i decided to ask. So
I have a few questions about this after reading the iPhone documentation on it:
I couldn't find anything about this in MySQL documentation. SELECT accesion_id, definition FROM accesion_table
The MySQL 5.4 documentation, on Optimizing Queries with EXPLAIN , says this about these
Apple really had bad documentation about how the provider connects and communicates to their
I need some advise about how to read PyQt's documentation. Because on my own
I have searched for this, and I can't find any documentation about doing these

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.