I'm trying to add this WebView to onClick function of a button but, WebView is always there before even click the button. I want WebView appear only when clicked . can anyone tell which code I need to put to get the WebView work when the button clicked .
<WebView
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center" />
xml code
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
protected Button mButton;
private WebView webview;
ViewPager mHolder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app_home);
Button mButton = (Button) findViewById(R.id.infobutton);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
webview = (WebView) findViewById(R.id.webView);
webview.getSettings().setJavaScriptEnabled(true);
String chtml = "<html>"+
"<style> div{height:50%; width:70%; background-color:#aaa;} body div:nth-child(2){height:20%; margin:20px; width:30%; background-color:#222;} h1{ color:red; font-size:24px;}</style>" +
"<body><h1>hello, webview</h1> <div></div> <div></div> </body></html>";
webview.loadData(chtml, "text/html", "UTF-8");
}
});
java code