ITSM Journal 1
ITSM Journal 1
:- 1
→ MainActivity.java
package com.example.helloworldapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
→ 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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="30sp"
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 No.:- 2
→ Colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="colorRed">#FF0000</color>
<color name="colorBlue">#0000FF</color>
</resources>
→ 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">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Color One"
android:background="@color/colorRed"
tools:layout_editor_absoluteX="28dp"
tools:layout_editor_absoluteY="70dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorBlue"
android:text="Color Two"
tools:layout_editor_absoluteX="179dp"
tools:layout_editor_absoluteY="70dp" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/colorGreen"
android:text="Color Three"
tools:layout_editor_absoluteX="305dp"
tools:layout_editor_absoluteY="70dp" />
</LinearLayout>
Output:-
Theme:-
→ AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<application
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/BlueTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
→ styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="BlueTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:Background">#00FF00</item>
<item name="android:textColor">#000000</item>
</style>
</resources>
Output:-
String:-
→ 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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ParaHead"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Description"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</LinearLayout>
→ strings.xml
<resources>
<string name="app_name">My Application</string>
<string name="ParaHead">Programming Media</string>
<string name="Description">The android Multimedia Framework</string>
</resources>
Output:-
String:-
→ 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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ParaHead"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Description"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</LinearLayout>
→ strings.xml
<resources>
<string name="app_name">My Application</string>
<string name="ParaHead">Programming Media</string>
<string name="Description">The android Multimedia Framework</string>
</resources>
Output:-
Drawables and Images:-
→ activity_main.xml
<ImageView
android:id="@+id/img1"
android:layout_width="259dp"
android:layout_height="150dp"
android:src="@drawable/image1" />
<ImageView
android:id="@+id/img2"
android:layout_width="230dp"
android:layout_height="136dp"
android:src="@drawable/image2" />
<ImageView
android:id="@+id/img3"
android:layout_width="304dp"
android:layout_height="165dp"
android:src="@drawable/image3" />
</LinearLayout>
→ Add Images
Output:-
Dimension:-
→ dimens.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="textview_height">35dp</dimen>
<dimen name="textview_width">150dp</dimen>
<dimen name="font_size">26sp</dimen></resources>
→ 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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/College"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="@dimen/font_size"
android:text="Shaliendra Education Society's"/>
</LinearLayout>
Output:-
Practical No:-3
→ MainActivity.java
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
String tag="ALifecycle";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(tag,"In the onCreate() event");
}
public void onStart()
{
super.onStart();
Log.d(tag,"In the onStart() event");
}
public void onRestart()
{
super.onRestart();
Log.d(tag,"In the onRestart() event");
}
public void onResume()
{
super.onResume();
Log.d(tag,"In the onResume() event");
}
public void onPause()
{
super.onPause();
Log.d(tag,"In the onPause() event");
}
public void onStop()
{
super.onStop();
Log.d(tag,"In the onStop() event");
}
public void onDestroy()
{
super.onDestroy();
Log.d(tag,"In the onDestroy() event");
}
public void MessageButton(View v)
{
if(v.getId() == R.id.button)
{
MessageBox("Welcome!!!");
}
}
public void MessageBox(String message)
{
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
}
Output:-
Practical No.:-3
B :- Creating a Fragment.
→ BlankFragment.java
package com.example.a3a;
import android.content.Context;
import android.os.Bundle;
import android.net.Uri;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class BlankFragment extends Fragment {
String tag="Lifecycle";
public BlankFragment() {
// Required empty public constructor
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Nullable
@Override
public View getView() {
return super.getView();
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onStop() {
super.onStop();
}
@Override
public void onDestroyView() {
super.onDestroyView();
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
→ 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:id="@+id/test_fragment"
class="com.example.a3a.BlankFragment"
tools:layout="@layout/fragment_blank"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
→ Fragment_blank.xml
Output:-
Practical No.:- 4
package com.example.mylayouts;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnrelative=(Button) findViewById(R.id.btnrelative);
btnrelative.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i=new Intent(getApplicationContext(),RelativeActivity.class);
startActivity(i);
}
});
}
}
Output:-
Similarly, create a new Activity and its corresponding layout
Activity Name = LinearHorizontalActivity
Layout Name = activity_linear_horizontal
In activity_linear_horizontal.xml
App -> res -> layout -> activity_linear_horizontal.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="horizontal"
tools:context=".LinearHorizontalActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b1"
android:text="Button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b2"
android:text="Button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b3"
android:text="Button3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b4"
android:text="Button4" />
</LinearLayout>
→ MainActivity.java
package com.example.mylayouts;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnrelative=(Button) findViewById(R.id.btnrelative);
Button btnlinearH=(Button) findViewById(R.id.btnlinearH);
btnrelative.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i=new Intent(getApplicationContext(),RelativeActivity.class);
startActivity(i);
}
});
btnlinearH.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i=new
Intent(getApplicationContext(),LinearHorizontalActivity.class);
startActivity(i);
}
});
}
}
→ activity_main.xml
→ MainActivity.java
package com.example.mylayouts;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnrelative=(Button) findViewById(R.id.btnrelative);
Button btnlinearH=(Button) findViewById(R.id.btnlinearH);
Button btnlinearV=(Button) findViewById(R.id.btnlinearV);
btnrelative.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i=new Intent(getApplicationContext(),RelativeActivity.class);
startActivity(i);
}
});
btnlinearH.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i=new
Intent(getApplicationContext(),LinearHorizontalActivity.class);
startActivity(i);
}
});
btnlinearV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i=new
Intent(getApplicationContext(),LinearVerticalActivity.class);
startActivity(i);
}
});
}
}
→ 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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Layout Demonstration"
android:layout_gravity="center" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Relative Activity"
android:layout_gravity="center"
android:id="@+id/btnrelative" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LinearLayot(Horizontal)"
android:id="@+id/btnlinearH"
android:layout_gravity="center" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LinearLayot(vertical)"
android:id="@+id/btnlinearV"
android:layout_gravity="center" />
</LinearLayout>
Output:-
Practical No.:-4
→ 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"
tools:context=".MainActivity">
<GridView
android:id="@+id/myGridview1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:columnWidth="100dp"
android:gravity="center"
android:minHeight="90dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
/>
</LinearLayout>
→ MainActivity.java
package com.example.practgrid;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import android.os.Bundle;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
GridView androidGridView;
Integer[] imageIDs={
R.drawable.elephant,R.drawable.koala,R.drawable.lion,
R.drawable.panda,R.drawable.tiger,R.drawable.monkey
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
androidGridView = (GridView) findViewById(R.id.myGridview1);
androidGridView.setAdapter(new ImageAdapterGridView(this));
final ArrayList<String>message=new ArrayList<>();
message.add("Elephant.bmp");
message.add("Koala.bmp");
message.add("Lion.bmp");
message.add("Panda.bmp");
message.add("Tiger.bmp");
message.add("Monkey.bmp");
androidGridView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent,
View v, int position, long id) {
String m=message.get(position);
Toast.makeText(MainActivity.this,""+m, Toast.LENGTH_LONG).show();
}
});
}
public class ImageAdapterGridView extends BaseAdapter {
private Context mContext;
public ImageAdapterGridView(Context c) {
mContext = c;
}
public int getCount() {
return imageIDs.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView mImageView;
if (convertView == null) {
mImageView = new ImageView(mContext);
mImageView.setLayoutParams(new GridView.LayoutParams(130,
130));
mImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
mImageView.setPadding(16, 16, 16, 16);
} else {
mImageView = (ImageView) convertView;
}
mImageView.setImageResource(imageIDs[position]);
return mImageView;
}
}
}
Output:-
Practical No.:-4
Aim :- Create an android application that display a login form with text from
username and password and button for submit and reset. On submitting, toast
should be display accordingly. i.e. “Correct Username and Password” if username
and password match and “Incorrect Username and Password” if username and
password do not match.
→ 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">
<LinearLayout
android:layout_width="300dp"
android:layout_height="600dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.333"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="65dp">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LOGIN" />
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username" />
<EditText
android:id="@+id/field1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password" />
<EditText
android:id="@+id/field2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send" />
<Button
android:id="@+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
→ MainActivity.java
package com.example.validate;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.lang.String;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button send=(Button)findViewById(R.id.send);
Button reset=(Button)findViewById(R.id.reset);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText username=(EditText)findViewById(R.id.field1);
EditText password=(EditText)findViewById(R.id.field2);
String username_string=username.getText().toString();
String password_string=password.getText().toString();
System.out.println(username_string);
if(username_string.equals("admin")&&password_string.equals("12345"))
{
Toast.makeText(getApplicationContext(),"Correct Username & Password",
Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(),"Incorrect Username or Password",
Toast.LENGTH_SHORT).show();
}
}
});
reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText username=(EditText)findViewById(R.id.field1);
EditText password=(EditText)findViewById(R.id.field2);
username.setText("");
password.setText("");
}
});
}
Output:-
Practical No.:-6
A :- Create an android application to demonstrate the use of menu the toast should
be appeared by selecting the menu items.
→ In mymenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<item
android:id="@+id/newb"
android:title="Bookmarks"/>
<item
android:id="@+id/search"
android:title="Search"/>
<item
android:id="@+id/save"
android:title="Save"/>
<item
android:id="@+id/share"
android:title="Share"/>
<item
android:id="@+id/delete"
android:title="Delete"/>
<item
android:id="@+id/exit"
android:title="Exit"/>
</menu>
→ In MainActivity.java
package com.example.a6amenu;
import androidx.appcompat.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater mi=getMenuInflater();
mi.inflate(R.menu.mymenu,menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case R.id.newb:
Toast.makeText(this,"New Selected",Toast.LENGTH_SHORT).show();
return true;
case R.id.share:
Toast.makeText(this,"Share Selected",Toast.LENGTH_SHORT).show();
return true;
case R.id.delete:
Toast.makeText(this,"Delete Selected",Toast.LENGTH_SHORT).show();
return true;
case R.id.save:
Toast.makeText(this,"Save Selected",Toast.LENGTH_SHORT).show();
return true;
case R.id.search:
Toast.makeText(this,"Search Selected",Toast.LENGTH_SHORT).show();
return true;
case R.id.exit:
Toast.makeText(this,"Exit Selected",Toast.LENGTH_SHORT).show();
return true;
default:
Toast.makeText(this,"Default",Toast.LENGTH_SHORT).show();
}
return super.onOptionsItemSelected(item);
}
}
Output:-
Practical No.:-6
→ MainActivity.java
package com.example.backbuttondialog;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AlertDialog.Builder;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onBackPressed()
{
AlertDialog.Builder box=new AlertDialog.Builder(this);
box.setTitle("AlertDialog Title");
box.setMessage("Do you want to save this? ");
box.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
box.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert=box.create();
alert.show();
}
}
Output:-
Practical No.:-7
Aim:-Create an android application to pass the data from one activity to another
activity or one application to another application in a same application using the
Intent.
→ 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:hint="Type something"
android:id="@+id/edittext1"/>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click"
android:layout_below="@+id/edittext1"
android:textSize="20sp" />
</RelativeLayout>
→ MainActivity.java
package com.example.a7practamp;
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;
public class MainActivity extends AppCompatActivity {
Button btn;
EditText et;
String st;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=findViewById(R.id.button1);
et=findViewById(R.id.edittext1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,SecondActivity.class);
st=et.getText().toString();
i.putExtra("Value",st);
startActivity(i);
finish();
}
});
}
}
→ Create an activity
→ activity_second.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=".SecondActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Value"
android:textSize="30sp"
android:gravity="center" />
</RelativeLayout>
→ SecondActivity.java
package com.example.a7practamp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
TextView tv;
String st;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
tv=findViewById(R.id.textView);
st=getIntent().getExtras().getString("Value");
tv.setText(st);
}
}
Output:-
Practical No:- 8
→ 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/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notify"
tools:layout_editor_absoluteX="152dp"
tools:layout_editor_absoluteY="163dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
→ MainActivity.java
package com.example.notification8b;
import androidx.appcompat.app.AppCompatActivity;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
import android.os.Bundle;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import android.view.View;
import android.widget.Toast;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
public final String CHANNEL_ID="personal_notification";
public final int NOTIFICATION_ID=001;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1=(Button)findViewById(R.id.b1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createNotificationChannel();
Toast.makeText(getApplicationContext(), "Hi", Toast.LENGTH_SHORT).show();
NotificationCompat.Builder builder=new
NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setContentTitle("Simple Notification");
builder.setContentText("This is a test notification");
builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat
notificationManagerCompat=NotificationManagerCompat.from(getApplicationContext());
notificationManagerCompat.notify(NOTIFICATION_ID,builder.build());
}
});
}
private void createNotificationChannel() {
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O)
{
CharSequence name="Personal Notification";
String description="This is description";
int importance= NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel notificationChannel=new
NotificationChannel(CHANNEL_ID,name,importance);
notificationChannel.setDescription(description);
NotificationManager
notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVIC
E);
notificationManager.createNotificationChannel(notificationChannel);
}
}
}
Output:-
Practical No.:- 9
Aim :- Threads
→ MainActivity.java
package com.example.hello.progressbarthread;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import java.util.*;
public class MainActivity extends AppCompatActivity {
private ProgressBar bar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bar=findViewById(R.id.progress);
}
public void startProgress(View view){
bar.setProgress(0);
new Thread(new Task()).start();
}
private class Task implements Runnable {
@Override
public void run(){
for(int i=0;i<=10;i++){
final int value=i;
try{
Thread.sleep(1000);
}catch (InterruptedException e){
.printStackTrace();
}
bar.setProgress(value);
}
}
}
}
→ activity_main.xml