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

URL Navigation and Factorial App

The document contains code for an Android application with multiple activities. It includes XML layouts for user input and buttons, as well as Java code for handling user interactions such as navigating to a URL, starting a dialer, and calculating the factorial of a number. The app demonstrates basic Android components like EditText, Button, and Intent for activity transitions.

Uploaded by

shashank220906
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)
2 views10 pages

URL Navigation and Factorial App

The document contains code for an Android application with multiple activities. It includes XML layouts for user input and buttons, as well as Java code for handling user interactions such as navigating to a URL, starting a dialer, and calculating the factorial of a number. The app demonstrates basic Android components like EditText, Button, and Intent for activity transitions.

Uploaded by

shashank220906
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

Practical No.

18

//xml file

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


<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<EditText
android:id="@+id/editTextUrl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter URL (e.g. [Link])"
android:text="[Link]"
android:inputType="textUri"/>

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

</LinearLayout>

//java file
package [Link].exp18;

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {


private EditText editTextUrl;
private Button buttonNavigate;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

editTextUrl = findViewById([Link]);
buttonNavigate = findViewById([Link]);

[Link](new [Link]() {
@Override
public void onClick(View v) {
String url = [Link]().toString().trim();
if (![Link]("[Link] && ![Link]("[Link] {
url = "[Link] + url;
}
Intent intent = new Intent(Intent.ACTION_VIEW, [Link](url));
startActivity(intent);
}
});
}
}
OUTPUT:

//xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<Button
android:id="@+id/buttonStartDialer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Start Dialer"/>
</RelativeLayout>

//java file
package [Link].exp18;

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

private Button buttonStartDialer;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

buttonStartDialer = findViewById([Link]);

[Link](new [Link]() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_DIAL);
[Link]([Link]("[Link] // Opens the dialer without a preset number
startActivity(intent);
}
});
}
}
OUTPUT:
//[Link]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
>

<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/buttonFactorial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Factorial"/>

</LinearLayout>

//[Link]
package [Link].exp18;

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {

private EditText editTextNumber;


private Button buttonFactorial;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

editTextNumber = findViewById([Link]);
buttonFactorial = findViewById([Link]);

[Link](new [Link]() {
@Override
public void onClick(View v) {
String numberStr = [Link]().toString().trim();
if (![Link]()) {
int number = [Link](numberStr);
Intent intent = new Intent([Link], [Link]);
[Link]("number", number);
startActivity(intent);
}
}
});
}
}

//[Link]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">

<TextView
android:id="@+id/textViewResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="Factorial result will appear here"/>
</LinearLayout>

//[Link]
package [Link].exp18;

import [Link];
import [Link];
import [Link];
public class ResultActivity extends AppCompatActivity {

private TextView textViewResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_result);

textViewResult = findViewById([Link]);

int number = getIntent().getIntExtra("number", -1); // Defaulting to -1 if data isn't received


if (number == -1) {
[Link]("Error: No number provided!");
} else {
long factorial = calculateFactorial(number);
[Link]("Factorial of " + number + " is: " + factorial);
}

private long calculateFactorial(int num) {


long result = 1;
for (int i = 1; i <= num; i++) {
result *= i;
}
return result;
}
}
OUTPUT:

You might also like