Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

PRACTICAL 18

1. List different methods used in Intent.

Methods used in Intent are as follows:

This is to launch a new activity or get an


Context.startActivity() existing activity to be action.

This is to start a new service or deliver


Context.startService() instructions for an existing service.

This is to deliver the message to broadcast


Context.sendBroadcast() receivers.

2. Write an intent to display the phone dialer with the given number filled in.

Intent intent = new Intent(Intent.ACTION_DIAL);


intent.setData(Uri.parse("tel :" + "+919359513084"));
startActivity(intent);

• Exercise:

1. Write a program to create a text field and a button “Navigate”. When


you enter “www.google.com” and press navigate button it should open
google page.

activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">

<EditText
android:id="@+id/url"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter URL"
android:textSize="20dp"
android:ems="10"
android:layout_marginTop="30dp"
android:layout_marginLeft="50dp"
/>

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Navigate"
android:layout_marginTop="30dp"
android:layout_marginLeft="50dp"/>

</LinearLayout>

MainActivity.java:

package com.example.pr_18;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

EditText url;
Button btn1;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
url=findViewById(R.id.url);
btn1=findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = url.getText().toString();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(text));
startActivity(intent);
}
});
}
}

2. Write a program to create button “Start Dialer”. When u click on this


button it should open the phone dialer.

activity_main.xml:

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


<LinearLayout 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"
android:orientation="vertical">

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Dialer"
android:layout_marginTop="30dp"
android:layout_marginLeft="50dp"/>

</LinearLayout>

MainActivity.java:
package com.example.pr_18;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

Button btn1;

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

btn1 = findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:" + "+919359513084"));
startActivity(intent);
}
});
}
}

First Screen
3. Write a program to create two screens. First screen will take one number
input from user. After click on Factorial button, second screen will open
and it should display factorial of the same number. Also specify which
type of intent you will use in this case.

We use explicit intent in this case.

activity_main.xml

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


<LinearLayout 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/num1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter any number"
android:inputType="number"
android:layout_marginTop="30dp"
android:layout_marginLeft="30dp"/>

<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate Factorial"
android:layout_marginTop="30dp"
android:layout_marginLeft="30dp"/>

</LinearLayout>

MainActivity.java:
package com.example.pr_16;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.*;

public class MainActivity extends AppCompatActivity {

EditText num1;
Button btn1;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1 = findViewById(R.id.num1);
btn1 = findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),MainActivity2.class);
i.putExtra("number",num1.getText().toString());
startActivity(i);
}
});

}
}

activity_main2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity2">

<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Factorial of a number is : "
android:textSize="20dp"
android:layout_marginTop="30dp"
android:layout_marginLeft="30dp"/>

</LinearLayout>

MainActivity2.java:

package com.example.pr_16;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity2 extends AppCompatActivity {

TextView result;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
result = findViewById(R.id.result);
Bundle b = getIntent().getExtras();
int no = Integer.parseInt(b.getString("number"));
int fact = 1;
for(int i=no;i>0;i--)
{
fact*=i;
}
result.setText("Factorial of "+ no + " is : " + fact);
}
}

You might also like