views:

597

answers:

2

Suppose I have a webview open:

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    main_webv = (WebView) findViewById(R.id.mainwebview);
    main_webv.setWebViewClient(new HelloWebViewClient());
    main_webv.getSettings().setJavaScriptEnabled(true);
    main_webv.getSettings().setSupportZoom(false);
    main_webv.addJavascriptInterface(new HelloJavascriptInterface(),"hello");
    main_webv.setWebChromeClient(new HelloWebChromeClient());
    main_webv.loadUrl(SPLASH);   
    main_webv.setVisibility( 4 );

    setContentView(R.layout.main_list);         
    main_listv = (ListView) findViewById(R.id.mainlistview);    


}

I simply want to create a ListView above this webview (covering it up...but sitll allowing the webview to run). I could toggle the views on and off. Thanks.

A: 

You can use FrameLayout; that way both the Views will be arranged on top of another. You can then toggle their visibility using the View.setVisibility(..) method.

[EDIT: ADDED CODE]

This is my layout XML(weblister.xml):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ListView
    android:id="@+id/list_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />
    <WebView
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

</FrameLayout>

Now, I create an Activity which will have both ListView and WebView in its View heirarchy but only one of them would be Visible:

public class WebAct extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.weblister);

            //setup listview
        ListView listView = (ListView) findViewById(R.id.list_view);
        listView.setAdapter(new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,
                new String[]{"One","Two"}){

        });
            // setup web view
        WebView webView = (WebView) findViewById(R.id.webview);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setSupportZoom(false);

        webView.loadUrl("http://www.google.com");

            // set visibility    
        listView.setVisibility(View.INVISIBLE);
        webView.setVisibility(View.VISIBLE);
    }

}

Note: The code might not be complete but I hope it does convey my point to you.

Samuh
What about ListActivity? Right now , my class extends Activity (for my webview). But I cannot extend both.
TIMEX
ListActivity extends Activity. So your class can extends ListActivity and also have a WebView inside.You will just need a ListView in your layout file with android:name = "@android:id/list"(if you choose to extend ListActivity).Also, for you info, there is no need to extend ListActivity, you can have your class extend Activity and have both ListView and WebView in your layout file.
Samuh
I will see if I can find some code for you...just so that things are clearer
Samuh
+1  A: 

Yes, setVisibility will probably help you if you want to programmatically switch Views on (View.VISIBLE) and off (VIEW.GONE).

Concerning your comment to Samuhs answer: In Java you don't have multiple inheritance. But ListActivity inherits from Activity so a ListActivity is pretty much the same (and more) as an Activity.

Also, you don't have to use a ListActivity to display ListViews, it simply provides some convenience-stuff for handling list based activities since they are so common.

What exactly is the error you get?

rflexor
+1 for taking the liberty to answer questions that the OP had
Samuh