How to create an activity without 'setContentView(R.layout.main)'

21,254

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.

Share:
21,254
user1472757
Author by

user1472757

Updated on June 23, 2020

Comments

  • user1472757
    user1472757 about 4 years

    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);
                   }
         }
           }
       }
    
  • user1472757
    user1472757 about 12 years
    Thanks alot Sam, I got it working. My only issue now is positioning the items, such as the button and the imageView created by the myView/onDraw. Ill update the question to show you what i mean. :)
  • user1472757
    user1472757 about 12 years
    Im guessing you can do it by RelativeLayout layout = new RelativeLayout(this); But how can I position the button and imageview?, Thanks
  • Sam
    Sam about 12 years
    @user1472757 I updated the code in my answer. Each attribute can either be set in XML or Java, in the Developer's Guide the XML attributes have a "related method" that is a synonymous Java function (ie for LinearLayout android:orientation is setOrientation()).