0% found this document useful (0 votes)
16 views71 pages

ITSM Journal 1

Uploaded by

lucky31dubey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views71 pages

ITSM Journal 1

Uploaded by

lucky31dubey
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 71

Practical No.

:- 1

Aim:- Using Android Studio Create Hello World Program.


Step 1: Create New Project in Android Studio
Open Android Studio and go to the File Menu >> New >> New Project .
Step 2: Configure Your New Project
Add your application details over here and hit "Next" button.
i.e.
• Application name
• Company domain
• Package name
• Project location
Step 3: Target Android Devices Select the form factors your app will be run on. Here, you can
select device type for your android application. And at the same time, you select Minimum
SDK which will be supported by your android application. When you select Minimum SDK,
Android studio will give you some active android devices percentage that will support your
android application. Otherwise, you can also click on "Help me choose" button for more
information about android platform versions.

Hit "Next" button once you are done.


Step 4: Add activity
This screen suggests you add any activity at first. If you don’t want to add any activity at
starting, you can select Add no activity option. If you have selected Phone and Tablet in option
in the last screen then it will display the only phone related suggestions. Select Activity as per
your project requirements.
For example, select Google Maps Activity for google map project, Blank Activity for blank
project etc.

If you are done with it, click "Next" button.


Step 5: Customize the Activity
Here, you can customize your selected activity. You can change your Activity name from here.
And also you can change Layout name (that will be attached to your activity), Title (Title of
the activity), Menu Resource Name (Menu’s resource file name).
If you want to add fragment to your activity then select Use a fragment option given below.
Program:-

→ 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

Aim :- Android Resources: (Color, Theme, String, Drawable, Dimension,


Image)
Color:-

→ 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

<?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">

<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

A :- Create an android application that display a welcome message on the click


of button. In this program show the various stages of the lifecycle of an activity.
→ 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="197dp"
android:layout_height="67dp"
android:text="Click Here"
android:onClick="MessageButton"
tools:layout_editor_absoluteX="107dp"
tools:layout_editor_absoluteY="222dp"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>

→ 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

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


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BlankFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Check Log Cat" />
</FrameLayout>

Output:-
Practical No.:- 4

A :- Program related to different layouts.


Create Activity
Right Click on app -> New -> Activity -> Empty Activity
Activity Name = RelativeActivity
Layout Name= activity_relative
App -> res -> layout -> activity_relative.xml
In activity_relative.xml
Write Following code in App -> res -> layout -> activity_relative.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=".RelativeActivity">
<TextView
android:id="@+id/lblComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Comments"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
<EditText
android:id="@+id/txtComments"
android:layout_width="fill_parent"
android:layout_height="170px"
android:textSize="18sp"
android:layout_alignLeft="@+id/lblComments"
android:layout_below="@+id/lblComments"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/btnSave"
android:layout_width="175px"
android:layout_height="wrap_content"
android:text="Save"
android:layout_below="@+id/txtComments"
android:layout_alignRight="@+id/txtComments" />
<Button
android:id="@+id/btnCancle"
android:layout_width="210px"
android:layout_height="wrap_content"
android:text="Cancle"
android:layout_below="@+id/txtComments"
android:layout_alignRight="@+id/txtComments" />
</RelativeLayout>
→ 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="Layout Demonstration"
android:layout_gravity="center"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Relative Activity"
android:layout_gravity="center"
android:id="@+id/btnrelative"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
→ 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);
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

<?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" />
</LinearLayout>
Output:-
Similarly, create a new Activity and its corresponding layout
Activity Name = LinearVerticalActivity
Layout Name = actibity_linear_vertical.
In activity_linear_vertical.xml
App -> res -> layout -> sctivity_linear_vertical.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=".LinearVerticalActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LinearLayoutVertical" />
<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);
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

B :- Create an android application using gridview layout and insert images of


different animals a the toast the animal namely clicking the image.

→ 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

C :- Create a table of button. On clicking the button it toast should appear


mentioning on button
clicked.
→ 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">
<TableLayout
android:layout_width="409dp"
android:layout_height="729dp"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/b1"
android:text="Button 1" />
<Button
android:id="@+id/b2"
android:text="Button 2" />
<Button
android:id="@+id/b3"
android:text="Button 3" />
</TableRow>
</TableLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
→ MainActivity.java
package com.example.tablelayout;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1=(Button)findViewById(R.id.b1);
Button b2=(Button)findViewById(R.id.b2);
Button b3=(Button)findViewById(R.id.b3);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Button 1 Clicked",
Toast.LENGTH_SHORT).show();
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Button 2 Clicked",
Toast.LENGTH_SHORT).show();
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Button 3 Clicked",
Toast.LENGTH_SHORT).show();
}
});
}
}
Output:-
Practical No.:-5

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.

→ Create New xml file for menu.

→ 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

B :- Create an android application to display alert dialog on pressing the Back


Button.

→ 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

Aim :- Create an android application to generate notification on button-click.

→ 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

<?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">
<ProgressBar
android:id="@+id/progress"
style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="false"
android:max="10"
android:padding="4dp"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Progress"
android:onClick="startProgress"/>
</LinearLayout>
Output:-

You might also like