Reputation: 35
I found this code in another post and I was wondering how I could make this url open in webView rather than the default Android browser.
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class LinkActivity extends Activity {
EditText txtLink;
Button btnOpenLink;
String defaultLink;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_link);
defaultLink = "http://www.google.com";
txtLink = (EditText) findViewById(R.id.editTextLink);
btnOpenLink = (Button) findViewById(R.id.buttonOpenLink);
btnOpenLink.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String page = txtLink.getText().toString();
if(!TextUtils.isEmpty(page)){
Uri uri = Uri.parse(defaultLink+"/"+page);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}else{
Toast.makeText(LinkActivity.this, "Please enter page on editText!!", Toast.LENGTH_LONG).show();
}
}
});
}
}
I would like this url to open in the app itself and not open the browser to go to the link. Any help would definitely be appreciated. Thank you.
Upvotes: 1
Views: 1794
Reputation: 2982
In your activity_link
, add a WebView object like below,
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
and your LinkActivity should be look like below:
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class LinkActivity extends Activity {
EditText txtLink;
Button btnOpenLink;
String defaultLink;
WebView mWebView;
String mUrl = "";
SharedPreferences mPrefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_link);
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
defaultLink = "http://www.google.com";
mWebView = (WebView) findViewById(R.id.web_view);
txtLink = (EditText) findViewById(R.id.editTextLink);
btnOpenLink = (Button) findViewById(R.id.buttonOpenLink);
btnOpenLink.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String page = txtLink.getText().toString();
if(!TextUtils.isEmpty(page)){
mUrl = defaultLink+"/"+page;
mWebView.loadUrl(url);
}else{
Toast.makeText(LinkActivity.this, "Please enter page on editText!!", Toast.LENGTH_LONG).show();
}
}
});
}
// This method will reloads the last opened page in the web view..!
@Override
protected void onResume()
{
super.onResume();
String url = mPrefs.getString("url", "");
if (!url.equalsignorecase(""))
{
mUrl = url;
txtLink.setText(url);
mWebView.loadUrl(url);
}
}
// And this will save the last loaded link in the Shared Preferences(Local Storage)
@Override
protected void onPause()
{
super.onPause();
mPrefs.edit().putString("url", mUrl).commit();
}
}
and for more info about the webview, should refer the below url: http://developer.android.com/reference/android/webkit/WebView.html
To learn more about shared preferences, you can go on the below url:
http://developer.android.com/reference/android/content/SharedPreferences.html
Upvotes: 1