Translate

Wednesday, 16 March 2016

Android Implicit Intent Example

Let's see the simple example of implicit intent that displays a web page.

activity_main.xml


File: activity_main.xml
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.     <EditText  
  7.         android:id="@+id/editText1"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_alignParentTop="true"  
  11.         android:layout_centerHorizontal="true"  
  12.         android:layout_marginTop="44dp"  
  13.         android:ems="10" />  
  14.     <Button  
  15.         android:id="@+id/button1"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:layout_below="@+id/editText1"  
  19.         android:layout_centerHorizontal="true"  
  20.         android:layout_marginTop="54dp"  
  21.         android:text="Visit" />  
  22. </RelativeLayout>  

Activity class


File: MainActivity.java
  1. package org.sssit.implicitintent;  
  2. import android.net.Uri;  
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.content.Intent;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.EditText;  
  10. public class MainActivity extends Activity {  
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.activity_main);  
  15.         final EditText editText1=(EditText)findViewById(R.id.editText1);  
  16.         Button button1=(Button)findViewById(R.id.button1);  
  17.         button1.setOnClickListener(new OnClickListener() {  
  18.             @Override  
  19.             public void onClick(View arg0) {  
  20.                 String url=editText1.getText().toString();  
  21.                 Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse(url));  
  22.                 startActivity(intent);  
  23.             }  
  24.         });  
  25.     }  
  26. }  


Output:

android implicit intent example output 1 android implicit intent example output 2 android implicit intent example output 3

No comments:

Post a Comment