[go: up one dir, main page]

0% found this document useful (0 votes)
177 views61 pages

Mobile Application Development Lab Manual

Android development using kotlin

Uploaded by

tannazmuni
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)
177 views61 pages

Mobile Application Development Lab Manual

Android development using kotlin

Uploaded by

tannazmuni
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/ 61

LAB MANUAL

OF
MOBILE APPLICATION DEVELOPMENT

COMPUTER ENGINEERING DEPARTMENT


MOBILEAPPLICATION DEV ELOPMENT

Practical: -1
Installation and setup of java development kit (JDK), setup android SDK, setup eclipse IDE,
setup android development tools (ADT) plugins, create android virtual device.

Environment setup for Android apps Development


To setup an environment for the Android application development, you will need to download and
install the following:
• Java Development Kit (JDK) 5 or 6
• The Android SDK
• Eclipse
Step 1: Download and Install JDK
The Android SDK makes use of the Java SE Development Kit (JDK). If your computer does not have
the JDK installed, you should start by downloading it from:
www.oracle.com/technetwork/java/javase/downloads/index.html and installing it prior to
moving to the next section.

Figure 1

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

Step 2: Download Android Development Environment


There are a number of ways to develop Android apps. The easiest way to get started quickly is to use
the ADT Bundle developed by Google. The ADT Bundle provides a number of tools that are essential
to building android apps.

To download ADT bundle go to https://developer.android.com/sdk/index.html. On this web page


click on the button "Download the SDK" as shown in below figure.

Figure 2

When you click on this button, it will ask to agree on terms and condition, now click the checkbox
agreeing to the terms and conditions and select either 32-bit or 64-bit, depending on your computer.
After you have agreed and selected your system type click on the "Download the SDK ADT Bundle
for Windows" button and the download will start. The downloaded file is a .zip file

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

Figure 3

Step 3: Installing the ADT bundle


Once downloading of the ADT Bundle has completed, Extract all the files to a location on your
computer. You will find two folders named eclipse and sdk and an application called SDK Manager
as shown in below figure.

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

Figure 4

Now you are going to open up the environment where you will be developing android applications
by click on eclipse folder. You will now see multiple folders and files as sown in below figure. You
need to double click on eclipse.exe to open the eclipse. Once eclipses get started, you can start
building your first application.

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

Practical: - 2
A. Create “hello world” application. That will display “Hello World” in the middle of the
screen in the black color with wheat color or white background.
B. create hello world using java

HelloMainActivity.java
package com.example.raj.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

Activity_hello_main.xml

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#fafad2"
tools:context="com.example.raj.myapplication.MainActivity">

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Hello World"
android:textSize="30sp"
android:textColor="#B22222"
android:layout_centerVertical="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="99dp" />
</RelativeLayout>

Output:

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

B. Create hello world using java

package com.example.raj.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView text = new TextView(this);


text.setText("Hello World");
setContentView(text);
}
}
Output:

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

Practical: -3
Create an Android App Which Has One Activity to get two numbers N1 and N2. On pressing
Button has four operations Add, Multiply, Divide, Subtract, on select any one operation it will
Display Result in Text View of Activity

Activity_main.xml

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


<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#fafad2"
tools:context="com.example.raj.myapplication.MainActivity"
android:weightSum="1">

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="No 1:"
android:id="@+id/tv1" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etno1"

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

android:ems="10"
android:hint="Enter No1"
android:inputType="number"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="No 2:"
android:id="@+id/tv2" />

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/etno2"
android:ems="10"
android:hint="Enter No2"
android:inputType="number"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Result is"
android:id="@+id/tv3"
android:layout_gravity="center" />

<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

<Button
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="+"
android:id="@+id/Add" />

<Button
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="-"
android:id="@+id/Sub" />

<Button
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="*"
android:id="@+id/Mul" />

<Button
android:layout_width="70dp"
android:layout_height="wrap_content"
android:text="/"
android:id="@+id/Div" />
</LinearLayout>

</LinearLayout>

</LinearLayout>

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

MainActivity.java

package com.example.raj.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

TextView tv1, tv2, Result;


EditText et1, et2;
Button Add, Sub, Mul, Div;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

et1 = (EditText)findViewById(R.id.etno1);
et2 = (EditText)findViewById(R.id.etno2);
Result = (TextView)findViewById(R.id.tv3);
Add = (Button)findViewById(R.id.Add);
Sub = (Button)findViewById(R.id.Sub);
Mul = (Button)findViewById(R.id.Mul);
Div = (Button)findViewById(R.id.Div);

