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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:49:39+00:00 2026-06-18T00:49:39+00:00

I have a custom class that extends ImageView public class MyCompass extends ImageView {

  • 0

I have a custom class that extends ImageView

    public class MyCompass extends ImageView 
    {
         private float direction;
// Rest Code

    }
      <ImageView class="myPakagename.MyCompass"
                android:layout_centerInParent="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/immg"
                android:src="@drawable/hub"/>

I am trying to call this in my fragment as

   myCompass = (MyCompass) view.findViewById(R.id.immg);

However this gives me classCastException. How can i make my Class extends ImageView?

@SuppressLint("DrawAllocation")
public class MyCompass extends ImageView 
{
     private float direction;

     public MyCompass(Context context) 
     {
      super(context);
      setImageResource(R.drawable.qibla_compass_hub);
     }

     public MyCompass(Context context, AttributeSet attrs) {
      super(context, attrs);
      this.setImageResource(R.drawable.qibla_compass_hub);        
      }

     public MyCompass(Context context, AttributeSet attrs, int defStyle) {
      super(context, attrs, defStyle);
      this.setImageResource(R.drawable.qibla_compass_hub);                                
      }

     @Override
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      setMeasuredDimension(
        MeasureSpec.getSize(widthMeasureSpec),
        MeasureSpec.getSize(heightMeasureSpec));
     }

     @Override
     protected void onDraw(Canvas canvas) {
      int w = getMeasuredWidth();
      int h = getMeasuredHeight();
      canvas.rotate(direction, w / 2, h / 2);

     }

     public void update(float dir){
      direction = dir;
      invalidate();
     }

My layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/qiblaBg"
    android:background="@drawable/qibla_bg">    

    <RelativeLayout 
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/qibla_compass_outer">

          <mypakage.MyCompass
              android:contentDescription="@string/qibla"
                    android:layout_centerInParent="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/qiblaImg"

// android:src=@”img” doesn’t work but android:background works however it again stretches to full screen.
/>

                            <RelativeLayout 
                                android:id="@+id/qiblaArrow"
                                android:layout_centerInParent="true"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:orientation="vertical" 
                                android:background="@drawable/qibla_compass_arrow" />      
    </RelativeLayout>


</RelativeLayout>

Using this, my MyCompass ImageView in layout stretches to full screen and Does’t show any image.

  • 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-18T00:49:41+00:00Added an answer on June 18, 2026 at 12:49 am

    You can refer to your custom image view in the xml directly using:

    <myPakagename.MyCompass
                android:layout_centerInParent="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/immg"
                android:src="@drawable/hub"/>
    

    And make sure that your custom view overrides:

    public MyCompass(Context context) {}
    

    And

    public MyCompass(Context context, AttributeSet attrs) {}
    

    And

    public MyCompass(Context context, AttributeSet attrs, int defStyle) { }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom class that I've written that extends ImageView (for Android Java).
I have a custom-made view that extends the View class. I would like 2
I have a custom class that extends Preference that I'm using in conjunction with
I have a custom class called NoteView that extends FrameLayout . Previously, I had
I have a custom class that extends MovieClip . The idea is that it
Hi I have a custom class that extends Sprite, that controls positioning on the
I have a custom View class that extends Spinner. I'm trying to figure out
I have a custom class that extends NSString . I'm attempting to serialize it
I currently have a custom Dialog class that extends DialogPreference(which of course is a
I have a custom class vtools.Image that extends BufferedImage. Then I am reading 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.