2

I want to design layouts like this:

Portrait mode xml:

<RelativeLayout>
    <ToolBar>
    <ToolBar>
    <TextView>
    </TextView>
    <VideoView>
    <VideoView>
    <Webview>
    </Webview>
</RelativeLayout>

Landscape mode xml:

 <RelativeLayout>
    <VideoView>
    <VideoView>
 </RelativeLayout>

Other views will be invisible for landscape mode. Thanks in advance. I am novice in android development.

Blo
  • 11,903
  • 5
  • 45
  • 99

2 Answers2

1

Since your layout contains videoview I think you wnt to play video on this page.

To prevent activity recreation and video restarts you should add android:configChanges="orientation|screenSize" to activity declaration and than manage layout in onConfigurationChanged

Just find views in onCreate`` than show hide them inonConfigurationChanged```

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // setContentView and another onCreate logic code

    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    textview = findViewById(R.id.textview);
    webView = findViewById(R.id.webview);
    videoView = findViewById(R.id.videoview);

    updateLayout(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    updateLayout(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE);
}

private void updateLayout(boolean isLandscape) {  
    if (isLandscape) {
        textview.setVisibility(View.GONE);
        webview.setVisibility(View.GONE);
        getSupportActionBar().hide();
    } else {
        textview.setVisibility(View.VISIBLE);
        webview.setVisibility(View.VISIBLE);
        getSupportActionBar().show();
    }
}
Anton Pogonets
  • 1,162
  • 7
  • 16
0

You can create two different layout xml file for the two different orientations with same name and store the file for portrait mode in the layout directory and landscape version in the layout-land directory under res directory. I think it's better to do it this way than managing visibility in runtime

Ajay Sivan
  • 2,807
  • 2
  • 32
  • 57
  • This will break video playback on rotation – Anton Pogonets Dec 29 '16 at 17:35
  • @PogonetsAnton Sorry, I didn't played with the `VideoView`. If it break's then its better go with your answer. – Ajay Sivan Dec 29 '16 at 17:39
  • Sorry. My videoview working perfect with @Pogonets Antom answer. – Aslam Hossin Dec 29 '16 at 17:59
  • @AslamHossin Thats great, I am really happy to hear that. Happy coding friend. Also give him a vote – Ajay Sivan Dec 29 '16 at 18:03
  • How to setLayoutParams of video view. for Portrait mode: videoView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 200)); and for Landscape mode : videoView.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT)); now working. please help or suggestion. – Aslam Hossin Dec 31 '16 at 04:25