Home »
Android
Android code for Implicit Intent
In this article, we will learn how to use implicit intent? There two types of code (XML and Java) implicit intent coding is done in Java code, XML code is used for the design of layout.
Submitted by Shamikh Faraz, on January 16, 2018
Consider the given code files for the implicit intent implementation. XML code will be used for the design and implicit intent coding is written in Java code file.
XML file: (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.faraz.implicit_intent.MainActivity">
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"
android:ems="10" />
<Button
android:text="Go To Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:id="@+id/button1" />
</android.support.constraint.ConstraintLayout>
Java file: (MainActivity.java)
package com.example.faraz.implicit_intent;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.net.URL;
public class MainActivity extends Activity {
Button button;
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button1);
EditText editText=(EditText)findViewById(R.id.editText1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url= editText.getText().toString();
Intent i=new Intent(Intent.ACTION_VIEW,Uri.parse(url));
startActivity(i);
}
});
}
}
Most Important Note: If you are going to copy paste this code then, don’t copy package (first line of java code), it will create itself in your android studio when you create your project. It is different for different computer systems. You will have to copy from second line of java code up to last line. If you copy package it will display error.
Output Images