Reputation: 1
I have an image button that when clicked opens a new activity with a web view in. The web view will display some custom HTML. When I click the button the web view displays nothing.
This is the code for my button view:-
public void GoToContact(View view)
{
WebView webView;
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.myWebView);
webView.getSettings().setJavaScriptEnabled(true);
String customHtml = "<html><body><h1>Hello, WebView</h1></body></html>";
webView.loadData(customHtml, "text/html", "UTF-8");
}
THIS IS MY WEB VIEW LAYOUT:-
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="@+id/GoToHome"
android:onClick="GoToTC"
android:background="@drawable/test"
android:layout_width="360dp"
android:layout_height="60dp"
android:contentDescription="@string/header"/>
<WebView
android:id="@+id/myWebView"
android:layout_width="match_parent"
android:layout_height="400dp" />
</LinearLayout>
Upvotes: 0
Views: 195
Reputation: 1555
try this
public class MainActivity extends Activity {
private Button button;
public void onCreate(Bundle savedInstanceState) {
final Context context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.buttonUrl);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(context, WebViewActivity.class);
startActivity(intent);
}
});
}
}
public class WebViewActivity extends Activity {
private WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://www.google.com");
}
}
Upvotes: 1
Reputation: 12523
First of all declare WebView as class member of your activity:
WebView webView;
public void GoToContact(View view)
{
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.myWebView);
webView.getSettings().setJavaScriptEnabled(true);
String customHtml = "<html><body><h1>Hello, WebView</h1></body></html>";
webView.loadData(customHtml, "text/html", "UTF-8");
}
then try it again... btw please show us your layout.xml and the code of the whole activity class.
Upvotes: 0