Why does my Browser open another Browser? - android

I am creating an Android application to work as a simple browser. The user enters a web address and then clicks a button. After this, the application should the website in the WebView. However, after the button is clicked, a notification pops up asking me to choose a browser to complete my action. [The options in the pop up are my default browser, mozilla, etc.]
However, I do not want to use any of them. What should I do ?
This is my .java file:
package com.example.all_in_one;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
public class SimpleBrowser extends Activity implements OnClickListener{
WebView myBrowser;
EditText URLaddress;
Button Go;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_browser);
myBrowser = (WebView) findViewById(R.id.WV);
URLaddress = (EditText) findViewById(R.id.webaddress);
Go = (Button) findViewById(R.id.go);
Go.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.go:
String address = URLaddress.getText().toString();
myBrowser.loadUrl("http://" + address);
break;
}
}
}
This is my .xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/LL1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="5" >
<EditText
android:id="#+id/webaddress"
android:inputType="textNoSuggestions"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<Button
android:id="#+id/go"
android:text="Go"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="4"
/>
</LinearLayout>
<WebView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/WV"
android:layout_below="#+id/LL1" />
</RelativeLayout>
I have added the Internet permission in my Manifest file.
Please help. Thank you in advance.

You have to pass the WebViewClient class object to browser.setWebViewClient(new BrowserClient()) to open the links in the webview.
private class BrowserClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d(tag, "shouldOverrideUrlLoading");
view.loadUrl(url);
return true;
}
}
and in your onCreate() call like this
myBrowser.setWebViewClient(new BrowserClient());

Related

How can I add Share Button to my android app

How can I add Share Button to my android app, I tried several solutions here in and nothing seems to work.
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.package.name.MainActivity"
android:weightSum="1">
<include
android:id="#+id/toolbar"
layout="#layout/toll_bar"
/>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="368dp"
android:layout_height="495dp"
tools:context=".MainActivity"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp">
tools:ignore="MergeRootFrame">
<WebView
android:id="#+id/activity_main_webview"
android:layout_width="383dp"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar"
android:layout_marginTop="30dp"
/>
</FrameLayout>
</LinearLayout>
Toll_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:elevation="4dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:text="#string/app_name"
android:textAlignment="viewStart"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textColorHighlight="#000000"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/about"
android:layout_width="45dp"
android:layout_height="45dp"
android:text="About"
android:background="#drawable/icon"
android:layout_marginLeft="127dp"
/>
<Button
android:id="#+id/share"
android:layout_width="45dp"
android:layout_height="45dp"
android:text="share"
android:background="#drawable/share"
android:layout_marginLeft="10dp"
/>
<Button
android:id="#+id/rate"
android:layout_width="45dp"
android:layout_height="45dp"
android:text="Rate Us"
android:background="#drawable/rate"
android:layout_marginLeft="10dp"
/>
</android.support.v7.widget.Toolbar>
Main_Activity.xml
package com.package.name;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ShareActionProvider;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar)findViewById(R.id.toolbar);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
mWebView.loadUrl("http://beta.html5test.com/");
mWebView.loadUrl("file:///android_asset/Html36289/index.html");
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
// Stop local links and redirects from opening in browser instead of WebView
mWebView.setWebViewClient(new MyAppWebViewClient());
mWebView .getSettings().setJavaScriptEnabled(true);
Button btnRate = (Button) findViewById(R.id.rate);
btnRate.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
onClickRateThisApp(v);
}});
}
private boolean isActivityStarted(Intent aIntent) {
try
{
startActivity(aIntent);
return true;
}
catch (ActivityNotFoundException e)
{
return false;
}
}
public void onClickRateThisApp(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=com.supercell.hayday"));
if (!isActivityStarted(intent)) {
intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.package.name"));
if (!isActivityStarted(intent)) {
Toast.makeText(this, "Could not open Android market, please check if the market app installed or not. Try again later", Toast.LENGTH_SHORT).show();
}
}
}
}
Thank you for your answer, i tried different solution and it worked.
youtube.com/watch?v=U39RsxrWhIA
In my opinion, toll_bar.xml is unnecessary.
First, in activity_main.xml, please change
<include
android:id="#+id/toolbar"
layout="#layout/toll_bar" />
to
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize"
android:background="#color/colorPrimary"/>
Second, create a menu file named toolbar.xml place it into res/menu directory.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/menu_about"
android:enabled="true"
android:icon="#drawable/icon"
android:title="about"
app:showAsAction="always" />
<item
android:id="#+id/menu_share"
android:enabled="true"
android:icon="#drawable/share"
android:title="share"
app:showAsAction="always" />
<item
android:id="#+id/menu_rate"
android:enabled="true"
android:icon="#drawable/rate"
android:title="rate us"
app:showAsAction="always" />
</menu>
Third, here is the new MainActivity.java
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
#Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
mWebView.loadUrl("http://beta.html5test.com/");
mWebView.loadUrl("file:///android_asset/Html36289/index.html");
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
// Stop local links and redirects from opening in browser instead of WebView
mWebView.setWebViewClient(new MyAppWebViewClient());
mWebView.getSettings().setJavaScriptEnabled(true);
}
private boolean isActivityStarted(Intent aIntent) {
try {
startActivity(aIntent);
return true;
} catch (ActivityNotFoundException e) {
return false;
}
}
public void onClickRateThisApp() {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=com.supercell.hayday"));
if (!isActivityStarted(intent)) {
intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.package.name"));
if (!isActivityStarted(intent)) {
Toast.makeText(this, "Could not open Android market, please check if the market app installed or not. Try again later", Toast.LENGTH_SHORT).show();
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_about:
//click the about button
break;
case R.id.menu_share:
//click the share button
break;
case R.id.menu_rate:
onClickRateThisApp();// param view is unnecessary
break;
}
return true;
}
}
I hope that will help you. If so, please adopt my answer.:-D Have a nice day.
You can add this code this would help you.
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(Intent.createChooser(share, ""));
This will open the multiple sharing option of app.
This help me to share with another one .

