Prac 18
Prac 18
PRACTICAL NO: 18
X. Exercise
1 -Write a program to create a text field and a button “Navigate”. When you enter
“www.google.com” and press navigate button it should open google page.
Code:
<?xml version="1.0" encoding="utf-8"?> android:layout_height="wrap_content"
<androidx.constraintlayout.widget.ConstraintLay android:hint="Enter URL Here"
out
android:textColor="@android:color/black"
xmlns:android="http://schemas.android.com/apk/ android:textAlignment="center"/>
res/android"
<Button
xmlns:app="http://schemas.android.com/apk/res- android:id="@+id/navigate"
auto" android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools" android:text="Navigate To"/>
android:layout_width="match_parent"
android:layout_height="match_parent" <TextView
tools:context=".MainActivity"> android:id="@+id/textView"
android:layout_width="match_parent"
<LinearLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:text="Note - Enter URL In
android:layout_height="wrap_content" Proper Format"
android:orientation="vertical"
android:layout_margin="20dp" android:textColor="@android:color/black"
android:padding="20dp" android:textAlignment="center"
android:layout_marginTop="30dp"
app:layout_constraintTop_toTopOf="parent" android:textSize="20sp"/>
app:layout_constraintStart_toStartOf="parent" <TextView
android:id="@+id/textView2"
app:layout_constraintEnd_toEndOf="parent"> android:layout_width="match_parent"
android:layout_height="wrap_content"
<EditText android:text="Ex. 'www.google.com'"
android:id="@+id/urlbar" android:textSize="20sp"
android:layout_width="match_parent"
dd
</LinearLayout>
android:textColor="@android:color/black"
android:textAlignment="center" </androidx.constraintlayout.widget.ConstraintLa
android:layout_marginTop="20dp"/> yout>
MainActivity.java Program
package co6i.micro.project.practical18_1;
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;
import android.util.Log;
Button navigate_to;
EditText url_bar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navigate_to = findViewById(R.id.navigate);
url_bar = findViewById(R.id.urlbar);
navigate_to.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String url = url_bar.getText().toString().trim();
if (!url.isEmpty()) {
// Ensure the URL starts with https:// or http://
if (!url.startsWith("http://") && !url.startsWith("https://")) {
url = "https://" + url;
}
} else {
Log.e(TAG, "URL field is empty!");
}
}
});
}
}
Output:
dd
2 - Write a program to create button “Start Dialer”. When u click on this button it should
open the phone dialer.
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="300dp"
android:layout_centerHorizontal="true"
android:text="Start Dialer" />
</RelativeLayout>
MainActivity.java Program
package com.example.android;
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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:+919876543210")); // Moved inside before startActivity
startActivity(intent);
dd
}
});
}
}
Output:
dd
3 - 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.
Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
tools:context=".FactorialActivity"
android:padding="20dp">
Output:
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Factorial of number is"
android:textSize="22sp"
android:textStyle="bold"
android:layout_centerInParent="true"/>
</RelativeLayout>
MainActivity.java Program
package co6i.micro.project.practical18_3;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fact_num = findViewById(R.id.fact_num);
dd
if (numberStr.isEmpty()) {
Toast.makeText(this, "Please enter a number", Toast.LENGTH_SHORT).show();
return;
}
package co6i.micro.project.practical18_3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class FactorialActivity extends AppCompatActivity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_factorial);
Bundle b = getIntent().getExtras();
int no = Integer.parseInt(b.getString("number"));
long f=1;
for(int i=no; i>0; i--)
{
f = f * i;
}
tv = findViewById(R.id.tv);
tv.setText("Factorial of " + no + " is " + f);
}
}