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

Practical No. 18

The document provides code snippets for creating Android applications that include user interface elements such as text fields and buttons. It describes functionalities like opening a web page with a URL input and dialing a phone number from a user input. Additionally, it includes a feature to calculate the factorial of a number input by the user and display the result on a second screen using intents.

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 DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

Practical No. 18

The document provides code snippets for creating Android applications that include user interface elements such as text fields and buttons. It describes functionalities like opening a web page with a URL input and dialing a phone number from a user input. Additionally, it includes a feature to calculate the factorial of a number input by the user and display the result on a second screen using intents.

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 DOCX, 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 android:dataExtractionRules="@xml/data_extr
AppCompatActivity { action_rules"
android:fullBackupContent="@xml/backup_ru
TextView resultText; les"
android:icon="@mipmap/ic_launcher"
@Override android:label="@string/app_name"
protected void onCreate(Bundle android:roundIcon="@mipmap/ic_launcher_ro
savedInstanceState) { und"
super.onCreate(savedInstanceState); android:supportsRtl="true"
setContentView(R.layout.activity_result); android:theme="@style/Theme.PracticalNo18
_2"
resultText = findViewById(R.id.resultText); tools:targetApi="31">
<activity
// Retrieve the number passed from android:name=".MainActivity"
MainActivity android:exported="true">
int number = getIntent().getIntExtra("number", <intent-filter>
0); <action
android:name="android.intent.action.MAIN" />
// Calculate factorial <category
long factorial = calculateFactorial(number); android:name="android.intent.category.LAUNCHE
R" />
// Display result </intent-filter>
resultText.setText("Factorial of " + number + " </activity>
is: " + factorial); <activity
} android:name=".ResultActivity"
android:exported="false">
private long calculateFactorial(int n) { </activity>
long result = 1; </application>
for (int i = 1; i <= n; i++) { </manifest>
result *= i;
}
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