JeffLemon
JeffLemon

Reputation: 273

Switch between 2 layouts in Android

I have two layouts, what is the best way to switch between them programatically (not using an xml file).

And how can I do it?

Upvotes: 1

Views: 925

Answers (1)

Basic Coder
Basic Coder

Reputation: 11422

Basically there are two ways.

1.) you got one XML containing all components you want to use. The ones which are not available at the moment, should bis hidden. When the user should have the possibility to use them, just make them visible

2.) this is definitly the better solution because Android is concipated for this method. You have 2 Activities and 2 layout XML files. When you want to display another layout, start the second Activity.

in your first Activity:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainlayout);

    Button btn = (Button) findViewById(R.id.button1);
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(StackOverflowActivity.this, Login.class);
            startActivityForResult(i, LOGIN_REQUEST);
        }
    });

}

your second Activity;

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

Upvotes: 2

Related Questions