Add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO Auto-Generated Method...
if(et1.getText().toString().equals(""))
{

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

et1.setError("Please fill the No1");


}
else if(et2.getText().toString().equals(""))
{
et2.setError("Please fill the No2");
}
else
{
int a = Integer.parseInt(et1.getText().toString());
int b = Integer.parseInt(et2.getText().toString());
int c = a + b;
Result.setText("Answer="+c);
}
}
});

Sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO Auto-Generated Method...

if(et1.getText().toString().equals(""))
{
et1.setError("Please fill the No1");
}
else if(et2.getText().toString().equals(""))
{
et2.setError("Please fill the No2");
}
else
{
int a = Integer.parseInt(et1.getText().toString());
int b = Integer.parseInt(et2.getText().toString());
int c = a - b;
Result.setText("Answer="+c);
DEPARTMENT OF COMPUTER ENGINEERING
MOBILEAPPLICATION DEV ELOPMENT
}

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

}
});

Mul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO Auto-Generated Method...

if(et1.getText().toString().equals(""))
{
et1.setError("Please fill the No1");
}
else if(et2.getText().toString().equals(""))
{
et2.setError("Please fill the No2");
}
else
{
int a = Integer.parseInt(et1.getText().toString());
int b = Integer.parseInt(et2.getText().toString());
int c = a * b;
Result.setText("Answer="+c);
}
}
});

Div.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO Auto-Generated Method...

if(et1.getText().toString().equals(""))
{
et1.setError("Please fill the No1");
}
else if(et2.getText().toString().equals(""))
DEPARTMENT OF COMPUTER ENGINEERING
MOBILEAPPLICATION DEV ELOPMENT
{

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

et2.setError("Please fill the No2");


}
else
{
int a = Integer.parseInt(et1.getText().toString());
int b = Integer.parseInt(et2.getText().toString());
int c = a / b;
Result.setText("Answer="+c);
}
}
});

}
}

OUTPUT:

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

Practical: -4

Create application for demonstration of android activity life cycle.

Activity_life_cycle.xml

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.raj.lifecycle.LifeCycle">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Android Activity Life Cycle"
android:id="@+id/tv1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>

LifeCycle.java

package com.example.raj.lifecycle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

public class LifeCycle extends AppCompatActivity {

TextView tv1;
String text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_life_cycle);

tv1 = (TextView)findViewById(R.id.tv1);
text = tv1.getText().toString();
text = text + "\n InsideCreate Method";
tv1.setText(text);
}
public void onStart()
{
super.onStart();
text = tv1.getText().toString();
text = text + "\n InsideStart Method";
tv1.setText(text);
}
public void onRestart()
{
super.onRestart();
text = tv1.getText().toString();
text = text + "\n InsideRestart Method";
tv1.setText(text);
}
public void onResume()
{
super.onResume();
text = tv1.getText().toString();
text = text + "\n InsideResume Method";
tv1.setText(text);
}
DEPARTMENT OF COMPUTER ENGINEERING
MOBILEAPPLICATION DEV ELOPMENT

public void onPause()


{
super.onPause();
text = tv1.getText().toString();
text = text + "\n InsidePause Method";
tv1.setText(text);
}
public void onStop()
{
super.onStop();
text = tv1.getText().toString();
text = text + "\n InsideStop Method";
tv1.setText(text);
}
public void onDestroy()
{
super.onDestroy();
text = tv1.getText().toString();
text = text + "\n InsideDestroy Method";
tv1.setText(text);
}
}
OUTPUT:

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

Practical: -5

Create registration page to demonstrate of basic widget available in android.

Activity_widgetdemo.xml

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.raj.widgetdemo.widgetdemo">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Registration"
android:id="@+id/tv1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Name:"
android:id="@+id/tv2"
android:layout_below="@+id/tv1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="37dp" />

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Mobile No:"
android:id="@+id/tv3"
android:layout_below="@+id/tv2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="39dp" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/et1"
android:layout_above="@+id/tv3"
android:layout_alignLeft="@+id/tv1"
android:layout_alignStart="@+id/tv1"
android:ems="10"
android:hint="name"/>

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/et2"
android:layout_alignTop="@+id/tv3"
android:layout_alignLeft="@+id/et1"
android:layout_alignStart="@+id/et1"
android:ems="10"
android:hint="mobile no"/>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
DEPARTMENT OF COMPUTER ENGINEERING
MOBILEAPPLICATION DEV ELOPMENT

