Monday, June 17, 2013

Web browser by Action View

Hi Guys!

Today I am going to share the code for open web browser by action view in android.
In this article we will create an application which will open a user input website in a web browser. In order to open a web browser, we will create an intent object with ACTION_VIEW as action and data with �http� in the url. Since http request is invoked by the external browser, no explicit permission is needed for this application.

activity_main.xml






MainActivity.java

package com.sunil.browserview;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity implements OnClickListener{

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btn = (Button) findViewById(R.id.btn_browse);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
EditText txt = (EditText) findViewById(R.id.te_url);

Intent intent = new Intent("android.intent.action.VIEW");

Uri data = Uri.parse("http://"+txt.getText().toString());

intent.setData(data);

startActivity(intent);

}
}


Rocky

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation.

0 comments:

Post a Comment

 

Copyright @ 2013 Android Developers Tipss.