Android Google Map API V2 - Buttons above fragment

I have managed to get a map to show on the screen. I am trying to create buttons above the map and then add actions to this. E.g. the button will change the location of the map. I am basically trying to adapt my code to this: https://www.youtube.com/watch?v=awX5T-EwLPc. However, I am using "extends Fragment Activity" which messes everything up.
My problem: I cannot create a button above the map, it is placed inside the map. I am trying to create a button above the map, and assign code so I can move to a location on the map etc. Here is my code:
MainActivity.java
package com.example.theapp;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements OnClickListener
{
Button button;
TextView text;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button1);
text=(TextView)findViewById(R.id.textView1);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
text.setText("The button worked!");
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/map"
android:layout_alignTop="#+id/map"
android:text="Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_alignRight="#+id/map"
android:layout_marginRight="39dp"
android:text="TextView" />
</RelativeLayout>
From the code, my app displays a button on the map and a text view. When you click the button, its meant to change the text, but it doesn't do anything. I have researched tutorials and by the looks of it, I will have to create multiple fragments?
Question: How would I be able to create a button on that page, that will then pin point the user on the map?
Thank you in advance.
You are not setting any functionality to the Button... You should use
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
text.setText("that's how you do it!");
}
});
You need not to worry about "FragmentActivity" until and unless you use fragment class.
Just use this as normal Activity. it will work well.
package com.example.theapp;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements OnClickListener
{
Button button;
TextView text;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button1);
button.setOnClickListener(this);
text=(TextView)findViewById(R.id.textView1);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
text.setText("The button worked!");
}
}

youtube output on my app without using a standard browser with webview

I want to put the webversion of youtube in my application where you can do everything(like watch and search videos,..) without using a standard browser. I have written this code but every time pop ups which browser I want to use (like Google chrome,..)but I want to let it display on my appscreen and I have set the permission of internet already.
here the code:
youtube.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
youtube.java:
package com.example.listentomusic;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class Youtube extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.youtube);
WebView wv = (WebView)findViewById(R.id.webView1);
wv.loadUrl("http://www.youtube.com");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
You need to create your CustomWebViewClient & set it to your webView.
Like this -
private class CustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
Now set this CustomeWebViewClient to your webView-
WV.setWebViewClient(new CustomWebViewClient());
Also main(String[] args) is of no use, remove it.