android:text="Cast:"
android:id="@+id/tv4"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/et2"
android:layout_alignLeft="@+id/et2"
android:layout_alignStart="@+id/et2"
android:id="@+id/radioGroup">

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="General"
android:id="@+id/rb1"
android:checked="false" />

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SEBC"
android:id="@+id/rb2"
android:checked="false" />

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SC"
android:id="@+id/rb3"
android:checked="false" />

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ST"
android:id="@+id/rb4"
android:checked="false" />

</RadioGroup>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Language:"
android:id="@+id/tv5"
android:layout_below="@+id/radioGroup"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="37dp" />

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gujarati"
android:id="@+id/chk1"
android:checked="false"
android:layout_alignBottom="@+id/tv5"
android:layout_alignLeft="@+id/btn"
android:layout_alignStart="@+id/btn" />

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hindi"
android:id="@+id/chk2"
DEPARTMENT OF COMPUTER ENGINEERING
MOBILEAPPLICATION DEV ELOPMENT

android:checked="false"
android:layout_alignTop="@+id/chk1"
android:layout_toRightOf="@+id/tv1"
android:layout_toEndOf="@+id/tv1" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SUBMIT"
android:id="@+id/btn"
android:layout_alignLeft="@+id/radioGroup"
android:layout_alignStart="@+id/radioGroup"
android:layout_below="@+id/chk1"
android:onClick="submit"/>

</RelativeLayout>

widgetdemo.java

package com.example.raj.widgetdemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class widgetdemo extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_widgetdemo);
}
public void submit(View v)
{

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

Toast.makeText(this,"You have Registered successfully.",Toast.LENGTH_LONG).show();


}
}

OUTPUT:

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

Practical: -6

Create sample application with login module. (Check username and password) on successful
login, change Text view “Login Successful.” And on failing login, alert user using Toast “Login
fail.”
Activity_login.xml

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


<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.raj.loginpage.login"
android:background="#f0dfc9">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Login Page"
android:id="@+id/tv1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="37dp"
android:textColor="#116a1a"
android:textStyle="bold" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="UserID:"
DEPARTMENT OF COMPUTER ENGINEERING
MOBILEAPPLICATION DEV ELOPMENT

android:id="@+id/tv2"
android:layout_below="@+id/tv1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="55dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Pass:"
android:id="@+id/tv3"
android:layout_below="@+id/tv2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="43dp" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/et1"
android:ems="10"
android:hint="Enter User id"
android:layout_alignBottom="@+id/tv2"
android:layout_alignLeft="@+id/tv1"
android:layout_alignStart="@+id/tv1" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:hint="Enter Password"
android:numeric="integer"
android:id="@+id/et2"
android:layout_alignBottom="@+id/tv3"
DEPARTMENT OF COMPUTER ENGINEERING
MOBILEAPPLICATION DEV ELOPMENT

android:layout_alignLeft="@+id/et1"
android:layout_alignStart="@+id/et1" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:id="@+id/btn"
android:layout_below="@+id/et2"
android:layout_centerHorizontal="true"
android:layout_marginTop="37dp"
android:background="@android:color/darker_gray" />

</RelativeLayout>

login.java

package com.example.raj.loginpage;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class login extends AppCompatActivity {


EditText et1,et2;
Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
et1 = (EditText)findViewById(R.id.et1);
et2 = (EditText)findViewById(R.id.et2);

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

btn1 = (Button)findViewById(R.id.btn);

btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String id = et1.getText().toString();
String pass = et2.getText().toString();

if(id.equals("abc")&& pass.equals("123"))
{
Toast.makeText(getApplicationContext(),"You have Login Successfully”,
Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(),"Invalid ID and Password”,
Toast.LENGTH_LONG).show();
}
}
});
}
}

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

OUTPUT:

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

Practical: -7

Create an Android App which has one activity to get Fern hit or Celsius. On Pressing “Convert”
button it shows the Result will be displayed on Text View of Activity.

Activity_fernhit.xml

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.raj.fernhit.Fernhit">

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/et1"
android:ems="15"
android:hint="Enter Value"
android:layout_marginTop="52dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/tvResult"
android:text="Result"
android:layout_below="@+id/et1"
android:layout_alignLeft="@+id/et1"

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

android:layout_alignStart="@+id/et1" />

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Celcius"
android:id="@+id/cb"
android:layout_below="@+id/tvResult"
android:layout_alignLeft="@+id/tvResult"
android:layout_alignStart="@+id/tvResult"
android:layout_marginTop="45dp"
android:checked="false" />

<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fahrenhit"
android:id="@+id/fb"
android:checked="false"
android:layout_below="@+id/cb"
android:layout_alignLeft="@+id/cb"
android:layout_alignStart="@+id/cb" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Convert"
android:id="@+id/btn1"
android:onClick="submit"
android:layout_below="@+id/fb"
android:layout_centerHorizontal="true" />

