How to display 2 views in 1 activity (Android)?

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 just want to create a ListView over this webview (covering it ... but a sitll letting you start the webview). I could turn on and off the views. Thanks.

+4
source share
2 answers

You can use FrameLayout ; so both views will be placed on top of the other. Then you can switch their visibility using the View.setVisibility(..) method.

[EDIT: ADDED CODE]

This is my XML layout (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 am creating an Activity that will have both ListView and WebView in its view hierarchy, but only one of them will 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 may not be complete, but I hope it conveys my point.

+5
source

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

Regarding your comment on Samuhs, the answer is: in Java you don't have multiple inheritance. But ListActivity inherits from Activity, so ListActivity is almost the same (and more) as Activity.

Also, you don’t have to use ListActivity to display ListViews, it just provides some convenience for handling list-based actions, as they are so common.

What exactly do you get?

+1
source

Source: https://habr.com/ru/post/1300852/


All Articles