pr17,18
pr17,18
Activity_main.xml:-
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;
@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:-
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;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
button = findViewById(R.id.button);
if (!url.startsWith("http://") && !
url.startsWith("https://")) {
url = "https://" + 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;
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">
</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">
</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">
</RelativeLayout>