Connor
Connor

Reputation: 211

Android how to load a new layout

I'm trying to load a new layout when I click a button, this layout has only a webView in it so my goal is, when the button is clicked that it opens the webView and directs the user to a pre-determined page. Right now I have

Button codesBtn = (Button)findViewById(R.id.imagebutton1);
    codesBtn.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            setContentView(R.layout.codes);
        }
    });

This is inside my onCreate() method in my main activity. I have a couple concerns:

1) is this the correct place to put this block of code into?
2) Do I need to create a separate activity for the webView and what I want the button to do?
3) If so, what is the basic structure of the activity needed?

Thanks in advance!

Upvotes: 2

Views: 4447

Answers (1)

Cheryl Simon
Cheryl Simon

Reputation: 46844

In general, rather than changing the layout in the current activity it is easier to launch a new activity with the new layout.

If you want to direct the user to a website, you could use an intent to ask the browser to open (example taken from this question)

String url = "http://almondmendoza.com/android-applications/";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);

Or, you could create an Activity that just has a WebView and launch that by saying;

Intent i = new Intent(this, MyWebViewActivity.class);
i.putExtra("destination", myDestination);
startActivity(i);

Upvotes: 1

Related Questions