0

I have two activities "Main" (with a button in it) & "Second", and I want when I click on the button in Main activity then Second activity should be launched but it should behave like a browser. Means browser should be opened when Second activity is launched.

My code is as follow:
In manifest file I have written

    <activity android:name=".Second">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.BROWSABLE"/>
        </intent-filter>
    </activity>

In Main.java file I have written event handler like this

Button b = (Button) findViewById(R.id.button1);

b.setOnClickListener(new OnClickListener() {

         @Override
         public void onClick(View v) {
         // TODO Auto-generated method stub

         Intent intent = new Intent(Main.this, Second.class);
         startActivity(intent);
    }
});

First of all, is it possible what I want and if yes then am I doing it in right way? And really sorry if I my question is stupid, because I am new to Android

swdeveloper
  • 874
  • 1
  • 10
  • 31

4 Answers4

1

write this in your Second activity. As your second activity will call it will open a browser.

You can also use this in onClick() also.

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")));
MAC
  • 15,363
  • 8
  • 51
  • 92
1

When I started learning about android and its intents I found it very confusing. But category BROWSABLE in the Manifest does the following.

The target activity can be safely invoked by the browser to display data referenced by a link — for example, an image or an e-mail message.

Read more on: http://developer.android.com/guide/components/intents-filters.html

The two other answers opens the standard web browser and goes to the address specified. If you want a custom browser make the second activity like a web view like this example:

http://www.mkyong.com/android/android-webview-example/

Good luck!

just_user
  • 10,086
  • 14
  • 78
  • 119
1
    public class WebDialog extends Dialog
{

    static final int                      BLUE                  = 0xFF6D84B4;
    static final float[]                  DIMENSIONS_DIFF_LANDSCAPE =
                                                                    { 20, 60 };
    static final float[]                  DIMENSIONS_DIFF_PORTRAIT  =
                                                                    { 40, 60 };
    static final FrameLayout.LayoutParams   FILL                    = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
    static final int                      MARGIN                    = 4;
    static final int                      PADDING                   = 2;
    static final String                   DISPLAY_STRING            = "touch";

    private String                        mUrl;
//  private DialogListener                mListener;
    private ProgressDialog                mSpinner;
    private WebView                       mWebView;
    private LinearLayout                  mContent;
    private TextView                      mTitle;

    public WebDialog(Context context, String url)
    {
        super(context);
        mUrl = url;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        mSpinner = new ProgressDialog(getContext());
        mSpinner.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mSpinner.setMessage("Loading...");

        mContent = new LinearLayout(getContext());
        mContent.setOrientation(LinearLayout.VERTICAL);
        setUpTitle();
        setUpWebView();
        Display display = getWindow().getWindowManager().getDefaultDisplay();
        final float scale = getContext().getResources().getDisplayMetrics().density;
        int orientation = getContext().getResources().getConfiguration().orientation;
        float[] dimensions = (orientation == Configuration.ORIENTATION_LANDSCAPE) ? DIMENSIONS_DIFF_LANDSCAPE : DIMENSIONS_DIFF_PORTRAIT;
        addContentView(mContent, new LinearLayout.LayoutParams(display.getWidth() - ((int) (dimensions[0] * scale + 0.5f)), display.getHeight() - ((int) (dimensions[1] * scale + 0.5f))));
    }

    private void setUpTitle()
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        Drawable icon = getContext().getResources().getDrawable(R.drawable.ic_launcher);
        mTitle = new TextView(getContext());
        mTitle.setText("Website");
        mTitle.setTextColor(Color.WHITE);
        mTitle.setTypeface(Typeface.DEFAULT_BOLD);
        mTitle.setBackgroundColor(BLUE);
        mTitle.setPadding(MARGIN + PADDING, MARGIN, MARGIN, MARGIN);
//      mTitle.setCompoundDrawablePadding(MARGIN + PADDING);
//      mTitle.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
        mContent.addView(mTitle);
    }

    private void setUpWebView()
    {
        mWebView = new WebView(getContext());
        mWebView.setVerticalScrollBarEnabled(false);
        mWebView.setHorizontalScrollBarEnabled(false);
        mWebView.setWebViewClient(new WebDialog.DialogWebViewClient());
        mWebView.getSettings().setJavaScriptEnabled(true);

        System.out.println(" mURL = "+mUrl);

        mWebView.loadUrl(mUrl);
        mWebView.setLayoutParams(FILL);
        mContent.addView(mWebView);
    }

    private class DialogWebViewClient extends WebViewClient
    {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            view.loadUrl(url);

            return true;
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
        {
            super.onReceivedError(view, errorCode, description, failingUrl);
            WebDialog.this.dismiss();
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon)
        {
            super.onPageStarted(view, url, favicon);
            mSpinner.show();
        }

        @Override
        public void onPageFinished(WebView view, String url)
        {
            super.onPageFinished(view, url);
            String title = mWebView.getTitle();
            if (title != null && title.length() > 0)
            {
                mTitle.setText(title);
            }
            mSpinner.dismiss();
        }

    }
}
Yash
  • 1,721
  • 13
  • 14
0

Seems like you are trying to open a browser to a specific page when the button is clicked on the Main acitvity.

If that is the case you don't need the Second activity, you just need to do something like this:

Button b = (Button) findViewById(R.id.button1);

b.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
        startActivity(i);
    }
}
Costi Muraru
  • 2,007
  • 17
  • 25