Friday, 9 January 2015

Implicit intent Example

Implicit Intent is navigating our activity to internal program or an activity




Step 1: create new project
Step 2: write the following code in activity_main.xml

activity_main.xml
<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"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/google"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/main_button_google" />


</RelativeLayout>

Step 3: add the following code in strings.xml

strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Implicit Intent Example</string>
    <string name="main_button_google">google</string>

</resources>

Step 4: add the following code in MainActivity.java

MainActivity.java
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;

public class MainActivity extends Activity {

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

              Button google = (Button) findViewById(R.id.google);
              google.setOnClickListener(new OnClickListener() {

                     @Override
                     public void onClick(View v) {
                           Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri
                                         .parse("http://www.google.com"));
                           startActivity(webIntent);
                     }
              });
       }

}

No comments:

Post a Comment