3 Intent And Intent Filters




Types of Android Intents
There are two types of intents in android: implicit and explicit.

1)  Implicit Intent
Implicit Intent doesn't specify the component. In such case, intent provides information of available components provided by the system that is to be invoked.
For example, you may write the following code to view the webpage.
         1.    Intent intent=new Intent(Intent.ACTION_VIEW);
         2.    intent.setData(Uri.parse("https://developer.android.com/"));
         3.    startActivity(intent);

2)  Explicit Intent
Explicit Intent specifies the component. In such case, intent provides the external class to be invoked.
         1.    Intent i = new Intent(getApplicationContext(), ActivityTwo.class);
         2.    startActivity(i);

Algorithm:
Step 1) Create a new Android Studio Project named as Implicit_Intent. Step 2)Goto Project Structure and open the activity_main.xml file.
Step 3) Add one PlainText (EditText), Button tag or drag and drop from the palette. Step 4)Create two reference variables for EditText and Button classes.
Step 5) Add setOnClickListener method to Button class reference variable and implement it.

button.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v){

}
});


Step 5) Run the application from the menu bar select Run and Click on Run App option.


Program: 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=".MainActivity">

<EditText android:id="@+id/editText"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:ems="10" android:inputType="textPersonName" android:text="Name"
app:layout_constraintBottom_toTopOf="@+id/button" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />

<Button android:id="@+id/button"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="228dp" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:text="Button" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>


MainActivity.java

package com.example.implicit_intent;

import android.content.Intent; import android.net.Uri;
import android.support.v7.app.AppCompatActivity; import android.os.Bundle;
import android.view.View; import android.widget.Button; import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

Button button; EditText editText;


@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
button = findViewById(R.id.button); editText = findViewById(R.id.editText);

button.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
String url=editText.getText().toString(); Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
});
}

}

Output:




Comments

Popular posts from this blog

20 Simple Content Provider Example

14 Popup Menu

19 SQLIte SImple Database Example