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

Prac18_Updated_Android_Programs

The document contains code for three Android applications: one that opens a URL in a browser, another that opens the phone dialer, and a third that calculates the factorial of a number. Each application includes both Java and XML components, detailing the user interface and functionality. The factorial application also includes a second activity to display the calculated result.

Uploaded by

ankitasurya663
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 views

Prac18_Updated_Android_Programs

The document contains code for three Android applications: one that opens a URL in a browser, another that opens the phone dialer, and a third that calculates the factorial of a number. Each application includes both Java and XML components, detailing the user interface and functionality. The factorial application also includes a second activity to display the calculated result.

Uploaded by

ankitasurya663
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/ 5

Prac18 - Android Programs

1. Open Google in Browser - Java (MainActivity.java)

package com.example.opengoogle;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


EditText editText;
Button navigateButton;

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

editText = findViewById(R.id.editText);
navigateButton = findViewById(R.id.button);

navigateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = editText.getText().toString();
if (!url.startsWith("http://") && !url.startsWith("https://")) {
url = "http://" + url;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
});
}
}

1. Open Google in Browser - XML (activity_main.xml)

<?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:padding="20dp"
android:gravity="center">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter URL (e.g., www.google.com)"
android:inputType="textUri" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Navigate" />
</LinearLayout>

2. Open Phone Dialer - Java (MainActivity.java)

package com.example.opendialer;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


Button dialButton;

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

dialButton = findViewById(R.id.button);

dialButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:"));
startActivity(intent);
}
});
}
}

2. Open Phone Dialer - XML (activity_main.xml)

<?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:gravity="center"
android:orientation="vertical">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Dialer" />
</LinearLayout>

3. Factorial Calculation - Java (MainActivity.java)

package com.example.factorial;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


EditText numberInput;
Button factorialButton;

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

numberInput = findViewById(R.id.editTextNumber);
factorialButton = findViewById(R.id.button);

factorialButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int number = Integer.parseInt(numberInput.getText().toString());
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("number", number);
startActivity(intent);
}
});
}
}

3. Factorial Calculation - Java (SecondActivity.java)


package com.example.factorial;

import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class SecondActivity extends AppCompatActivity {


TextView resultTextView;

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

resultTextView = findViewById(R.id.textViewResult);
int number = getIntent().getIntExtra("number", 0);

int factorial = 1;
for (int i = 1; i <= number; i++) {
factorial *= i;
}

resultTextView.setText("Factorial: " + factorial);


}
}

3. Factorial Calculation - XML (activity_main.xml)

<?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">

<EditText
android:id="@+id/editTextNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter a number"
android:inputType="number" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate Factorial" />
</LinearLayout>
3. Factorial Calculation - XML (activity_second.xml)

<?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:gravity="center"
android:orientation="vertical">

<TextView
android:id="@+id/textViewResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Factorial Result"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>

You might also like