Vishal Yadav 1000017720

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 25

LIST OF PRACTICAL

1. Create an android application with login module (Check username and

password) on successful login alert to user using toast message.

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

TextView tv1 , tv2;

EditText et1 , et2;

Button bt1;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

tv1 = (TextView) findViewById(R.id.textview2);

tv2 = (TextView) findViewById(R.id.textview4);

et1 = (EditView) findViewById(R.id.editTextTextPersonName);

et2 = (EditView) findViewById(R.id.editTextTextPersonName2);

public void display(View view) {

if (et1.getText().tostring().equals("abc") && et2..getText().toString().equals("xyz")) {


tv2.settext("Login successful");

} else {

Tost.makeText(this, "Login failed", Toast.LENGTH_LONG).show();

tv1.setText(et1.getText().toString());

tv2.setText(et2.getText().toString());

2. Develop an android app for performing some basic operations such as


addition, subtraction, multiplication, division, etc

package com.example.calculator;

import android.support.annotation.Nullable;

import android.support.annotation.StyleRes;

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 {

private EditText editTextNumber1, editTextNumber2;

private Button buttonAdd, buttonSubtract, buttonMultiply, buttonDivide;

private TextView textViewResult;

@Override

protected void onCreate(@Nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

editTextNumber1 = findViewById(R.id.editTextNumber1);

editTextNumber2 = findViewById(R.id.editTextNumber2);

buttonAdd = findViewById(R.id.buttonAdd);

buttonSubtract = findViewById(R.id.buttonSubtract);

buttonMultiply = findViewById(R.id.buttonMultiply);
buttonDivide = findViewById(R.id.buttonDivide);

textViewResult = findViewById(R.id.textViewResult);

buttonAdd.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

performOperation('+');

});

buttonSubtract.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

performOperation('-');

});

buttonMultiply.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

performOperation('*');

});

buttonDivide.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {


performOperation('/');

});

private void performOperation(char operation) {

double num1 = Double.parseDouble(editTextNumber1.getText().toString());

double num2 = Double.parseDouble(editTextNumber2.getText().toString());

double result = 0;

switch (operation) {

case '+':

result = num1 + num2;

break;

case '-':

result = num1 - num2;

break;

case '*':

result = num1 * num2;

break;

case '/':

if (num2 != 0) {

result = num1 / num2;

} else {

textViewResult.setText("Error: Cannot divide by zero");


return;

break;

textViewResult.setText("Result: " + result);

3. Create an Android application that takes input from the user and displays the

user’s input on another activity.

1.MainAcitivity.java

package com.example.sahilkumar;

import android.content.Intent;
import android.os.Bundle; import android.view.View; import

android.widget.Button; import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends


AppCompatActivity{

EditText editTextFirstName, editTextEmail;

Button buttonSubmit;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

editTextFirstName = findViewById(R.id.inputeditTextFirstName);

editTextEmail = findViewById(R.id.inputeditTextEmail); buttonSubmit

= findViewById(R.id.submitButton);

buttonSubmit.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

String firstName = editTextFirstName.getText().toString(); String email =


editTextEmail.getText().toString();

Intent intent = new Intent(MainActivity.this,MainActivity2.class); intent.putExtra("firstName",


firstName); intent.putExtra("email", email); startActivity(intent);

});

2.MainAcitivity2.java
package com.example.sahilkumar; import android.content.Intent; import android.os.Bundle;
import android.widget.TextView;

import

androidx.appcompat.app.AppCompatActivity;

public class MainActivity2 extends AppCompatActivity {

TextView textViewDisplay;

@Override protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2);

textViewDisplay = findViewById(R.id.textViewDisplay); Intent intent = getIntent(); if (intent !=


null) {

String firstName = intent.getStringExtra("firstName"); String email =


intent.getStringExtra("email");

if (firstName != null && email != null) {

String displayText = "First Name: " + firstName + "\nEmail: " + email;


textViewDisplay.setText(displayText);

}}} }
4. Create an Android application that demonstrates the methods of the lifecycle

of an Android activity.

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
Toast toast = Toast.makeText(getApplicationContext(), "onCreate Called",
Toast.LENGTH_LONG).show();

protected void onStart() {

super.onStart();

Toast toast = Toast.makeText(getApplicationContext(), "onStart Called",


Toast.LENGTH_LONG).show();

@Override

protected void onRestart() {

super.onRestart();

Toast toast = Toast.makeText(getApplicationContext(), "onRestart Called",


Toast.LENGTH_LONG).show();

protected void onPause() {

super.onPause();

Toast toast = Toast.makeText(getApplicationContext(), "onPause Called",


Toast.LENGTH_LONG).show();

protected void onResume() {

super.onResume();

Toast toast = Toast.makeText(getApplicationContext(), "onResume Called",


Toast.LENGTH_LONG).show();

protected void onStop() {


super.onStop();

Toast toast = Toast.makeText(getApplicationContext(), "onStop Called",


Toast.LENGTH_LONG).show();

protected void onDestroy() {

super.onDestroy();

Toast toast = Toast.makeText(getApplicationContext(), "onDestroy Called",


Toast.LENGTH_LONG).show();

}}

5. Develop an android application for making phone calls using phone dialer.

// MainActivity.kt

package com.example.myapplication;
import android.Manifest;

import android.content.Intent;

import android.content.pm.PackageManager;

import android.net.Uri;

import android.os.Bundle;

import android.view.View;

import android.widget.EditText;

import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import androidx.core.app.ActivityCompat;

public class MainActivity extends AppCompatActivity {

private static final int REQUEST_CALL_PHONE = 1;

private EditText phoneNumberEditText;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

phoneNumberEditText = findViewById(R.id.mobile);

}
public void makePhoneCall(View view) {

String phoneNumber = phoneNumberEditText.getText().toString().trim();

if (!phoneNumber.isEmpty()) {

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) !=
PackageManager.PERMISSION_GRANTED) {

ActivityCompat.requestPermissions(this, new String[]


{Manifest.permission.CALL_PHONE}, REQUEST_CALL_PHONE);

} else {

String dial = "tel:" + phoneNumber;

startActivity(new Intent(Intent.ACTION_CALL, Uri.parse(dial)));

} else {

Toast.makeText(this, "Please enter a phone number", Toast.LENGTH_SHORT).show();

@Override

public void onRequestPermissionsResult(int requestCode, String[] permissions, int[]


grantResults) {

super.onRequestPermissionsResult(requestCode, permissions, grantResults);

if (requestCode == REQUEST_CALL_PHONE) {

if (grantResults.length > 0 && grantResults[0] ==


PackageManager.PERMISSION_GRANTED) {

makePhoneCall(findViewById(android.R.id.content));

} else {
Toast.makeText(this, "Permission DENIED", Toast.LENGTH_SHORT).show();

6. Create an android application to demonstrate the use of options menus in an

App.

package example.javatpoint.com.optionmenu;

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

import android.support.v7.widget.Toolbar;

import android.view.Menu;

import android.view.MenuItem;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(toolbar);

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present

getMenuInflater().inflate(R.menu.menu_main, menu);

return true;

@Override

public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();

switch (id){
case R.id.item1:

Toast.makeText(getApplicationContext(),"Item 1 Selected",Toast.LENGTH_LONG).show();

return true;

case R.id.item2:

Toast.makeText(getApplicationContext(),"Item 2 Selected",Toast.LENGTH_LONG).show();

return true;

case R.id.item3:

Toast.makeText(getApplicationContext(),"Item 3 Selected",Toast.LENGTH_LONG).show();

return true;

default:

return super.onOptionsItemSelected(item);

}}}
7. Develop an Android application that plays a media file.

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import java.util.concurrent.TimeUnit;
public class MainActivity extends Activity {
private Button b1,b2,b3,b4;
private ImageView iv;
private MediaPlayer mediaPlayer;
private double startTime = 0;
private double finalTime = 0;

private Handler myHandler = new Handler();;


private int forwardTime = 5000;
private int backwardTime = 5000;
private SeekBar seekbar;
private TextView tx1,tx2,tx3;

public static int oneTimeOnly = 0;


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

b1 = (Button) findViewById(R.id.button);
b2 = (Button) findViewById(R.id.button2);
b3 = (Button)findViewById(R.id.button3);
b4 = (Button)findViewById(R.id.button4);
iv = (ImageView)findViewById(R.id.imageView);

tx1 = (TextView)findViewById(R.id.textView2);
tx2 = (TextView)findViewById(R.id.textView3);
tx3 = (TextView)findViewById(R.id.textView4);
tx3.setText("Song.mp3");

mediaPlayer = MediaPlayer.create(this, R.raw.song);


seekbar = (SeekBar)findViewById(R.id.seekBar);
seekbar.setClickable(false);
b2.setEnabled(false);

b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Playing
sound",Toast.LENGTH_SHORT).show();
mediaPlayer.start();

finalTime = mediaPlayer.getDuration();
startTime = mediaPlayer.getCurrentPosition();

if (oneTimeOnly == 0) {
seekbar.setMax((int) finalTime);
oneTimeOnly = 1;
}

tx2.setText(String.format("%d min, %d sec",


TimeUnit.MILLISECONDS.toMinutes((long) finalTime),
TimeUnit.MILLISECONDS.toSeconds((long) finalTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long)
finalTime)))
);

tx1.setText(String.format("%d min, %d sec",


TimeUnit.MILLISECONDS.toMinutes((long) startTime),
TimeUnit.MILLISECONDS.toSeconds((long) startTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long)
startTime)))
);

seekbar.setProgress((int)startTime);
myHandler.postDelayed(UpdateSongTime,100);
b2.setEnabled(true);
b3.setEnabled(false);
}
});

