0% found this document useful (0 votes)
57 views

Final Term Paper Android Programming Solution

The document outlines code snippets for three Android applications: 1) A unit converter app that converts kilograms to pounds. 2) A sticky notes app that allows users to add, view and delete notes using a recycler view and Realm database. 3) A food app login page that validates user credentials on login.

Uploaded by

Hafsa Sweet
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)
57 views

Final Term Paper Android Programming Solution

The document outlines code snippets for three Android applications: 1) A unit converter app that converts kilograms to pounds. 2) A sticky notes app that allows users to add, view and delete notes using a recycler view and Realm database. 3) A food app login page that validates user credentials on login.

Uploaded by

Hafsa Sweet
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/ 3

Department of Computer Science

Android Programming
Final Term (Fall 2022)
BSCS 7th Semester (Evening)
a) Unit Converter:
package com.example.unconvert;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


private Button button;
private TextView textView;
private EditText editText;

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
textView = findViewById(R.id.textView);
editText = findViewById(R.id.editText);

public void onClick(View v){


String s = editText.getText().toString();
int kg = Integer.parseInt(s);
double pound = 2.205 * kg;
textView.setText("The corresponding value in Pounds is " + pound);
Toast.makeText(this, "Thanks for using this app",
Toast.LENGTH_SHORT).show();
}}

b) Sticky Notes App


package com.example.notesapp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import io.realm.Realm;
import io.realm.RealmChangeListener;
import io.realm.RealmResults;
import io.realm.Sort;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
Department of Computer Science
Android Programming
Final Term (Fall 2022)
BSCS 7th Semester (Evening)

import com.google.android.material.button.MaterialButton;

public class MainActivity extends AppCompatActivity {


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MaterialButton addNoteBtn = findViewById(R.id.addNewNoteBtn);
TextView noNotes = findViewById(R.id.noNotes);
addNoteBtn.setOnClickListener(v -> {
startActivity(new Intent(MainActivity.this, AddNoteActivity.class));
});
Realm.init(getApplicationContext());
Realm realm = Realm.getDefaultInstance();
RealmResults<Note> notesList = realm.where(Note.class)
.findAllSorted("createdTime", Sort.DESCENDING);
if (notesList.isEmpty()) {
noNotes.setVisibility(View.VISIBLE);
} else {
noNotes.setVisibility(View.GONE);
}
RecyclerView recyclerView = findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
MyAdapter myAdapter = new MyAdapter(getApplicationContext(),
notesList);
recyclerView.setAdapter(myAdapter);
notesList.addChangeListener(new
RealmChangeListener<RealmResults<Note>>() {
public void onChange(RealmResults<Note> notes) {

if (notesList.isEmpty()) {
noNotes.setVisibility(View.VISIBLE);
} else {
noNotes.setVisibility(View.GONE);
}
myAdapter.notifyDataSetChanged();
}});}}
c) Food App Login Page
package com.example.foodgram;

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class login extends AppCompatActivity {

EditText user;
EditText pass;
Button login;
Department of Computer Science
Android Programming
Final Term (Fall 2022)
BSCS 7th Semester (Evening)
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);

user=findViewById(R.id.user);
pass=findViewById(R.id.pass);
login=findViewById(R.id.button);

login.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (user.getText().toString().equals("admin") &&
pass.getText().toString().equals("admin"))
{
Intent intent=new Intent(login.this,MainActivity.class);
startActivity(intent);
}
else {
Toast.makeText(login.this,"Username or Password
Incorrect",Toast.LENGTH_SHORT).show();
}
if (TextUtils.isEmpty(user.getText().toString())){
user.setError("Please Enter User Name");
return;
}
else if (TextUtils.isEmpty(pass.getText().toString())){
pass.setError("Please Enter Password");
return;}}});}}

2) Define the following built-in functions:

toString():
A toString() is an in-built method in Java that returns the value given to it in string format.

getApplicationContext ():

It is used to return the Context which is linked to the Application which holds all activities running inside it. When
we call a method or a constructor, we often have to pass a Context and often we use “this” to pass the activity
Context or “getApplicationContext” to pass the application Context.

putExtra():
the putExtra() method is used to send data between Android activities.

trim():
The trim() method removes whitespace from both sides of a string. The trim() method does not change the original
string.
equals(): The equals() method compares two strings, and returns true if the strings are equal, and false if not.

You might also like