0% found this document useful (0 votes)
22 views

Practical No 21

The document provides code examples for three Android applications: one that opens a web page when a URL is entered, another that opens the phone dialer, and a third that calculates the factorial of a number input by the user. Each application includes XML layout files and corresponding Java code. The document also specifies the use of explicit intents for navigating between activities in the factorial example.

Uploaded by

yash gavali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Practical No 21

The document provides code examples for three Android applications: one that opens a web page when a URL is entered, another that opens the phone dialer, and a third that calculates the factorial of a number input by the user. Each application includes XML layout files and corresponding Java code. The document also specifies the use of explicit intents for navigating between activities in the factorial example.

Uploaded by

yash gavali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Practical No 21

X. 1) write a simple and small program to create a edittext and a button


"Navigate" when you enter "www.google.com" and press the button it
should open a google page in android
XML FIle:
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<EditText
android:id="@+id/e1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Website" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Navigate"
android:textSize="20dp"
android:onClick="btn1"/>
</LinearLayout>

Java File:

package com.yash.ptr18;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
EditText e1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
e1=findViewById(R.id.e1);
}
public void btn1(View view) {
String url = e1.getText().toString();
if (url.equals("www.google.com")) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://" + url));
startActivity(intent);
} else {
Toast.makeText(MainActivity.this, "Enter a valid URL", Toast.LENGTH_SHORT).show();
}
}
}

OUTPUT:

2) write a program to create button "Strat dialer" when you click on this button
it should open phone dialer

XML Code:

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Dialer"
android:padding="10dp"
android:onClick="btn1"/>
</LinearLayout>

JAVA Code:
package com.yash.ptr182;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
}
public void btn1(View view) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:"));
startActivity(intent);
}
}

OUTPUT:
3) Write a simple and very small program to create two screens. First screen will
take one number input from user. After clicking 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.

XML Code:

i. Main Activity:

<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<EditText
android:id="@+id/e1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enter number" />
<Button
android:onClick="btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Factorial"
android:textSize="20dp"/>
</LinearLayout>

ii. Fact Activity:

<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".fact">
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp" />
</LinearLayout>

Java Code:
i. Main Activity:

package com.yash.ptr183;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
EditText e1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
e1=findViewById(R.id.e1);
}
public void btn1(View view) {
String a=e1.getText().toString();
int num=Integer.parseInt(a);
if (a.isEmpty()){
Toast.makeText(this, "Fill the field", Toast.LENGTH_SHORT).show();
} else{
Intent intent=new Intent(this, fact.class);
intent.putExtra("num",num);
startActivity(intent);
}
}
}

ii. Fact:

package com.yash.ptr183;
import android.os.Bundle;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
public class fact extends AppCompatActivity {
TextView txt1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_fact);
txt1=findViewById(R.id.txt1);
int num1= getIntent().getIntExtra("num",0);
long fact1= fact(num1);
txt1.setText("Factorial of "+num1+" is: "+fact1);
}
public long fact(int n){
long fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
}

OUTPUT:

You might also like