I am creating an application in which I have to use webView and display a HTML file saved in application in assets folder. This is my mainActivity code.
public class MainActivity extends Activity {
WebView browser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
browser = (WebView)findViewById(R.id.wv1);
browser.loadUrl("file:///android_asset/test.htm");
WebSettings webSettings = browser.getSettings();
browser.getSettings().setJavaScriptEnabled(true);
setContentView(browser);
}
}
and this is my xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Test Application"
android:gravity="center_horizontal">
</TextView>
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/wv1"
android:layout_marginTop="3dp">
</WebView>
whin I am trying to run this application , it goes crash..
can any one please help me to find out what is the mistake i am doing.
You cannot call setContentView(browser) because browser is already a child of another view -- the LinearLayout.
If you don't need the other views, just change the layout xml file and remove them, leaving only the WebView. Otherwise, remove the setContentView(browser) line.
You're using setContentView() twice and supplying the view that already has a parent. That is why it is giving you the error saying call removeView() on the child's parent first. So remove setContentView(browser).
Your code should look like this.
public class MainActivity extends Activity {
WebView browser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
browser = (WebView)findViewById(R.id.wv1);
browser.loadUrl("file:///android_asset/test.htm");
browser.getSettings().setJavaScriptEnabled(true);
}
}
Related
I have an Android Activity as below.
public class DummyActivity extends android.support.v7.app.ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dummy_layout);
Intent intent = getIntent();
int dots = intent.getExtras().getInt("dots");
}
}
This activity gets the value of dots from another activity without any problem.
Now I want to pass dots to the layout dummy_layout which is given below.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.test.DummyView
android:id="#+id/dummyView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
</LinearLayout>
I tried creating a hidden field in 'dummy_layout' but could not get it working. Is there any straight forward way I can do this ?. I need to access the value of 'dots' in the init method of 'dummyView' which is the class handling 'dummy_layout' as seen in the layout xml.
Why don't you just do
((DummyView) findViewById(R.id.dummyView)).setDots(dots)
in your onCreate?
I am trying to load url inside my custom webview
webview.xml-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:id="#+id/scrollview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none">
<WebView
android:id="#+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</ScrollView>
</RelativeLayout>
and here is my classfile
public class AppDev extends CordovaActivity implements WLInitWebFrameworkListener {
private WebView webview;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
WL.createInstance(this);
WL.getInstance().showSplashScreen(this);
WL.getInstance().initializeWebFramework(getApplicationContext(), this);
webview = (WebView)findViewById(R.id.webView);
}
public void onInitWebFrameworkComplete(WLInitWebFrameworkResult result){
if (result.getStatusCode() == WLInitWebFrameworkResult.SUCCESS) {
// super.loadUrl(WL.getInstance().getMainHtmlFilePath());//
webview.loadUrl(WL.getInstance().getMainHtmlFilePath());
} else {
handleWebFrameworkInitFailure(result);
}}
loading url in webview is producing following error-
com.worklight.common.Logger$UncaughtExceptionHandler
java.lang.NullPointerException
com.worklight.common.Logger$UncaughtExceptionHandler(com.AppDev.AppDev.onInitWebFrameworkCo mplete(AppDev.java:49)
is there anyway to load url in custom webview not in cordova?
Any Help :(
Did you try following the Integrating Server Generated Pages in Hybrid Applications training module and sample application?
The module explains how to bring up a webview and display an external website in it (and return back to the application's webview).
I am trying to stream a video stored on dropbox on android's webview but only a blank space is
displayed, my code is below :
activity_web_view.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/webView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/tview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/webView"
android:text="Description" >
</TextView>
</RelativeLayout>
public class WebViewA extends Activity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://www.dropbox.com/s/XXXX/video.mp4?dl=0?client_id=XXXX");
}
}
I have also tried the following Urls on webView.loadUrl(), but none
of them work, only a blank space appears :
"https://dl.dropbox.com/s/XXXX/video1.mp4?dl=0?client_id=XXXX"
"https://dl.dropbox.com/s/XXXX/video2.mp4?dl=1?client_id=XXXX"
"https://dl.dropboxusercontent.com/s/XXXX/video1.mp4"
"https://www.dropbox.com/s/XXXX/video1.mp4?dl=0"
if I try http://www.google.com it works, I have also seen some tutorials
with dropbox addresses that have an 'u' instead of an 's' like
"https://www.dropbox.com/u/..."
does WebView only works with dropbox urls with an 'u', if so how can I
get that type of address.
I'm trying to build an android app, which just consists of a webview. I took the example from android dev site and modified it. But as simple as it looks, every call to findViewById results in a null pointer.
The .java file:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webView = (WebView) findViewById(R.id.webview);
// returns null pointer
webView.loadUrl("file:///android_asset/index.html");
setContentView(webView);
}
}
The layout file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context=".MainActivity" >
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
And I put following line into the manifest:
<uses-permission android:name="android.permission.INTERNET" />
I'm puzzled ...
Change to
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout); // muylayout is your layout.xml
WebView webView = (WebView) findViewById(R.id.webview);
// returns null pointer
webView.loadUrl("file:///android_asset/index.html");
}
}
You need to set the content of your layout to the activity before calling findViewById. findViewById looks for a view with the id provided in the current layout inflated.
Just in case the accepted answer is not working. Since i have two different layouts for phone and tablet but only one have WebView which cause findViewById() get null.
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.
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.
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?