Brendan Van Der Merwe
Brendan Van Der Merwe

Reputation: 722

Set presentationDisplay Orientation programmatically

The app I am working on will have 2 displays. Using the DisplayManager I am casting my presentation to a second screen.

I want to find a way to set the orientation of my presentation class to landscape and my activity class to portrait. Code bellow called onCreate of activity to render the presentation.

@Override
protected void onCreate(Bundle savedInstanceState) {

    // Get the display manager service.
    mDisplayManager = (DisplayManager)getSystemService(Context.DISPLAY_SERVICE);
    Display[] presentationDisplays = mDisplayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION);
    if (presentationDisplays.length > 0) {

        mProductPresentation = new ProductPresentation(this, presentationDisplays[0], product);
        mProductPresentation.show();

        Log.d(TAG, " on display #" + presentationDisplays[0].getDisplayId() + ".");
    }
}

The code works perfectly and the display gets rendered correctly. I just want to know if its possible to flip the orientation of the presentation class without changing the orientation of the activity

Upvotes: 1

Views: 313

Answers (1)

Brendan Van Der Merwe
Brendan Van Der Merwe

Reputation: 722

I ended up just grabbing the layout and rotating it. See code below

@Override
protected void onCreate(Bundle savedInstanceState) {
    // Be sure to call the super class.
    super.onCreate(savedInstanceState);

    //Assign the correct layout
    mViewLayout = getLayoutInflater().inflate(R.layout.presentation_product_details, null);
    setContentView(mViewLayout);

    //We need to wait till the view is created so we can flip it and set the width & height dynamically
    mViewLayout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener());
}

private class OnGlobalLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener{

    @Override
    public void onGlobalLayout() {
        //height is ready
        mViewLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);

        int width = mViewLayout.getWidth();
        int height = mViewLayout.getHeight();

        mViewLayout.setTranslationX((width - height) / 2);
        mViewLayout.setTranslationY((height - width) / 2);
        mViewLayout.setRotation(90.0f);

        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams( height, width);

        // Inflate the layout.
        setContentView(mViewLayout, lp);

        findViews();
    }
}

Upvotes: 1

Related Questions