Android Activity Lifecycle & Layouts
Android Activity Lifecycle & Layouts
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: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
}
@Override
protected void onStart() {
[Link]();
Log.d("LifeCycle", "onStart");
}
@Override
protected void onResume () {
[Link]();
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" />
</[Link]>
Output:
Practical 2: Write a program to demonstrate different types of layouts
Program: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
Log.d("LifeCycle", "onCreate");
}
Program: acitivity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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 [Link];
import [Link];
import [Link];
import [Link];
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].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: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
Log.d("LifeCycle", "onCreate");
}
Program: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
Log.d("LifeCycle", "onCreate");
}
Program: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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>
</[Link]>
import [Link];
import [Link];
import [Link];
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].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: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
add_btn=findViewById([Link]);
subtract_btn=findViewById([Link]);
num1=findViewById([Link].edit_text1);
num2=findViewById([Link].edit_text2);
result=findViewById([Link]);
add_btn.setOnClickListener(new [Link]() {
@Override
public void onClick(View v) {
[Link]([Link]([Link]([Link]().toString())
+[Link]([Link]().toString())));
});
subtract_btn.setOnClickListener(new [Link]() {
@Override
public void onClick(View v) {
[Link]([Link]([Link]([Link]().toString())-
[Link]([Link]().toString())));
}
});
Program: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
listView=findViewById([Link].list_view);
adapter=new
ArrayAdapter(this,[Link].simple_list_item_1,item);
[Link](adapter);
}
}
Program: acitivity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
ImageView imageView;
Gallery gallery;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
gallery=findViewById([Link].gallery1);
imageView=findViewById([Link]);
[Link](1);
[Link](new [Link]()
{
public void onItemClick(AdapterView<?> parent, View v, int
position, long id) {
// show the selected Image
[Link]([Link][position]);
}
});
}
}
Program: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
[Link](mImageIds[index]);
[Link](new [Link](200, 200));
[Link]([Link].FIT_XY);
return i;
}
Program: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
Button date;
Button time;
[Link]();
}
@Override
public void onTimeSet(TimePicker view, int pHour,
int pMinute) {
mHour = pHour;
mMinute = pMinute;
}
}, mHour, mMinute, true);
[Link](mHour+"-"+mMinute);
[Link]();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
mYear= [Link]().get([Link]);
mMonth=[Link]().get([Link])+1;
mDay=[Link]().get(Calendar.DAY_OF_MONTH) ;
mHour = [Link]().get(Calendar.HOUR_OF_DAY) ;
mMinute = [Link]().get([Link]);
date=findViewById([Link]);
time=findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
show_Datepicker();
}
});
[Link](new [Link]() {
@Override
public void onClick(View v) {
show_Timepicker();
}
});
}
}
Program: activity_main.xml
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
Button date;
Button time;
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
mMonth = monthOfYear + 1;
mYear=year;
mDay=dayOfMonth;
[Link](dayOfMonth+"-"+monthOfYear+1+"-"+year);
}
}, mYearParam, mMonthParam, mDayParam);
[Link]();
}
@Override
public void onTimeSet(TimePicker view, int pHour,
int pMinute) {
mHour = pHour;
mMinute = pMinute;
}
}, mHour, mMinute, true);
[Link](mHour+"-"+mMinute);
[Link]();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
mYear= [Link]().get([Link]);
mMonth=[Link]().get([Link])+1;
mDay=[Link]().get(Calendar.DAY_OF_MONTH) ;
mHour = [Link]().get(Calendar.HOUR_OF_DAY) ;
mMinute = [Link]().get([Link]);
date=findViewById([Link]);
time=findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
show_Datepicker();
}
});
[Link](new [Link]() {
@Override
public void onClick(View v) {
show_Timepicker();
}
});
}
}
Output:
Practical 7: Develop a simple application for demonstrating Option Menu and Context Menu
Program: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link].R;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_option_menu);
button = findViewById([Link]);
registerForContextMenu(button);
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
[Link] menuInfo) {
[Link](menu, v, menuInfo);
[Link]("Context Menu");
[Link](0, [Link](), 0, "Upload");
[Link](0, [Link](), 0, "Search");
[Link](0, [Link](), 0, "Share");
[Link](0, [Link](), 0, "Bookmark");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
[Link](this, "Selected Item: " +[Link](),
Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate([Link].my_menu,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if ([Link]==[Link]()){
[Link](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"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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" />
</[Link]>
Output:
Practical 8: Develop an application to send SMS
Program: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
button=findViewById([Link].button2);
[Link](new [Link]() {
@Override
public void onClick(View v) {
sendSMS(v);
}
});
}
}
Program: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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" />
</[Link]>
Output:
Practical 9: Develop an application to send Email
Program: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
button=findViewById([Link].button2);
[Link](new [Link]() {
@Override
public void onClick(View v) {
final Intent emailIntent = new
Intent([Link].ACTION_SEND);
[Link]("text/plain");
[Link]([Link].EXTRA_EMAIL, new
String[]{ "username@[Link]"});
[Link]([Link].EXTRA_SUBJECT,
"Hello There");
[Link]([Link].EXTRA_TEXT, "Add
Message here");
[Link]("message/rfc822");
try {
startActivity([Link](emailIntent,
"Send email using..."));
} catch ([Link] ex) {
[Link]([Link], "No Clients Installed",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
Program: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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" />
</[Link]>
Output:
Practical 10: Develop an application to Demonstrate a Service
Program: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
}
}
Program: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
@Override
public void onDestroy() {
[Link]();
[Link](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="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
webView=findViewById([Link].web_view);
[Link]().setJavaScriptEnabled(true);
[Link](new WebViewClient() {
@SuppressWarnings("deprecation")
@Override
public void onReceivedError(WebView view, int errorCode, String
description, String failingUrl) {
[Link]([Link], description,
Toast.LENGTH_SHORT).show();
}
@TargetApi([Link].VERSION_CODES.M)
@Override
public void onReceivedError(WebView view, WebResourceRequest req,
WebResourceError rerr) {
onReceivedError(view, [Link](),
[Link]().toString(), [Link]().toString());
}
});
webView .loadUrl("[Link]
}
}
Program: activity_man.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
button=findViewById([Link].next_activity);
[Link](new [Link]() {
@Override
public void onClick(View v) {
startActivity(new Intent([Link],
[Link]));
}
});
}
}
Program: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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: [Link]
[Link] package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
EditText enterText;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
enterText=findViewById([Link].input_field);
button=findViewById([Link].save_button);
[Link](new [Link]() {
@Override
public void onClick(View v) {
if (![Link]().toString().isEmpty()) {
String state = [Link]();
if (Environment.MEDIA_MOUNTED.equals(state)) {
if ([Link].SDK_INT >= 23) {
if (checkPermission()) {
File sdcard =
[Link]();
File dir = new File([Link]()
+ "/text/");
[Link]();
File file = new File(dir, "[Link]");
FileOutputStream os = null;
try {
os = new FileOutputStream(file);
[Link]([Link]().toString().getBytes());
[Link]();
} catch (IOException e) {
[Link]();
}
} else {
requestPermission(); // Code for permission
}
} else {
File sdcard =
[Link]();
File dir = new File([Link]() +
"/text/");
[Link]();
File file = new File(dir, "[Link]");
FileOutputStream os = null;
try {
os = new FileOutputStream(file);
[Link]([Link]().toString().getBytes());
[Link]();
} catch (IOException e) {
[Link]();
}
}
}
}
}
});
}
private boolean checkPermission() {
int result =( [Link]([Link],
[Link].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 ([Link] > 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="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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: [Link]
<uses-permission
android:name="[Link].WRITE_EXTERNAL_STORAGE"/>
<uses-permission
android:name="[Link].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="[Link]" />
</manifest>
Output:
Practical 14: Develop an app to Store and Fetch Data from SQl lite database
Program: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
ListView listView;
DbHelper dbHelper;
FloatingActionButton floatingActionButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
listView=findViewById([Link]);
floatingActionButton=findViewById([Link]);
[Link](new [Link]() {
@Override
public void onClick(View v) {
startActivity(new Intent([Link],
[Link]));
}
});
dbHelper=new DbHelper(this);
@Override
protected void onResume() {
[Link]();
arrayAdapter =new
ArrayAdapter(this,[Link].simple_list_item_1,[Link]());
[Link](arrayAdapter);
}
}
Program: activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<[Link]
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" />
</[Link]>
Program: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
@Override
public void onCreate(SQLiteDatabase db) {
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
String query="drop table if exists Contacts";
[Link](query);
[Link]("Name",Name);
[Link]("Contacts",null,contentValues);
[Link]();
return true;
SQLiteDatabase sqLiteDatabase=getWritableDatabase();
if([Link]()){
while([Link]()){
[Link]([Link](1));
}
}
else{
//[Link](, "", Toast.LENGTH_SHORT).show();
}
return contact;
}
Output:
Practical 15: Develop an app to Show map of a given Location
Program: [Link]
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
Location currentLocation;
FusedLocationProviderClient fusedLocationProviderClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
fusedLocationProviderClient =
[Link](this);
fetchLocation();
}
private void fetchLocation() {
if ([Link](
this, [Link].ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED && [Link](
this, [Link].ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
[Link](this, new String[]
{[Link].ACCESS_FINE_LOCATION}, REQUEST_CODE);
return;
}
@Override
public void onMapReady(GoogleMap googleMap) {
LatLng latLng = new LatLng([Link](),
[Link]());
MarkerOptions markerOptions = new
MarkerOptions().position(latLng).title("I am here!");
[Link]([Link](latLng));
[Link]([Link](latLng,
5));
[Link](markerOptions);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[]
permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE:
if ([Link] > 0 && grantResults[0] ==
PackageManager.PERMISSION_GRANTED) {
fetchLocation();
}
break;
}
}
}
Program: Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<[Link]
xmlns:android="[Link]
xmlns:app="[Link]
xmlns:tools="[Link]
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="[Link]"/>
</[Link]>