0% found this document useful (0 votes)
60 views33 pages

AMP2

The document describes 6 practical Android projects: 1. Uses various event listeners like key, touch, click, and long click listeners to display Toast messages. 2. Changes an image resource on a button click. 3. Displays a list of 10 items using a ListView and handles item clicks. 4. Implements a spinner to display and select menu items, displaying the selection in a Toast. 5. Checks if airplane mode is on or off and displays the status. 6. Builds a basic calculator app that takes two numbers and can perform addition, subtraction, division, multiplication, percentage calculation, and clear functions.
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)
60 views33 pages

AMP2

The document describes 6 practical Android projects: 1. Uses various event listeners like key, touch, click, and long click listeners to display Toast messages. 2. Changes an image resource on a button click. 3. Displays a list of 10 items using a ListView and handles item clicks. 4. Implements a spinner to display and select menu items, displaying the selection in a Toast. 5. Checks if airplane mode is on or off and displays the status. 6. Builds a basic calculator app that takes two numbers and can perform addition, subtraction, division, multiplication, percentage calculation, and clear functions.
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/ 33

PRACTICAL 1

Event Listener – key listener, touch listener, on click listener,


Long listener
Design:
Code:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ImageView img = findViewById(R.id.img);


Button btn = findViewById(R.id.btn);
EditText txt = findViewById(R.id.txt);
txt.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {

if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode ==


KeyEvent.KEYCODE_ENTER){
Toast.makeText(MainActivity.this, "" +
txt.getText().toString(), Toast.LENGTH_SHORT).show();
return false;
}
return false;
}
});
img.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Toast.makeText(getApplicationContext(), "Img has been
Touched", Toast.LENGTH_SHORT).show();

return false;
}
});

btn.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
Toast.makeText(getApplicationContext(), "Long clicked has
been pressed", Toast.LENGTH_SHORT).show();
return false;
}
});

btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "the Button has
been clicked", Toast.LENGTH_SHORT).show();
}
});
}
}
Output :
PRACTICAL 2
Image change on button click
Design:
Code :
public class MainActivity extends AppCompatActivity {

public void next(View view){

ImageView f1 = (ImageView) findViewById(R.id.f1);


f1.setImageResource(R.drawable.f2);

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Output :
PRACTICAL 3
Display 10 List Items by ListView
Design

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


<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</RelativeLayout>
Code:
public class MainActivity extends AppCompatActivity {

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

ListView listView = findViewById(R.id.listview);

List<String> list = new ArrayList<>();


list.add("dog");
list.add("cat");
list.add("cow");
list.add("ox");
list.add("goat");
list.add("monkey");
list.add("lion");
list.add("tiger");
list.add("rabbit");
list.add("pig");

ArrayAdapter arrayAdapter = new


ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1,
list);
listView.setAdapter(arrayAdapter);

listView.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {

String text =
parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(),text,
Toast.LENGTH_SHORT).show();

}
});

}
}
Output:
PRACTICAL 4
Display Menu Item and display selected Item
Design:

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

<Spinner
android:id="@+id/spinner"
android:layout_width="221dp"
android:layout_height="27dp"
android:spinnerMode="dialog"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.068" />
</androidx.constraintlayout.widget.ConstraintLayout>
Code:
App >> res >> values >>String.xml
<resources>
<string name="app_name">menuitem2</string>
<string-array name="games">
<item>One</item>
<item>two</item>
<item>Three</item>
<item>four</item>
<item>Fifth</item>
<item>Six</item>
</string-array>
</resources>

MainActivity.java
public class MainActivity extends AppCompatActivity implements
AdapterView.OnItemSelectedListener {

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

Spinner spinner = findViewById(R.id.spinner);


ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this, R.array.games,
android.R.layout.simple_spinner_item);

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_it
em);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}

@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int
i, long l) {
String text = adapterView.getItemAtPosition(i).toString();
Toast.makeText(adapterView.getContext(),text,
Toast.LENGTH_SHORT).show();

@Override
public void onNothingSelected(AdapterView<?> adapterView) {

}
}
Output:
PRACTICAL 5
Check Wheather Airplane Mode status is On or Off

