Professional Documents
Culture Documents
Mad 18 Intent
Mad 18 Intent
2. Write an intent to display the phone dialer with the given number filled in.
• Exercise:
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;
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);
}
});
}
}
activity_main.xml:
<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;
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.
activity_main.xml
<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.*;
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;
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);
}
}