Question

I want to know is there any way to use native TextView or any other layout of android inside BaseAndEngine Activity.

My application is using Andengine for one of its whole screen. This screen is extended from BaseAndEngine and I need to use some native view like textview inside that screen. Because Andengine does not work fine for Arabic text and I need to show some Arabic text on gaming screen.

OR if possible how to show Arabic text in changeable text in Andengine. As changeable text write Arabic from left to right in reverse order.

Was it helpful?

Solution

Of course you can.

Check this code out - basically you override onSetContentView, then you can set whatever you want.

@Override
protected void onSetContentView() {
    final FrameLayout frameLayout = new FrameLayout(this);
    final FrameLayout.LayoutParams frameLayoutLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT);

    this.mRenderSurfaceView = new RenderSurfaceView(this);
    mRenderSurfaceView.setRenderer(mEngine);
    final FrameLayout.LayoutParams surfaceViewLayoutParams = new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());
    frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);

    //Create any other views you want here, and add them to the frameLayout.

    this.setContentView(frameLayout, frameLayoutLayoutParams);
}

(This method goes to your subclass of BaseGameActivity.)

You could also do it through xml, but I think this method is more clear.

OTHER TIPS

You can use Andengine for Arabic and Persian fonts too. But in a different manner. to do that you need to create a Sprite and add a bitmap to it. before that you draw your text on that bitmap.

the following code is an example that draw the Persian/Arabians text and attach it to a sprite. so we can attach the sprite to our scene. this is an example to show how we can do that, so you can adjust the bitmap and text size by yourself. if your device support Persian/Arabians, this code will work properly. if the text does not appear in your scene, change its position, it is out of screen

the example code function will print the "Persian Golf" in Persian/Arabians.

private void persianGolfPrinter(){
            BitmapTextureAtlas mBitmapTextureAtlas = new BitmapTextureAtlas(ResourceManager.getInstance().gameActivity.getTextureManager(), 400, 800, TextureOptions.BILINEAR);
            ITextureRegion mDecoratedBalloonTextureRegion;
            final IBitmapTextureAtlasSource baseTextureSource = new EmptyBitmapTextureAtlasSource(400, 800);
            final IBitmapTextureAtlasSource decoratedTextureAtlasSource = new BaseBitmapTextureAtlasSourceDecorator(baseTextureSource) {
                    @Override
                    protected void onDecorateBitmap(Canvas pCanvas) throws Exception {
                            this.mPaint.setColor(Color.BLACK);
                            this.mPaint.setStyle(Style.FILL);
                            this.mPaint.setTextSize(32f);
                            this.mPaint.setTextAlign(Align.CENTER);
                            pCanvas.drawText("خلیج فارس", 150, 150, this.mPaint);

                    }

                    @Override
                    public BaseBitmapTextureAtlasSourceDecorator deepCopy() {
                            throw new DeepCopyNotSupportedException();
                    }
            };

            mDecoratedBalloonTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromSource(mBitmapTextureAtlas, decoratedTextureAtlasSource, 0, 0);
            mBitmapTextureAtlas.load();

            Sprite test = new Sprite(0,0,mDecoratedBalloonTextureRegion,ResourceManager.getInstance().engine.getVertexBufferObjectManager());
            this.attachChild(test);
    }

don't use android textview... it makes your game ugly ....

You cannot use them directly in AndEngine, because the objects in Andengine are OpenGL objects. But you can use android OS to draw a bitmap of any standard view, and then create a texture and a sprite from that view. This is less than optimum for a case such as arabic text, but it will work. Be careful about memory leaks as you create the bitmaps of your views.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top