Design:
<?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:id="@+id/textView"
android:layout_width="239dp"
android:layout_height="72dp"
android:text="Hello World!"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="64dp"
android:layout_marginTop="100dp"
android:onClick="buttonCheckAirplaneModeStatus"
android:text="Check Airplane Mode Status"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
Code :
public class MainActivity extends AppCompatActivity {

private TextView textView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

textView = findViewById(R.id.textView);

public void buttonCheckAirplaneModeStatus(View view){

if (Settings.Global.getInt(this.getContentResolver(),

Settings.Global.AIRPLANE_MODE_ON, 0) != 0){

textView.setText("Airplane Mode is ON");

else {

textView.setText("Airplane Mode is OFF");

}
Output :
PRACTICAL 6
Calculator to take two number and perform Addition, Subtraction, Division,
Multiplication, Percentage, And Clear button

Design :
public class MainActivity extends AppCompatActivity {
EditText etNum1,etNum2;
TextView txtAns;
Button btnAdd,btnSub,btnDiv,btnMul,btnPer,btnClr;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etNum1=findViewById(R.id.etNum1);
etNum2=findViewById(R.id.etNum2);
txtAns=findViewById(R.id.txtAns);
btnAdd=findViewById(R.id.btnAdd);
btnSub=findViewById(R.id.btnSub);
btnDiv=findViewById(R.id.btnDiv);
btnMul=findViewById(R.id.btnMul);
btnPer=findViewById(R.id.btnPer);
btnClr=findViewById(R.id.btnClr);

btnAdd.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
String n1 = etNum1.getText().toString();
String n2 = etNum2.getText().toString();
int ans=Integer.parseInt(n1)+Integer.parseInt(n2);
txtAns.setText(String.valueOf(ans));
}
});
btnSub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String n1 = etNum1.getText().toString();
String n2 = etNum2.getText().toString();
int ans=Integer.parseInt(n1)-Integer.parseInt(n2);
txtAns.setText(String.valueOf(ans));
}
});
btnDiv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String n1 = etNum1.getText().toString();
String n2 = etNum2.getText().toString();
int ans=Integer.parseInt(n1)/Integer.parseInt(n2);
txtAns.setText(String.valueOf(ans));
}
});
btnMul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String n1 = etNum1.getText().toString();
String n2 = etNum2.getText().toString();
int ans=Integer.parseInt(n1)*Integer.parseInt(n2);
txtAns.setText(String.valueOf(ans));
}
});
btnPer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String n1 = etNum1.getText().toString();
String n2 = etNum2.getText().toString();
int ans=((Integer.parseInt(n1)*100)/Integer.parseInt(n2));
txtAns.setText(String.valueOf(ans));
}
});
btnClr.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
etNum1.setText("");
etNum2.setText("");
}
});
}
}
PRACTICAL 7
Programming Android Resources (Color, Theme, String, Drawable, Dimension, Image)
Display an Image

Display and Output


PRACTICAL 8
Create the media API in android to play an audio file.
Adding audio files to raw folder
MainActivity.kt

package com.example.mp3song

import android.media.MediaPlayer
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.*

class MainActivity : AppCompatActivity() {


internal lateinit var listView: ListView
internal lateinit var list: MutableList<String>
internal lateinit var adapter: ListAdapter
internal var mediaPlayer: MediaPlayer? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
listView = findViewById<View>(R.id.ListView) as ListView
list = ArrayList()
val fields = R.raw ::class.java!!.getFields()
for(i in fields.indices) {

list.add(fields[i].getName())

}
adapter = ArrayAdapter(this,android.R.layout.simple_list_item_1,list)
listView.adapter = adapter
listView.onItemClickListener = AdapterView.OnItemClickListener{adapterView, view,
i, l ->

if(mediaPlayer != null)
{
mediaPlayer!!.release()
}
val singh = resources.getIdentifier(list[i],"raw", packageName)
mediaPlayer = MediaPlayer.create(this,singh)
mediaPlayer!!.start()
}
}
}

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">
<ListView
android:id="@+id/ListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
Output
PRACTICAL 9
Create an android application to pass the data from current Application(Activity) to
another Application(Activity) using intent.

Design
MainActivity.java
package com.example.ribika_faltu;

import androidx.activity.result.contract.ActivityResultContracts;
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 {


EditText name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = findViewById(R.id.btn1);
name=findViewById(R.id.name);

btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SwitchActivity();
}
});
}
private void SwitchActivity(){
Intent si= new Intent(this, Activity2.class);
si.putExtra("name", name.getText().toString());
startActivity(si);
}
}
Activity2.java
package com.example.ribika_faltu;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Activity2 extends AppCompatActivity {

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

Button btn2= findViewById(R.id.btn2);


TextView txt3= findViewById(R.id.txt3);

Intent i=getIntent();
String fname= i.getStringExtra("name");
txt3.setText("Get lost " + fname);

btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SwitchActivity();
}
});
}
private void SwitchActivity(){
Intent ti= new Intent(this, MainActivity.class);
startActivity(ti);
}
}
Output
PRACTICAL 10
Create an android application to generate notification

MainActivity.java
Output
PRACTICAL 11
Create an android application to display Alert Dialog on pressing the Back Button

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

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press The Back Button of Your Phone"
android:textStyle="bold"
android:textSize="30dp"
android:gravity="center_horizontal"
android:layout_marginTop="180dp"
/>

</RelativeLayout>

MainActivity.java

package com.example.practical12;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onBackPressed(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setMessage("Do you want to exit?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});

AlertDialog alert= builder.create();


alert.show();
}
}

Output

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy