How to implement ViewSwitcher

209 Views Asked by At

I have two XML files currently. I have my activity_main.xml and a second file called horizontal.xml I want to switch between the two such that when the phone is held vertically it displays activity_main and when the phone is held horizontally it displays horizontal.xml Thus far, I have been unsuccessful implementing the viewswitcher. Any suggestions?

package com.example.flash;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
1

There are 1 best solutions below

0
On

You don't want to do that, as Android has an implemented way to this: Providing Resources/Alternative Resources.

In short: You put the landscape layout in layout-land, and it is switched automatically by Android for you. No need to code anything :)