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

pr17,18

The document contains multiple practical programming exercises for Android development. It includes creating a 'Hello World' application using lifecycle methods, a web navigation feature, a phone dialer button, and a factorial calculator with two screens. Each exercise provides XML layout files and Java code for the main activity to implement the described functionalities.

Uploaded by

Mayur Gole
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 views

pr17,18

The document contains multiple practical programming exercises for Android development. It includes creating a 'Hello World' application using lifecycle methods, a web navigation feature, a phone dialer button, and a factorial calculator with two screens. Each exercise provides XML layout files and Java code for the main activity to implement the described functionalities.

Uploaded by

Mayur Gole
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/ 15

Practical no:17

Q.Write a program to create a “Hello World”


Activity using all
Lifecycle methods to display message using
Log.d.

Activity_main.xml:-

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


<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/an
droid"
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"
tools:context=".MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="30dp"
android:layout_marginTop="40dp"
android:textColor="@color/black"
android:textStyle="bold"/>
</LinearLayout>
mainActivity:-

package com.example.pr14;

import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

private static final String TAG = "Lifecycle";

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "onCreate: Activity Created");
}

@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "onStart: Activity Started");
}
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume: Activity Resumed");
}

@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause: Activity Paused");
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "onStop: Activity Stopped");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: Activity Destroyed");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d(TAG, "onRestart: Activity Restarted");
}
}
+
Practical no:18
1.Write a program to create a text field and a
button “Navigate”.when you enter
“www.google.com” and press button it should
open google page.

Activity_main.xml:-

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


<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/an
droid"
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"
tools:context=".MainActivity">

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:hint="Enter URL"
android:textSize="20sp"
android:ems="10"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:text="Navigate"
android:textSize="20sp"/>

</LinearLayout>

mainActivity:-
package com.example.pr181;

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

public class MainActivity extends AppCompatActivity {

private EditText editText;


private Button button;

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

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

editText.setOnKeyListener((v, keyCode, event) ->


{
if (keyCode == KeyEvent.KEYCODE_ENTER &&
event.getAction() == KeyEvent.ACTION_UP) {
openWebPage();
return true;
}
return false;
});

button.setOnClickListener(v -> openWebPage());


}

private void openWebPage() {


String url = editText.getText().toString().trim();

if (!url.startsWith("http://") && !
url.startsWith("https://")) {
url = "https://" + url;
}

Intent intent = new Intent(Intent.ACTION_VIEW,


Uri.parse(url));

startActivity(intent);
}
}
2.Write a program to create a 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"
tools:context=".MainActivity">

<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="300dp"
android:text="Start Dialer"
android:textSize="30sp"/>
</LinearLayout>
mainActivity:-
package com.example.pr182;

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 {


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

b1 = findViewById(R.id.b1);
b1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
Intent intent = new
Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:")); // Opens
dialer without a number
startActivity(intent);
}
});
}
}
3.Write a program to create two screens.first will
take a one
Input number from user.After click on Factorial
button,secondscreen will open and it should
display factorial of the number.Also specify which
type of intent will use in this case.

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

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/an
droid"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<!-- EditText for number input -->


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

<!-- Button to trigger factorial calculation -->


<Button
android:id="@+id/factorialButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/numberInput"
android:layout_centerHorizontal="true"
android:text="Factorial" />

</RelativeLayout>

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

xmlns:android="http://schemas.android.com/apk/res/an
droid"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<!-- TextView to display factorial result -->


<TextView
android:id="@+id/factorialResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="24sp"
android:text="Factorial result will appear here" />

</RelativeLayout>

Activity_reasult.xml:-
<?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">

<!-- TextView to display factorial result -->


<TextView
android:id="@+id/factorialResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="24sp"
android:text="Factorial result will appear
here" />

</RelativeLayout>

You might also like