</RelativeLayout>

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

Fernhit.java

package com.example.raj.fernhit;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

public class Fernhit extends AppCompatActivity {


EditText et1;
TextView tv1Result;
RadioButton cb,fb;
Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fernhit);
}
public void submit(View v)
{
et1 = (EditText)findViewById(R.id.et1);
tv1Result = (TextView)findViewById(R.id.tvResult);
double a = Double.parseDouble(String.valueOf(et1.getText()));
cb = (RadioButton)findViewById(R.id.cb);
fb = (RadioButton)findViewById(R.id.fb);

if(cb.isChecked())
{
tv1Result.setText(f2c(a)+"Degree C");
fb.setChecked(false);
}
else
{

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

tv1Result.setText(c2f(a)+"Degree F");
cb.setChecked(false);
}
}
private double c2f(double c)
{
return (c*9)/5+32;
}
private double f2c(double f)
{
return (f-32)*5/9;
}
}
OUTPUT:

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

Practical: -8
Create application for demonstrate of second activity using intent.

Activity_main.xml

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.raj.myapplication.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Show First Activity"
android:id="@+id/tv1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Second Activity"
android:id="@+id/btn1"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="Show"/>

</RelativeLayout>

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

MainActivity.java

package com.example.raj.myapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Show(View v)
{
Intent i = new Intent(this,Main2Activity.class);
startActivity(i);
}
}

Activity_main2.xml

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.raj.myapplication.Main2Activity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Show Second Activity"
android:id="@+id/tv1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>

Main2Activity.java

package com.example.raj.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Main2Activity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}

OUTPUT:

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

Practical: -9

Create application for demonstrate of data passing between First activity to second activity
using intent.

Activity_main.xml

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.raj.myapplication.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Show First Activity"
android:id="@+id/tv1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/et1"
android:hint="Enter Message"
android:layout_below="@+id/tv1"
android:layout_marginTop="64dp"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true" />

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Second Activity"
android:id="@+id/btn1n"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="Show"/>
</RelativeLayout>

MainActivity.java

package com.example.raj.myapplication;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {


EditText et1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Show(View v)
{
et1 = (EditText)findViewById(R.id.et1);
String text = et1.getText().toString();
Intent i = new Intent(this, Main2Activity.class);
i.putExtra("text",text);
startActivity(i);

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

}
}

Activity_main2.xml

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.raj.myapplication.Main2Activity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Show Second Activity"
android:id="@+id/tv1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/tv2"
android:layout_below="@+id/tv1"
android:layout_alignLeft="@+id/tv1"
android:layout_marginTop="77dp" />

</RelativeLayout>

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

Main2Activity.java

package com.example.raj.myapplication;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class Main2Activity extends ActionBarActivity {


TextView tv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

Intent i = getIntent();
String text = i.getStringExtra("text");
tv2 = (TextView)findViewById(R.id.tv2);
tv2.setText(text);
}
}

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

OUTPUT:

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

Practical: -10

Create application for demonstrate of call Built-in application for (ACTION_VIEW &
ACTION_DIAL) activity using intent.

Activity_main.xml

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


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.raj.google.Google">

<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/imageButton"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="@mipmap/ic_launcher_google1"
android:onClick="Google"/>

<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/imageButton2"
android:layout_alignTop="@+id/imageButton"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="@mipmap/ic_launcher_dial"

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

android:onClick="Dial"/>

</RelativeLayout>

MainActivity.java

package com.example.raj.google;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class Google extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_google);
}
public void Google(View v)
{
Intent i = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.google.com"));
startActivity(i);
}
public void Dial(View v)
{
Intent i = new Intent(Intent.ACTION_DIAL,Uri.parse("tel:+91"));
startActivity(i);
}
}

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

OUTPUT:

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

Practical: -11

Create an application for demonstrate of toggle button in android.

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

<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tg1"
android:layout_marginLeft="60dp"
android:layout_marginTop="100dp"
android:textOn="ON"
android:textOff="OFF"
android:checked="true"/>

<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tg2"
android:layout_marginTop="100dp"
android:layout_toRightOf="@+id/tg1"
android:textOn="ON"
android:textOff="OFF"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

android:id="@+id/btn"
android:text="Button"
android:layout_marginLeft="100dp"
android:layout_marginTop="150dp"/>

</RelativeLayout>

MainActivity.java

package com.example.togglebutton;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final ToggleButton tg1 = (ToggleButton)findViewById(R.id.tg1);


