How To Open Browser In Android



This is a sample program to show the use of Intents. Intents are a powerful concept as they allow the creation of loosely coupled applications. Intents can be used to communicate between any installed application components on the device.
Underlying Algorithm:
Basic description of algorithm in step by step form:
1.) Create a project IntentBrowser
2.) Put the following code in layout/main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"

   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:layout_gravity="center"
   >
<Button android:id="@+id/Button01" android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="Browser"android:onClick="openBrowser"></Button>
</LinearLayout>
3.) We will start a new Intent with the method :
Intent i = new Intent("android.intent.action.VIEW", Uri.parse("http://www.google.com"));
4.) As your activity gets called with intent you can get the data from the intent and display it in your application.
5.) In this example click event of the button will open Browser.
6.) Run the application.
Steps to Create:
1.) Open Eclipse. Use the New Project Wizard and select Android Project Give the respective project name i.e. IntentBrowser. Enter following information:
Project name: IntentBrowser
Build Target: Android 2.3
Application name: IntentBrowser
Package name: com.sample.IntentBrowser
Create Activity: IntentBrowser
On Clicking Finish IntentBrowser code structure is generated with the necessary Android Packages being imported along with IntentBrowser.java. IntentBrowser class will look like following:
package com.sample.IntentBrowser;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
public class IntentBrowser extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
        }
        public void openBrowser(View view) {
                Intent i = new Intent("android.intent.action.VIEW", Uri.parse("http://www.google.com"));
                startActivity(i);
        }
}
Output –The final output:
Read More..

How to get started on Android with AdMob

AdMob: Getting Started on Android

Read More..

Java code to display the solution of Tower of Hanoi using recursion

/*
Program to display the solution of Tower of Hanoi using recursion.
*/
import java.io.*;
class towerOfHanoi
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
towerOfHanoi call = new towerOfHanoi();
System.out.print("Enter number of discs : ");
int n = Integer.parseInt(br.readLine());
System.out.print("
Label of 1st stick : ");
String a = br.readLine();
System.out.print("Label of 2nd stick : ");
String b = br.readLine();
System.out.print("Label of 3rd stick : ");
String c = br.readLine();
call.hanoi(n,a,b,c);
}
void hanoi (int n, String a, String b, String c)
{
if(n==1)
System.out.println("Move disc from " +a +" to " +c);
else
{
hanoi(n-1,a,c,b);
System.out.println("Move disc from " +a +" to " +c);
hanoi(n-1,b,a,c);
}
}
}


/**
* ALGORITHM :
* ---------
* 1. Start
* 2. Accept the number of discs from the user.
* 3. Ask for the labels of sticks A, B & and C.
* 4. Move n−1 discs from A to B. This leaves disc #n alone on stick A.
* 5. Move disc #n from A to C.
* 6. Move n−1 discs from B to C so they sit on disc #n.
* 7. Repeat steps 4 to 6 using recursion.
* 8. End
*/

/*
OUTPUT :
------
Enter number of discs : 4
Label of 1st stick : start
Label of 2nd stick : mid
Label of 3rd stick : end

Move disc from start to mid
Move disc from start to end
....
....
....
....

..
.
.

*/



Read More..

Blog Archive

Powered by Blogger.