Android_Practical
Android_Practical
Android_Practical
To navigate transitions between stages of the activity lifecycle, the Activity class provides a core set of
six callbacks: onCreate(), onStart(), onResume(), onPause(), onStop(), and onDestroy(). The
system invokes each of these callbacks as an activity enters a new state.
Program: MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
Log.d("LifeCycle", "onStart");
}
@Override
protected void onResume () {
super.onResume();
Log.d("LifeCycle", "onResume");
}
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Output:
Practical 2: Write a program to demonstrate different types of layouts
Program: MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("LifeCycle", "onCreate");
}
Program: acitivity_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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 2">
</Button>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 3">
</Button>
</LinearLayout>
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("LifeCycle", "onCreate");
}
Program: activity_main.xml
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2">
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3">
</Button>
</LinearLayout>
Program: MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("LifeCycle", "onCreate");
}
Program: activity_main.xml
<?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=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button"
android:layout_centerInParent="true"
android:text="Button 2">
</Button>
<Button
android:layout_width="wrap_content"
android:layout_below="@+id/button"
android:layout_centerInParent="true"
android:layout_height="wrap_content"
android:text="Button 3">
</Button>
<Button
android:layout_width="wrap_content"
android:layout_toLeftOf="@+id/button"
android:layout_centerInParent="true"
android:layout_height="wrap_content"
android:text="Button 3">
</Button>
<Button
android:layout_width="wrap_content"
android:layout_toRightOf="@+id/button"
android:layout_centerInParent="true"
android:layout_height="wrap_content"
android:text="Button 3">
</Button>
</RelativeLayout>
Program: MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("LifeCycle", "onCreate");
}
Program: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="342dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button"
android:layout_centerInParent="true"
android:text="Button 2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/button2"
tools:layout_editor_absoluteY="341dp"></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_centerInParent="true"
android:text="Button 3"
app:layout_constraintTop_toBottomOf="@+id/button2"
tools:layout_editor_absoluteX="160dp"></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toLeftOf="@+id/button"
android:text="Button 3"
app:layout_constraintBottom_toTopOf="@+id/button2"
tools:layout_editor_absoluteX="160dp"></Button>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toRightOf="@+id/button"
android:text="Button 3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></Button>
</androidx.constraintlayout.widget.ConstraintLayout>
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("LifeCycle", "onCreate");
}
Program: activity_main.xml
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="342dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button"
android:layout_centerInParent="true"
android:text="Button 2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/button2"
tools:layout_editor_absoluteY="341dp"></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_centerInParent="true"
android:text="Button 3"
app:layout_constraintTop_toBottomOf="@+id/button2"
tools:layout_editor_absoluteX="160dp"></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toLeftOf="@+id/button"
android:text="Button 3"
app:layout_constraintBottom_toTopOf="@+id/button2"
tools:layout_editor_absoluteX="160dp"></Button>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toRightOf="@+id/button"
android:text="Button 3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</Button>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="342dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="342dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="342dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="342dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button"
app:layout_constraintEnd_toStartOf="@+id/button2"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="342dp" />
</GridLayout>
Program: MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add_btn=findViewById(R.id.add);
subtract_btn=findViewById(R.id.subtract);
num1=findViewById(R.id.edit_text1);
num2=findViewById(R.id.edit_text2);
result=findViewById(R.id.result);
add_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.setText(String.valueOf(Integer.parseInt(num1.getText().toString())
+Integer.parseInt(num2.getText().toString())));
});
subtract_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.setText(String.valueOf(Integer.parseInt(num1.getText().toString())-
Integer.parseInt(num2.getText().toString())));
}
});
Program: activity_main.xml
<?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"
android:columnCount="2"
android:rowCount="2"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:id="@+id/result"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_height="200dp">
</TextView>
<EditText
android:layout_width="match_parent"
android:layout_margin="5dp"
android:id="@+id/edit_text1"
android:layout_below="@+id/result"
android:hint="Enter 1st Number"
android:maxLines="1"
android:inputType="number"
android:layout_marginTop="10dp"
android:layout_height="wrap_content">
</EditText>
<EditText
android:layout_width="match_parent"
android:id="@+id/edit_text2"
android:maxLines="1"
android:inputType="number"
android:layout_below="@+id/edit_text1"
android:hint="Enter 2nd Number"
android:layout_margin="5dp"
android:layout_marginTop="10dp"
android:layout_height="wrap_content">
</EditText>
<Button
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="+"
android:layout_below="@+id/edit_text2"
android:layout_margin="5dp"
android:id="@+id/add">
</Button>
<Button
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="-"
android:layout_below="@+id/edit_text2"
android:layout_margin="5dp"
android:layout_toRightOf="@+id/add"
android:id="@+id/subtract">
</Button>
</RelativeLayout>
Output:
Practical 4: Write a program to demonstrate List View
Program: MainActivity.java
package com.example.listview;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=findViewById(R.id.list_view);
adapter=new
ArrayAdapter(this,android.R.layout.simple_list_item_1,item);
listView.setAdapter(adapter);
}
}
Program: acitivity_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"
tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:id="@+id/list_view"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp" />
</LinearLayout>
Output:
Practical 5: Write a program to demonstrate Photo Gallery
Program: MainActivity.java
package com.example.photogallery;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.ImageView;
ImageView imageView;
Gallery gallery;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gallery=findViewById(R.id.gallery1);
imageView=findViewById(R.id.imageView);
gallery.setSpacing(1);
gallery.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int
position, long id) {
// show the selected Image
imageView.setImageResource(galleryAdapter.mImageIds[position]);
}
});
}
}
Program: GalleryAdapter.java
package com.example.photogallery;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
i.setImageResource(mImageIds[index]);
i.setLayoutParams(new Gallery.LayoutParams(200, 200));
i.setScaleType(ImageView.ScaleType.FIT_XY);
return i;
}
Program: 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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Gallery
android:id="@+id/gallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/imageView"
android:layout_marginTop="100dp"
android:layout_width="250dp"
android:layout_gravity="center_horizontal"
android:layout_height="250dp"
android:src="@drawable/abc_vector_test" />
</LinearLayout>
Output:
Practical 6: Write a program to demonstrate Time Picker and Date Picker
Program: MainActivity.java
package com.example.dateandtimepicker;
import androidx.appcompat.app.AppCompatActivity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import java.util.Calendar;
Button date;
Button time;
datePickerDialog.show();
}
@Override
public void onTimeSet(TimePicker view, int pHour,
int pMinute) {
mHour = pHour;
mMinute = pMinute;
}
}, mHour, mMinute, true);
time.setText(mHour+"-"+mMinute);
timePickerDialog.show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mYear= Calendar.getInstance().get(Calendar.YEAR);
mMonth=Calendar.getInstance().get(Calendar.MONTH)+1;
mDay=Calendar.getInstance().get(Calendar.DAY_OF_MONTH) ;
mHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY) ;
mMinute = Calendar.getInstance().get(Calendar.MINUTE);
date=findViewById(R.id.date);
time=findViewById(R.id.time);
date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
show_Datepicker();
}
});
time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
show_Timepicker();
}
});
}
}
Program: activity_main.xml
package com.example.dateandtimepicker;
import androidx.appcompat.app.AppCompatActivity;
import android.app.DatePickerDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TimePicker;
import java.util.Calendar;
Button date;
Button time;
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mMonth = monthOfYear + 1;
mYear=year;
mDay=dayOfMonth;
date.setText(dayOfMonth+"-"+monthOfYear+1+"-"+year);
}
}, mYearParam, mMonthParam, mDayParam);
datePickerDialog.show();
}
@Override
public void onTimeSet(TimePicker view, int pHour,
int pMinute) {
mHour = pHour;
mMinute = pMinute;
}
}, mHour, mMinute, true);
time.setText(mHour+"-"+mMinute);
timePickerDialog.show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mYear= Calendar.getInstance().get(Calendar.YEAR);
mMonth=Calendar.getInstance().get(Calendar.MONTH)+1;
mDay=Calendar.getInstance().get(Calendar.DAY_OF_MONTH) ;
mHour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY) ;
mMinute = Calendar.getInstance().get(Calendar.MINUTE);
date=findViewById(R.id.date);
time=findViewById(R.id.time);
date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
show_Datepicker();
}
});
time.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
show_Timepicker();
}
});
}
}
Output:
Practical 7: Develop a simple application for demonstrating Option Menu and Context Menu
Program: MainActivity.java
package com.example.dateandtimepicker;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.example.dateandtimepicker.R;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_option_menu);
button = findViewById(R.id.button);
registerForContextMenu(button);
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, "Upload");
menu.add(0, v.getId(), 0, "Search");
menu.add(0, v.getId(), 0, "Share");
menu.add(0, v.getId(), 0, "Bookmark");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
Toast.makeText(this, "Selected Item: " +item.getTitle(),
Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.my_menu,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (R.id.share==item.getItemId()){
Toast.makeText(this, "Share", Toast.LENGTH_SHORT).show();
}
return true;
}
}
<item android:id="@+id/share"
android:title="Share App"/>
<item android:id="@+id/rate"
android:title="Rate Us !"/>
<item android:id="@+id/play"
app:showAsAction="always"
android:icon="@drawable/ic_android_black_24dp"
android:title="Play Store"/>
</menu>
Program: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".OptionMenu">
<Button
android:id="@+id/button"
android:layout_width="wrap_contenat"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Output:
Practical 8: Develop an application to send SMS
Program: MainActivity.java
package com.example.sendsms;
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;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendSMS(v);
}
});
}
}
Program: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".MainActivity">
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="146dp"
android:layout_marginLeft="146dp"
android:layout_marginTop="310dp"
android:layout_marginEnd="177dp"
android:layout_marginRight="177dp"
android:layout_marginBottom="373dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Output:
Practical 9: Develop an application to send Email
Program: MainActivty.java
package com.example.sendsms;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentUris;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Intent emailIntent = new
Intent(android.content.Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new
String[]{ "[email protected]"});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"Hello There");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Add
Message here");
emailIntent.setType("message/rfc822");
try {
startActivity(Intent.createChooser(emailIntent,
"Send email using..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this, "No Clients Installed",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
Program: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".MainActivity">
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="146dp"
android:layout_marginLeft="146dp"
android:layout_marginTop="310dp"
android:layout_marginEnd="177dp"
android:layout_marginRight="177dp"
android:layout_marginBottom="373dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Output:
Practical 10: Develop an application to Demonstrate a Service
Program: MainActivity.java
package com.example.simpleservice;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Program: MyService.java
package com.example.simpleservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
}
Program: activity_main.xml
<?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=".MainActivity">
<Button
android:onClick="startService"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/start_service"
android:text="Start Service">
</Button>
<Button
android:onClick="stopService"
android:layout_width="match_parent"
android:layout_below="@id/start_service"
android:text="Stop Service"
android:layout_height="wrap_content"
android:id="@+id/Stop_Service">
</Button>
</RelativeLayout>
Output:
Practical 11: Develop an application to demonstrate WebView
Program: MainActivity.java
package com.example.webview;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.TargetApi;
import android.os.Bundle;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView=findViewById(R.id.web_view);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@SuppressWarnings("deprecation")
@Override
public void onReceivedError(WebView view, int errorCode, String
description, String failingUrl) {
Toast.makeText(MainActivity.this, description,
Toast.LENGTH_SHORT).show();
}
@TargetApi(android.os.Build.VERSION_CODES.M)
@Override
public void onReceivedError(WebView view, WebResourceRequest req,
WebResourceError rerr) {
onReceivedError(view, rerr.getErrorCode(),
rerr.getDescription().toString(), req.getUrl().toString());
}
});
webView .loadUrl("https://geekdevilmodernboyz.blogspot.com/");
}
}
Program: activity_man.xml
<?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=".MainActivity">
<TextView
android:layout_width="match_parent"
android:id="@+id/text"
android:layout_height="wrap_content"
android:text="Welcome"
android:textAlignment="center"
android:textSize="25dp">
</TextView>
<WebView
android:layout_width="match_parent"
android:layout_marginTop="5dp"
android:layout_below="@+id/text"
android:id="@+id/web_view"
android:layout_height="match_parent"/>
</RelativeLayout>
Output:
Practical 12: Demonstrate the application of Intent Class
Program: MainActivity.java
package com.example.intent;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=findViewById(R.id.next_activity);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,
Main2Activity.class));
}
});
}
}
Program: activity_main.xml
<?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=".Main2Activity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:textSize="20dp"
android:textStyle="bold"
android:text="This is the 2nd Activity"/>
</RelativeLayout>
Output:
Practical 13: Develop an app to write to external storage and create a text file
Program: MainActivity.java
MainActivity.java package com.example.writingtoexternalstorage;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
EditText enterText;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
enterText=findViewById(R.id.input_field);
button=findViewById(R.id.save_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!enterText.getText().toString().isEmpty()) {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
if (Build.VERSION.SDK_INT >= 23) {
if (checkPermission()) {
File sdcard =
Environment.getExternalStorageDirectory();
File dir = new File(sdcard.getAbsolutePath()
+ "/text/");
dir.mkdir();
File file = new File(dir, "sample.txt");
FileOutputStream os = null;
try {
os = new FileOutputStream(file);
os.write(enterText.getText().toString().getBytes());
os.close();
} catch (IOException e) {
e.printStackTrace();
}
} else {
requestPermission(); // Code for permission
}
} else {
File sdcard =
Environment.getExternalStorageDirectory();
File dir = new File(sdcard.getAbsolutePath() +
"/text/");
dir.mkdir();
File file = new File(dir, "sample.txt");
FileOutputStream os = null;
try {
os = new FileOutputStream(file);
os.write(enterText.getText().toString().getBytes());
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
});
}
private boolean checkPermission() {
int result =( ContextCompat.checkSelfPermission(MainActivity.this,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE));
if (result == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
return false;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String
permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
Log.e("value", "Permission Granted, Now you can use local
drive .");
} else {
Log.e("value", "Permission Denied, You cannot use local drive
.");
}
break;
}
}
}
Program: activity_main.xml
<?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=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/input_field"
android:hint="Enter your text here"
android:maxLines="1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/save_button"
android:layout_below="@+id/input_field"
android:text="Save"/>
</RelativeLayout>
Setting up permissions: AndroidManifest.xml
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:requestLegacyExternalStorage="true"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
Output:
Practical 14: Develop an app to Store and Fetch Data from SQl lite database
Program: MainActivitivity.java
package com.example.contacts;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.util.ArrayList;
ListView listView;
DbHelper dbHelper;
FloatingActionButton floatingActionButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=findViewById(R.id.listview);
floatingActionButton=findViewById(R.id.floatingActionButton);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,
CreateContact.class));
}
});
dbHelper=new DbHelper(this);
@Override
protected void onResume() {
super.onResume();
arrayAdapter =new
ArrayAdapter(this,android.R.layout.simple_list_item_1,dbHelper.getContact());
listView.setAdapter(arrayAdapter);
}
}
Program: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".MainActivity">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="50dp"
android:clickable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@android:drawable/ic_input_add" />
<ListView
android:layout_width="match_parent"
android:id="@+id/listview"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Program: DbHelper.java
package com.example.contacts;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
@Override
public void onCreate(SQLiteDatabase db) {
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
String query="drop table if exists Contacts";
db.execSQL(query);
contentValues.put("Name",Name);
sqLiteDatabase.insert("Contacts",null,contentValues);
sqLiteDatabase.close();
return true;
SQLiteDatabase sqLiteDatabase=getWritableDatabase();
if(cursor.moveToFirst()){
while(cursor.moveToNext()){
contact.add(cursor.getString(1));
}
}
else{
//Toast.makeText(, "", Toast.LENGTH_SHORT).show();
}
return contact;
}
Output:
Practical 15: Develop an app to Show map of a given Location
Program: MainActivity.java
package com.example.googlemapview;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.FragmentActivity;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
Location currentLocation;
FusedLocationProviderClient fusedLocationProviderClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fusedLocationProviderClient =
LocationServices.getFusedLocationProviderClient(this);
fetchLocation();
}
private void fetchLocation() {
if (ActivityCompat.checkSelfPermission(
this, Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
this, Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
return;
}
@Override
public void onMapReady(GoogleMap googleMap) {
LatLng latLng = new LatLng(currentLocation.getLatitude(),
currentLocation.getLongitude());
MarkerOptions markerOptions = new
MarkerOptions().position(latLng).title("I am here!");
googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,
5));
googleMap.addMarker(markerOptions);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[]
permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
fetchLocation();
}
break;
}
}
}
Program: Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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=".MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
</androidx.constraintlayout.widget.ConstraintLayout>