b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Pausing
sound",Toast.LENGTH_SHORT).show();
mediaPlayer.pause();
b2.setEnabled(false);
b3.setEnabled(true);
}
});

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int temp = (int)startTime;

if((temp+forwardTime)<=finalTime){
startTime = startTime + forwardTime;
mediaPlayer.seekTo((int) startTime);
Toast.makeText(getApplicationContext(),"You have Jumped forward 5
seconds",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(),"Cannot jump forward 5
seconds",Toast.LENGTH_SHORT).show();
}
}
});

b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int temp = (int)startTime;

if((temp-backwardTime)>0){
startTime = startTime - backwardTime;
mediaPlayer.seekTo((int) startTime);
Toast.makeText(getApplicationContext(),"You have Jumped backward 5
seconds",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(),"Cannot jump backward 5
seconds",Toast.LENGTH_SHORT).show();
}
}
});
}

private Runnable UpdateSongTime = new Runnable() {


public void run() {
startTime = mediaPlayer.getCurrentPosition();
tx1.setText(String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes((long) startTime),
TimeUnit.MILLISECONDS.toSeconds((long) startTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
toMinutes((long) startTime)))
);
seekbar.setProgress((int)startTime);
myHandler.postDelayed(this, 100);
}
};
}

8. Develop an Android application that utilise the concept of autocomplete and

suggests to the user while entering inputs.

import android.graphics.Color;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends AppCompatActivity {


String[] language ={"C","C++","Java",".NET","iPhone","Android","ASP.NET","PHP"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this,android.R.layout.select_dialog_item,language);
AutoCompleteTextView actv =
(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView);
actv.setThreshold(1);
actv.setAdapter(adapter);
actv.setTextColor(Color.RED);
}
}
9. Develop an Android application that adds five items to a ListView

import android.os.Bundle

import android.widget.ArrayAdapter

import android.widget.Button

import android.widget.ListView

import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {

private lateinit var listView: ListView

private lateinit var addButton: Button


private lateinit var adapter: ArrayAdapter<String>

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

listView = findViewById(R.id.listView)

addButton = findViewById(R.id.addButton)

// Create an ArrayAdapter

adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1)

listView.adapter = adapter

// Add five items to the list initially

for (i in 1..5) {

adapter.add("Item $i")

addButton.setOnClickListener {

// Add a new item to the list when the button is clicked

val itemCount = adapter.count + 1

adapter.add("Item $itemCount")
}

You might also like