final ToggleButton tg2 = (ToggleButton) findViewById(R.id.tg2);
Button btn = (Button) findViewById(R.id.btn);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(),"ToggleButton 1 -"
+tg1.getText().toString()+"\n"+"ToggleButton 2 -"
+tg2.getText().toString(),Toast.LENGTH_LONG).show();

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

}
});
}
}

OUTPUT:

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

Practical: -12

Create an application for demonstrate of spinner view in android.

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"
tools:ignore="ExtraText">

<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner1"
android:layout_centerHorizontal="true"
android:layout_marginTop="85dp"
android:background="#ed5072"/>

</RelativeLayout>

MainActivity.java

package com.example.spiner;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
DEPARTMENT OF COMPUTER ENGINEERING
MOBILEAPPLICATION DEV ELOPMENT

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {


String[]country = {"india","Japan","USA","Other"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner sp = (Spinner)findViewById(R.id.spinner1);
sp.setOnItemSelectedListener(this);

ArrayAdapter aa = new ArrayAdapter(this, android.R.layout.simple_spinner_item,country);


aa.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
sp.setAdapter(aa);
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getApplicationContext(),country[i], Toast.LENGTH_LONG).show();
}
}
OUTPUT:

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

Practical: -13

Crate an application for demonstrate of Progress bar in android.


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:id="@+id/l1"
android:orientation="vertical"
tools:context=".MainActivity">

<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:layout_gravity="center"
android:id="@+id/p1"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="Start"
android:textStyle="bold"
android:textSize="25dp"
android:layout_gravity="center"/>

</LinearLayout>

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

MainActivity.java

package com.example.progressbar;

import static android.graphics.Color.BLACK;


import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {


Button btn;
ProgressBar simpleprogressbar;
LinearLayout l1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
simpleprogressbar = (ProgressBar) findViewById(R.id.p1);

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
simpleprogressbar.setVisibility(View.VISIBLE);
}
});
}
}

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

OUTPUT:

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

Practical: -14

Create an application to demonstrate option menu in android.


Activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.raju.option.MainActivity"
tools:showIn="@layout/activity_main"
android:id="@+id/r1">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />

<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:id="@+id/toolbar">
</android.support.v7.widget.Toolbar>
</RelativeLayout>

Menu_main.xml

<menu 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"
tools:context="com.example.raju.option.MainActivity">

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never" />

<item android:id="@+id/i1"
android:title="item1"/>
<item android:id="@+id/i2"
android:title="item2"/>
<item android:id="@+id/i3"
android:title="item3">
</item>
</menu>

MainActivity.JAva

package com.example.raju.option;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import static android.graphics.Color.BLUE;

public class MainActivity extends AppCompatActivity {


RelativeLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DEPARTMENT OF COMPUTER ENGINEERING
MOBILEAPPLICATION DEV ELOPMENT

layout = (RelativeLayout)findViewById(R.id.r1);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id)
{
case R.id.i1:
layout.setBackgroundColor(Color.BLUE);
return true;
case R.id.i2:
layout.setBackgroundColor(Color.YELLOW);
return true;
case R.id.i3:
layout.setBackgroundColor(Color.GREEN);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

OUTPUT:

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

Practical: -15

Create an application to demonstrate context menu in android.


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:id="@+id/l1"
android:orientation="vertical"
tools:context=".MainActivity">

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn1"
android:text="Click"
android:textSize="35dp"
android:layout_gravity="center"/>

</LinearLayout>

Menu_main.xml

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


<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/item1"
android:title="Green"/>
<item android:id="@+id/item2"
android:title="Blue"/>
<item android:id="@+id/item3"
android:title="Red"/>

</menu>

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

MainActivity.JAva

package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {


LinearLayout l1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btn = (Button) findViewById(R.id.btn1);


registerForContextMenu(btn);
l1 = (LinearLayout) findViewById(R.id.l1);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v
, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater menuInflater = getMenuInflater();
getMenuInflater().inflate(R.menu.menu_file,menu);
}
@Override
public boolean onContextItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();

DEPARTMENT OF COMPUTER ENGINEERING


MOBILEAPPLICATION DEV ELOPMENT

switch (id)
{
case R.id.item1:
l1.setBackgroundColor(Color.GREEN);
return true;
case R.id.item2:
l1.setBackgroundColor(Color.BLUE);
return true;
case R.id.item3:
l1.setBackgroundColor(Color.RED);
return true;
default:
return super.onContextItemSelected(item);
}
}
}
OUTPUT:

DEPARTMENT OF COMPUTER ENGINEERING

You might also like