Skip to content Skip to sidebar Skip to footer

Android Studio: Activity_main.xml And Content_main.xml Not Showing Design Preview && Classnotfoundexception

I want to use CircularLayout in my android app and I'm following this Circular Layout . Errors: 1)activity_main.xml and content_main.xml are not showing any design preview. 2)On r

Solution 1:

Create outer class YourNewView extends View but not as Activity extends View.

In xml put as <com.example.shalini.circlelayout.YourNewView

Wherever you use id for your new circular layout cast it with YourNewView

Try it.

publicclassYourNewViewextendsView {
privatefinalstaticintTOTAL_DEGREE=360;
privatefinalstaticintSTART_DEGREE= -90;

private Paint mPaint;
privateRectFmOvalRect=null;

privateintmItemCount=5;
privateint mSweepAngle;

privateint mInnerRadius;
privateint mOuterRadius;
private Bitmap mCenterIcon;
privateint[] mColors = {Color.RED, Color.YELLOW, Color.GREEN, Color.BLUE, Color.CYAN};
private String[] mTitles = {"APPT CENTER", "MEDS CABINET", "CHECK-IN", "MY TRACKERS", "MY ACCOUNTS"};


publicYourNewView(Context context) {
    this(context, null);
}

publicYourNewView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

publicYourNewView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    mPaint = newPaint(Paint.ANTI_ALIAS_FLAG);
    mPaint.setStrokeWidth(2);

    mSweepAngle = TOTAL_DEGREE / mItemCount;

    mInnerRadius = 125;
    mOuterRadius = 400;

    mCenterIcon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
}

@OverrideprotectedvoidonDraw(Canvas canvas) {

    intwidth= getWidth();
    intheight= getHeight();

    if (mOvalRect == null) {
        mOvalRect = newRectF(width / 2 - mOuterRadius, height / 2 - mOuterRadius, width / 2 + mOuterRadius, height / 2 + mOuterRadius);
    }

    for (inti=0; i < mItemCount; i++) {
        intstartAngle= START_DEGREE + i * mSweepAngle;
        mPaint.setColor(mColors[i]);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawArc(mOvalRect, startAngle, mSweepAngle, true, mPaint);

        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
        canvas.drawArc(mOvalRect, startAngle, mSweepAngle, true, mPaint);

        intcenterX= (int) ((mOuterRadius + mInnerRadius) / 2 * Math.cos(Math.toRadians(startAngle + mSweepAngle / 2)));
        intcenterY= (int) ((mOuterRadius + mInnerRadius) / 2 * Math.sin(Math.toRadians(startAngle + mSweepAngle / 2)));
        canvas.drawBitmap(mCenterIcon, width / 2 + centerX - mCenterIcon.getWidth() / 2, height / 2 + centerY - mCenterIcon.getHeight() / 2, null);

        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawText(mTitles[i], width / 2 + centerX - mCenterIcon.getWidth() / 2, height / 2 + centerY + mCenterIcon.getHeight(), mPaint);
    }

    mPaint.setColor(Color.WHITE);
    mPaint.setStyle(Paint.Style.FILL);
    canvas.drawCircle(width / 2, height / 2, mInnerRadius, mPaint);
    canvas.drawBitmap(mCenterIcon, width / 2 - mCenterIcon.getWidth() / 2, height / 2 - mCenterIcon.getHeight() / 2, null);

    super.onDraw(canvas);


   }
  }

Please remove Activity extends View from MainActivity

Post a Comment for "Android Studio: Activity_main.xml And Content_main.xml Not Showing Design Preview && Classnotfoundexception"