display webview below progressbar in framelayout

I want to display webview below progressbar. How to do it ?
My code is as below :
<FrameLayout
android:id="#+id/flWeb"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<ProgressBar
android:id="#+id/pbWebsite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top" />
<WebView
android:id="#+id/wvWebsite"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
I used this and solved my problem...
<FrameLayout
android:id="#+id/flWebpre"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView
android:id="#+id/wvWebsite"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<ProgressBar
android:id="#+id/pbWebsite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
android:layout_gravity="center_horizontal" />
</FrameLayout>
This is the code i have used to show the progress:
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
WebView mWvUrl;
#Override
protected void onCreate(Bundle savedInstanceState) {
final Activity activity=this;
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
activity.setProgressBarIndeterminateVisibility(false);
mWvUrl = (WebView) findViewById(R.id.wv_url);
mWvUrl.getSettings().setJavaScriptEnabled(true);
Button btnLoad = (Button) findViewById(R.id.btn_load);
btnLoad.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EditText etUrl = (EditText) findViewById(R.id.et_url);
mWvUrl.loadUrl(etUrl.getText().toString());
activity.setProgressBarIndeterminateVisibility(true);
}
});
mWvUrl.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
/** This prevents the loading of pages in system browser */
return false;
}
/** Callback method, executed when the page is completely loaded */
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
Toast.makeText(getBaseContext(),
"Page loaded Completely",
Toast.LENGTH_SHORT).show();
/** Hiding Indeterminate Progress Bar in the title bar*/
activity.setProgressBarIndeterminateVisibility(false);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
You need another Layout. The FrameLayout is not the better way to do alignement between components. If you are using a FrameLayout, components will be superimposed. I suggest you to use a LinearLayout vertical or a RelativeLayout.
Try this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ProgressBar
android:id="#+id/pbWebsite"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|top" />
<WebView
android:id="#+id/wvWebsite"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

Input modify URL Webview?

users have an activity with an edittext and a button.
in the edittext they have to insert their login ID. and press go to display a other activity with webview loading the right site.
an example. this is the link i want users to visit . www.example.com/time/id=1/type=student
if sombody enters a 2 in the edittext it wil open the times for student id 2 and so on.
this is what i have.
LOGIN
package com.beter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
import android.content.Intent;
public class LoginActivity extends Activity {
Button go_btn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
go_btn = (Button) findViewById(R.id.go_btn);
go_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(LoginActivity.this, WebActivity.class);
LoginActivity.this.startActivity(myIntent);
}
});
}
and the layout
<RelativeLayout 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" >
<EditText
android:id="#+id/idfield"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="#string/id"
android:inputType="number" >
<requestFocus />
</EditText>
<Button
android:id="#+id/go_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/idfield"
android:text="#string/login" />
The Webview Activity
package com.beter;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class WebActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
WebView webview = new WebView(this);
setContentView(webview);
webview.loadUrl("http://roosters.gepro-osi.nl/roosters/rooster.php?wijzigingen=1&leerling=116277&type=Leerlingrooster&afdeling=12-13_OVERIG&school=905");
}
}
and again the layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView
android:id="#+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
How do i make the user input. display the good webview. ?
First of all get a reference to your edittext in your Login Activity as
go_btn = (Button) findViewById(R.id.go_btn);
ed_txt = (EditText)findViewById(R.id.youredittextid);
The change your onClick to
public void onClick(View v) {
Intent myIntent = new Intent(LoginActivity.this, WebActivity.class);
myIntent.putExtra("id", ed_txt.getText());
LoginActivity.this.startActivity(myIntent);
}
Then retrive this id in your WebActivity onCreate as
String id = getIntent().getExtras().getString("id");
Then your url will be
String myURL = "www.example.com/time/id="+id+"/type=student";

Categories

Resources