2

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!

Ant4res
  • 1,217
  • 1
  • 18
  • 36
Connor
  • 211
  • 2
  • 8
  • 17
  • Have you tried it? it looks generally OK; no, you typically don't need a separate activity. If you've stumbled upon something specific, do ask a specific question. – Seva Alekseyev Feb 22 '11 at 18:08
  • @SevaAlekseyev well the error I'm getting with this block of code is that it doesn't recognize "codes" as a layout. Do I have to do something to the manifest file in order to use "codes" like I have above? – Connor Feb 22 '11 at 18:53
  • Edit codes.xml, make a layout the top-level object. A FrameLayout or a LinearLayout would do. Make your WebView its child. In Android, the top-level object of a layout XML file should be - duh - a layout, not a data view. – Seva Alekseyev Feb 22 '11 at 19:07

1 Answers1

1

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);
Community
  • 1
  • 1
Cheryl Simon
  • 46,552
  • 15
  • 93
  • 82
  • 1
    -1 Activities are expensive. If you just want to change the view, it's much easier to have a FrameLayout as a top-level view and flip frames within it. – Seva Alekseyev Feb 22 '11 at 19:05
  • @Seva Alekseyev Seriously? Please backup your claim. If you really think that is a better solution, you should provide an answer with the details of that. However, the standard best practices for Android is to use a different Activity for each unique visual interface. From the docs "An activity presents a visual user interface for one focused endeavor the user can undertake." Using different Activities gives you lots of nice default behavior like proper back behavior and familiar screen transitions that will make your app easy for your users to learn. – Cheryl Simon Feb 22 '11 at 19:55
  • If you swap views you need to implement all of that yourself, its certainly possible, but way overkill in the simple case. In general, only optimize for performance when you have done things the easy way, and find that you have an actual performance problem. Loading a WebView in a new Activity is not going to be an issue on any Android phone. – Cheryl Simon Feb 22 '11 at 19:56
  • Activities come with the overhead of: state management, nontrivial lifetime, intent processing, context management (resources and such), dialog box stack... Nothing of this is ever needed for the simple task of view flipping. – Seva Alekseyev Feb 22 '11 at 20:33
  • @Seva Alekseyev, Yes, there is overhead. I didn't say there wasn't. But is the overhead worth the gained simplicity? In this case I'd say definitely, yes. Some of this "overhead" is actually very useful for switching between views, thats why the Activities exist in the first place. If you are writing a game or something with strict speed requirements maybe you need to look at optimizing, but not for such a simple case as this. How many ms does using an Activity add? Compared to the time to load a web page? – Cheryl Simon Feb 22 '11 at 21:11