0% found this document useful (0 votes)
7 views6 pages

Practical No. 18

The document outlines the creation of an Android application with multiple functionalities, including a text field and button to navigate to a URL, a button to open the phone dialer, and a factorial calculator across two screens. It provides XML layout files for the user interface and Java code for the main activity and result activity, detailing how to handle user input and display results. Additionally, it includes the AndroidManifest.xml configuration necessary for the app's structure.

Uploaded by

ganeshkumbhar638
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)
7 views6 pages

Practical No. 18

The document outlines the creation of an Android application with multiple functionalities, including a text field and button to navigate to a URL, a button to open the phone dialer, and a factorial calculator across two screens. It provides XML layout files for the user interface and Java code for the main activity and result activity, detailing how to handle user input and display results. Additionally, it includes the AndroidManifest.xml configuration necessary for the app's structure.

Uploaded by

ganeshkumbhar638
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

Q. Write a program to create a text field and a xmlns:app="http://schemas.android.

com/apk/res-
button “Navigate”. When you enter auto"
“www.google.com” and press navigate button it xmlns:tools="http://schemas.android.com/tools"
should open google page. android:layout_width="match_parent"
android:layout_height="match_parent"
acitivity_main.xml
android:orientation="vertical"
<?xml version="1.0" encoding="utf-8"?> android:gravity="center"
<LinearLayout tools:context=".MainActivity">
xmlns:android="http://schemas.android.com/apk/res
/android" <EditText
xmlns:app="http://schemas.android.com/apk/res- android:layout_width="250dp"
auto" android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/etxturl"
android:layout_width="match_parent" android:hint="Enter a URL : "
android:layout_height="match_parent" android:layout_margin="40dp"
android:orientation="vertical" android:textSize="24sp"/>
android:gravity="center"
tools:context=".MainActivity"> <Button
android:layout_width="wrap_content"
<EditText android:layout_height="wrap_content"
android:layout_width="250dp" android:layout_margin="40dp"
android:layout_height="wrap_content" android:text="Send"
android:id="@+id/etxturl" android:onClick="send"
android:hint="Enter a URL : " android:textSize="20sp"
android:layout_margin="40dp" />
android:textSize="24sp"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="40dp"
android:text="Send"
android:onClick="send"
android:textSize="20sp"
/>

</LinearLayout>

MainAcitivity.java

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


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res
/android"
Q. 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"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">

<EditText
android:layout_width="250dp"
android:layout_height="wrap_content"
android:id="@+id/etxturl"
android:hint="Enter a Phone no. : "
android:layout_margin="40dp"
android:textSize="24sp"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="40dp"
android:text="Send"
android:onClick="send"
android:textSize="20sp"
/>

</LinearLayout>

MainActivity.java

package com.example.practicalno18_2;

import
androidx.appcompat.app.AppCompatActivity;

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 etxturl;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etxturl = findViewById(R.id.etxturl);
}
public void send(View view) {
String str = etxturl.getText().toString();
Intent i = new Intent(Intent.ACTION_DIAL);
i.setData(Uri.parse("tel:+91" + str));
startActivity(i);
} Q. 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
<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">

<EditText
android:id="@+id/e1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter a number"
android:layout_marginBottom="20dp"
android:inputType="number" /> Toast.makeText(MainActivity.this,
"Please enter a number",
<Button Toast.LENGTH_SHORT).show();
android:id="@+id/fact" } else {
android:background="#ff00ff" int number =
android:layout_width="match_parent" Integer.parseInt(numberStr);
android:layout_height="wrap_content" Intent intent = new
android:text="Factorial" /> Intent(MainActivity.this, ResultActivity.class);
</LinearLayout> intent.putExtra("number", number); //
Pass the number to the second screen
MainActivity.java startActivity(intent); // Navigate to the
package com.example.practicalno18_2; second screen
}
import android.content.Intent; }
import android.os.Bundle; });
import android.view.View; }
import android.widget.Button; }
import android.widget.EditText;
import android.widget.Toast; activity_result.xml

import <LinearLayout
androidx.appcompat.app.AppCompatActivity; xmlns:android="http://schemas.android.com/apk/res
/android"
public class MainActivity extends android:layout_width="match_parent"
AppCompatActivity { android:layout_height="match_parent"
android:orientation="vertical"
EditText inputNumber; android:padding="16dp">
Button factorialButton;
<TextView
@Override android:id="@+id/resultText"
protected void onCreate(Bundle android:layout_width="match_parent"
savedInstanceState) { android:layout_height="wrap_content"
super.onCreate(savedInstanceState); android:textSize="18sp" />
setContentView(R.layout.activity_main); </LinearLayout>

inputNumber = findViewById(R.id.e1); ResultActivity.java


factorialButton = findViewById(R.id.fact);
package com.example.practicalno18_2;
factorialButton.setOnClickListener(new
View.OnClickListener() { import android.os.Bundle;
@Override import android.widget.TextView;
public void onClick(View v) {
String numberStr = import
inputNumber.getText().toString(); androidx.appcompat.app.AppCompatActivity;
if (numberStr.isEmpty()) {
public class ResultActivity extends
AppCompatActivity { android:dataExtractionRules="@xml/data_extractio
n_rules"
TextView resultText;
android:fullBackupContent="@xml/backup_rules"
@Override android:icon="@mipmap/ic_launcher"
protected void onCreate(Bundle android:label="@string/app_name"
savedInstanceState) {
super.onCreate(savedInstanceState); android:roundIcon="@mipmap/ic_launcher_round"
setContentView(R.layout.activity_result); android:supportsRtl="true"

resultText = findViewById(R.id.resultText); android:theme="@style/Theme.PracticalNo18_2"


tools:targetApi="31">
// Retrieve the number passed from <activity
MainActivity android:name=".MainActivity"
int number = getIntent().getIntExtra("number", android:exported="true">
0); <intent-filter>
<action
// Calculate factorial android:name="android.intent.action.MAIN" />
long factorial = calculateFactorial(number); <category
android:name="android.intent.category.LAUNCHE
// Display result R" />
resultText.setText("Factorial of " + number + " </intent-filter>
is: " + factorial); </activity>
} <activity
android:name=".ResultActivity"
private long calculateFactorial(int n) { android:exported="false">
long result = 1; </activity>
for (int i = 1; i <= n; i++) { </application>
result *= i; </manifest>
}
return result;
}
}

AndroidManifest.xml

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


<manifest
xmlns:android="http://schemas.android.com/apk/res
/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"

You might also like