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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:39:21+00:00 2026-06-06T15:39:21+00:00

I know its possible to create activities by doing something like the code bellow,

  • 0

I know its possible to create activities by doing something like the code bellow, where the view is not set from xml file but like this: setContentView(new myView(this));

What i don’t understand is how to use this code but still have the ability to customize it, for instance if i wanted to add a button to the code bellow, how would i do it, because i cant simply add one to an xml layout can i?

ANY GOOD ANSWERS TO THIS WILL VERY MUCH APPRECIATED
thanks in advance!

package com.faceapp;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.media.FaceDetector;
import android.media.FaceDetector.Face;
import android.os.Bundle;
import android.view.View;

 public class FaceappActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        setContentView(new myView(this));
    }

    private class myView extends View{

     private int imageWidth, imageHeight;
     private int numberOfFace = 5;
     private FaceDetector myFaceDetect; 
     private FaceDetector.Face[] myFace;
     float myEyesDistance;
     int numberOfFaceDetected;

     Bitmap myBitmap;


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

   BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options();
   BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565; 
   myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.face5,   
      BitmapFactoryOptionsbfo);
   imageWidth = myBitmap.getWidth();
   imageHeight = myBitmap.getHeight();
   myFace = new FaceDetector.Face[numberOfFace];
   myFaceDetect = new FaceDetector(imageWidth, imageHeight, numberOfFace);
   numberOfFaceDetected = myFaceDetect.findFaces(myBitmap, myFace); 

  }

  @Override
  protected void onDraw(Canvas canvas) {
   // TODO Auto-generated method stub

            canvas.drawBitmap(myBitmap, 0, 0, null);

            Paint myPaint = new Paint();
            myPaint.setColor(Color.GREEN);
            myPaint.setStyle(Paint.Style.STROKE); 
            myPaint.setStrokeWidth(3);

            for(int i=0; i < numberOfFaceDetected; i++)
            {
             Face face = myFace[i];
             PointF myMidPoint = new PointF();
             face.getMidPoint(myMidPoint);
    myEyesDistance = face.eyesDistance();
             canvas.drawRect(
               (int)(myMidPoint.x - myEyesDistance),
               (int)(myMidPoint.y - myEyesDistance),
               (int)(myMidPoint.x + myEyesDistance),
               (int)(myMidPoint.y + myEyesDistance),
               myPaint);
            }
  }
    }
}

^^^^^^^^^^^^^^^
Answered

How to position the button and imageview? (Ideally using relative layout)
The picture bellow shows you what i mean:
(Ignore that the image is re-sized)

enter image description here
NEW CODE:

package com.test;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.media.FaceDetector;
import android.media.FaceDetector.Face;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class TesttActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        LinearLayout layout = new LinearLayout(this);
        Button button = new Button(this);
        button.setText("Button!");
        layout.addView(button);

        myView custom = new myView(this);
        layout.addView(custom);

        setContentView(layout);
    }

    private class myView extends View{

        private int imageWidth, imageHeight;
        private int numberOfFace = 5;
        private FaceDetector myFaceDetect; 
        private FaceDetector.Face[] myFace;
        float myEyesDistance;
        int numberOfFaceDetected;

        Bitmap myBitmap;


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

      BitmapFactory.Options BitmapFactoryOptionsbfo = new BitmapFactory.Options();
      BitmapFactoryOptionsbfo.inPreferredConfig = Bitmap.Config.RGB_565; 
      myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.face5,   
      BitmapFactoryOptionsbfo);
      imageWidth = myBitmap.getWidth();
      imageHeight = myBitmap.getHeight();
      myFace = new FaceDetector.Face[numberOfFace];
      myFaceDetect = new FaceDetector(imageWidth, imageHeight, numberOfFace);
      numberOfFaceDetected = myFaceDetect.findFaces(myBitmap, myFace); 

     }

     @Override
     protected void onDraw(Canvas canvas) {
      // TODO Auto-generated method stub

               canvas.drawBitmap(myBitmap, 0, 0, null);

               Paint myPaint = new Paint();
               myPaint.setColor(Color.GREEN);
               myPaint.setStyle(Paint.Style.STROKE); 
               myPaint.setStrokeWidth(3);

               for(int i=0; i < numberOfFaceDetected; i++)
               {
                Face face = myFace[i];
                PointF myMidPoint = new PointF();
                face.getMidPoint(myMidPoint);
       myEyesDistance = face.eyesDistance();
                canvas.drawRect(
                  (int)(myMidPoint.x - myEyesDistance),
                  (int)(myMidPoint.y - myEyesDistance),
                  (int)(myMidPoint.x + myEyesDistance),
                  (int)(myMidPoint.y + myEyesDistance),
                  myPaint);
               }
     }
       }
   }
  • 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-06T15:39:22+00:00Added an answer on June 6, 2026 at 3:39 pm

    You can pass setContentView() any form of view, to be the root view of your layout. Below is a dynamically built LinearLayout with a Button and your myView.

    public class Example extends Activity {
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
    
            LinearLayout layout = new LinearLayout(this);
            // Define the LinearLayout's characteristics
            layout.setGravity(Gravity.CENTER);
            layout.setOrientation(LinearLayout.VERTICAL);
    
            // Set generic layout parameters
            LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    
            Button button = new Button(this);
            button.setText("Button!");
            layout.addView(button, params); // Modify this
    
            myView custom = new myView(this);
            layout.addView(custom, params); // Of course, this too
    
            setContentView(layout);
        }
    }
    

    Understand that you can only add child views to your root view if you pass setContentView() a ViewGroup; like RelativeLayout, LinearLayout, etc. In other words you cannot do this:

            myView custom = new myView(this);
    
            Button button = new Button(this);
            button.setText("Button!");
    
            custom.addView(button); 
            // Nope! Method "addView()" does not exist for a regular View...
    
            setContentView(custom);
    

    Also, naming convention suggests that each word in a class name should have the first letter capitalized. So myView ought to be MyView, at a minimum it makes your code easier to read for other programmers and the compiler will highlight your class variables with the correct color.

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

Sidebar

Related Questions

I would like to know if its possible to get 2 sums from one
I would like to know if its possible to create two triggers, one on
Anyone know if its possible to yet create a css based drop shadow on
I'd like to know whether it's possible to create a VPN interface programmatically with
Hi it'd like to know if it's at all possible create a parametric equalizer
I know its possible to open an app from mobile safari using custom URL
I am doing an android applicaiton and I know its possible to show or
I know its possible to create a table that has an index on the
Possible Duplicate: internationalization of an iPhone Application I know that its possible to create
Does anyone know if its possible to set a Secure Cookie inside a Header?

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.