0% found this document useful (0 votes)
12 views10 pages

Practical No 18

The document contains multiple Android application examples demonstrating XML layouts and Java code for various functionalities, including a search feature, making phone calls, and calculating factorials. Each example includes the layout design in XML and the corresponding Java code for the main activity and any additional activities. The applications utilize user input through EditText fields and respond to button clicks to perform specific actions.

Uploaded by

janhavirhude
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)
12 views10 pages

Practical No 18

The document contains multiple Android application examples demonstrating XML layouts and Java code for various functionalities, including a search feature, making phone calls, and calculating factorials. Each example includes the layout design in XML and the corresponding Java code for the main activity and any additional activities. The applications utilize user input through EditText fields and respond to button clicks to perform specific actions.

Uploaded by

janhavirhude
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/ 10

Practical No : - 18

.xml code : -
<?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"
tools:context=".MainActivity">

<ImageView
android:layout_width="300dp"
android:layout_height="200dp"
android:src="@drawable/img1"
android:layout_gravity="center"
android:layout_marginTop="30dp"/>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Search here"
android:layout_gravity="center"
android:textSize="20dp"
android:id="@+id/e1"
android:layout_marginTop="20dp"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Search"
android:layout_marginTop="20dp"
android:onClick="fun1"
android:layout_gravity="center"/>
</LinearLayout>
.java code : -

package com.example.engine;

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

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

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 fun1(View view) {


Intent i1 = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com"));
startActivity(i1);
}}
Exercise 2 :-
.xml code: -
<?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"
tools:context=".MainActivity">

<ImageView
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:src="@drawable/img2"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="Enter Number"
android:id="@+id/t1"
android:gravity="center"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Call"
android:textSize="20dp"
android:onClick="fun1"
android:layout_gravity="center"
android:layout_marginTop="30dp"/>

</LinearLayout>

.java code :-
package com.example.searchengine;

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

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity
{
EditText t1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
t1=findViewById(R.id.t1);

public void fun1(View view)


{
Intent i1=new Intent(Intent.ACTION_VIEW);
String url="tel:"+t1.getText().toString();
i1.setData(Uri.parse(url));
startActivity(i1);

}
}

Output : -
Execise 3 :-

activity_main 1 .xml code

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:padding="20dp">

<EditText
android:id="@+id/e1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter a number"
android:inputType="number"
android:textAlignment="center"
tools:ignore="TouchTargetSizeCheck" />

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Find Factorial"
android:layout_marginTop="20dp"/>

</LinearLayout>
activity_main 2 .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="20dp">

<TextView
android:id="@+id/resultText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Factorial will be displayed here"
android:textSize="18sp"
android:textStyle="bold"
android:padding="10dp"/>
</LinearLayout>

activity_main 1 .java code

package com.example.fact;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


EditText e1;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

e1 = findViewById(R.id.e1);
btn = findViewById(R.id.btn);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String input = e1.getText().toString();
if (!input.isEmpty()) {
int num = Integer.parseInt(input);
if (num < 0) {
Toast.makeText(MainActivity.this, "Enter a positive number!",
Toast.LENGTH_SHORT).show();
} else {
// Send the number to SecondActivity
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("number", num);
startActivity(intent);
}
} else {
Toast.makeText(MainActivity.this, "Please enter a number!",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
activity_main 2 .java code
package com.example.fact;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class SecondActivity extends AppCompatActivity {


TextView resultText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

resultText = findViewById(R.id.resultText);

// Receive the number from MainActivity


Intent intent = getIntent();
int num = intent.getIntExtra("number", 0);

// Calculate factorial
long factorial = calculateFactorial(num);

// Display the result


resultText.setText("Factorial of " + num + " is: " + factorial);
}

// Method to calculate factorial


public long calculateFactorial(int n) {
long fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
}
Output :-

You might also like