skip to Main Content

Please read my question first

I want to open a URL in my app not in phone’s browser.

You can see in Facebook or Twitter that how they open a Link in there own App.

This will redirect to my phone’s browser

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);

but I want to open URL like Facebook App.

Is there any specific API for this kind of view.

5

Answers


  1. You can use webview in a new activity to open url. https://developer.android.com/guide/webapps/webview.html

    Login or Signup to reply.
  2. I think you want to use Chrome Custom Tabs, check the link!

    Here the basic example from the website

    // Use a CustomTabsIntent.Builder to configure CustomTabsIntent.
    // Once ready, call CustomTabsIntent.Builder.build() to create a CustomTabsIntent
    // and launch the desired Url with CustomTabsIntent.launchUrl()
    
    String url = ¨https://paul.kinlan.me/¨;
    CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    CustomTabsIntent customTabsIntent = builder.build();
    customTabsIntent.launchUrl(this, Uri.parse(url));
    
    Login or Signup to reply.
  3. Following code will help you to open the url in your app:

    import android.app.Activity;
    import android.os.Bundle;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.Toast;
    
    public class AppWebViewActivity extends Activity {
    
        private WebView mWebview;
        private String mWebViewUrl;
        @Override
        public void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
    
            mWebview  = new WebView(this);
            mWebViewUrl= getIntent().getStringExtra("web_view_url");
    
            mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
    
            final Activity activity = this;
    
            mWebview.setWebViewClient(new WebViewClient() {
                public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
                }
            });
    
            mWebview .loadUrl(mWebViewUrl);
            setContentView(mWebview );
    
        }
    
    }
    

    Define above activity in manifest like this:

     <activity
            android:name="<package-name>.AppWebViewActivity "
            android:screenOrientation="portrait" >
        </activity>
    

    And Add internet permission as well:

    <uses-permission android:name="android.permission.INTERNET" />
    

    And to call this activity just follow below code:

    Intent intent = new Intent(getBaseContext(), AppWebViewActivity .class);
    intent.putExtra("web_view_url", "your-complete-url");
    startActivity(intent)
    

    You can have custom ui in that case you have to define webview in xml and take the reference of layout in your webview activity.

    Happy Coding!!!!

    Login or Signup to reply.
  4. you can open url via webview within your app steps are below:

    xml code below for webview;

    <?xml version="1.0" encoding="utf-8"?>
    
    <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.whiskey.servicedog.DocumentPlay"
        tools:showIn="@layout/activity_document_play">
    
        <WebView
            android:id="@+id/webView"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentTop="true">
    
        </WebView>
    
    </RelativeLayout>
    

    next code for java file below:

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_document_play);
    
            webView = (WebView) findViewById(R.id.webView);
    
            webView.getSettings().setJavaScriptEnabled(true);
            webView.getSettings().setBuiltInZoomControls(true);
            webView.loadUrl(urlpath);
        }
    

    hope this helps you out

    Login or Signup to reply.
  5. add a webview activity/fragment, and show website in webview.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search