Vote count:
0
I have created a simple browser application with a edittext , go button and history button. Please suggest what coding do i need to implement for history button such that when i press it my browsing history gets displayed on the hi.xml layout. Please suggest what coding changes do i need to make in my xml file as well as java file .
MainActivity.java:
package com.example.history;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener{
EditText t;
Button g,h;
WebView w;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
g=(Button) findViewById(R.id.button1);
h=(Button) findViewById(R.id.button2);
t=(EditText) findViewById(R.id.editText1);
w=(WebView) findViewById(R.id.webView1);
h.setOnClickListener(this);
g.setOnClickListener(this);
w.getSettings().setJavaScriptEnabled(true);
w.getSettings().setLoadWithOverviewMode(true);
w.getSettings().setUseWideViewPort(true);
w.setWebViewClient(new Callback());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId())
{
case R.id.button1:
String c;
c=t.getText().toString();
String theWebsite=("http://").concat(c);
t.setText(theWebsite);
w.loadUrl(theWebsite);
InputMethodManager imm=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(w.getWindowToken(),0);
break;
case R.id.button2:
Intent a=new Intent(MainActivity.this,h.class);
startActivity(a);
break;
}
}
}
class Callback extends WebViewClient{
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return (false);
}
}
activity_main.xml:
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="15dp"
android:layout_marginTop="16dp"
android:ems="10" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_marginTop="34dp"
android:layout_toRightOf="@+id/textView1"
android:text="Go" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_centerVertical="true"
android:text="History" />
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
h.java:
package com.example.history;
import android.app.Activity;
import android.os.Bundle;
public class h extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.hi);
}
}
hi.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
asked 23 secs ago
Aucun commentaire:
Enregistrer un commentaire