0% found this document useful (0 votes)
7 views16 pages

Mad Programs

The document provides a guide on creating a simple SQLite database in an Android application using the openOrCreateDatabase() method. It includes XML layout examples for user interfaces, such as buttons for creating a database, sending SMS, converting temperature, and capturing images. Additionally, it covers Android activity lifecycle methods and UI design using a table layout for number input.

Uploaded by

Bhumit Maru
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)
7 views16 pages

Mad Programs

The document provides a guide on creating a simple SQLite database in an Android application using the openOrCreateDatabase() method. It includes XML layout examples for user interfaces, such as buttons for creating a database, sending SMS, converting temperature, and capturing images. Additionally, it covers Android activity lifecycle methods and UI design using a table layout for number input.

Uploaded by

Bhumit Maru
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/ 16

Create a simple database in SQLite

This procedure is by openOrCreateDatabase()


1. The package imported into the application is android.database.sqlite.SQLiteDatabase.
2. Here the class used is SQLiteDatabase.
3. The method used to create the database or connect to the database is openOrCreateDatabse()
method.

Program: activity_main.xml import


android.database.sqlite.SQLiteDatabase;
<?xml version="1.0" encoding="utf-8"?>
import android.os.Bundle;
<RelativeLayout
xmlns:android=http://schemas.android.com/ import
apk/res/android android.support.v7.app.AppCompatActivity;

xmlns:tools="http://schemas.android.com/to import android.view.View;


ols" import android.widget.Button;;
android:layout_width="match_parent" public class MainActivity extends
android:layout_height="match_parent" AppCompatActivity {

tools:context=".MainActivity"> SQLiteDatabase sqLiteDatabaseObj;

<Button Button EnterData;

android:text="Create SQLite Database" @Override

android:layout_width="fill_parent" protected void onCreate(Bundle


savedInstanceState) {
android:layout_height="wrap_content"
super.onCreate(savedInstanceState);
android:layout_centerHorizontal="true"
setContentView(R.layout.activity_main);
android:layout_marginTop="46dp"
createData =
android:id="@+id/button" /> (Button)findViewById(R.id.button);
</RelativeLayout> createData.setOnClickListener(new
View.OnClickListener() {
MainActivity.java
@Override
package in.edu.vpt.insertusingasync;
public void onClick(View view) {
import android.app.ProgressDialog;
sqLiteDatabaseObj =
import android.content.Context; openOrCreateDatabase("AndroidJSonDataB
ase",
Context.MODE_PRIVATE, null); }
} });

program to demonstrate declaring and using permissions / Send or Receive


SMS
Permission declaring :
The permissions are declared in AndroidManifest.xml file under Manifest folder. Permission can
be set by tag in AndroidManifest.xml.
Example: Following example is to send SMS
AndroidManifest.xml
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com
/apk/res/android" app:layout_constraintBottom_toBottomOf="
xmlns:app="http://schemas.android.com/apk parent"
/res-auto"
xmlns:tools="http://schemas.android.com/to app:layout_constraintEnd_toEndOf="parent
ols" "/>
android:layout_width="match_parent" <TextView
android:layout_height="match_parent" android:id="@+id/textView2"
tools:context=".MainActivity"> android:layout_width="70dp"
<TextView android:layout_height="43dp"
android:id="@+id/textView" android:layout_marginEnd="276dp"
android:layout_width="81dp" android:layout_marginBottom="512dp"
android:text="Sms Text"
android:layout_height="41dp"
app:layout_constraintBottom_toBottomOf="
android:layout_marginEnd="268dp"
parent"
android:layout_marginBottom="576dp" app:layout_constraintEnd_toEndOf="parent
" />
android:text="To :"
<EditText
android:id="@+id/etPhno" android:layout_height="wrap_content"
android:layout_width="wrap_content" android:layout_marginEnd="156dp"
android:layout_height="wrap_content" android:layout_marginBottom="400dp"
android:layout_marginEnd="40dp" android:text="SEND SMS"
app:layout_constraintBottom_toBottomOf="
android:layout_marginBottom="572dp"
parent"
android:ems="10" app:layout_constraintEnd_toEndOf="parent
" />
android:inputType="textPersonName"
</androidx.constraintlayout.widget.Constrai
ntLayout>
app:layout_constraintBottom_toBottomOf="
parent" MainActivity.java
package com.example.testreceivesms;
app:layout_constraintEnd_toEndOf="parent
" /> import
androidx.appcompat.app.AppCompatActivit
<EditText y;
android:id="@+id/etmsg" import androidx.core.app.ActivityCompat;
android:layout_width="193dp" import
android:layout_height="51dp" androidx.core.content.ContextCompat;

android:layout_marginEnd="56dp" import android.Manifest;

android:layout_marginBottom="504dp" import android.content.IntentFilter;

android:inputType="textPersonName" import
android.content.pm.PackageManager;

app:layout_constraintBottom_toBottomOf=" import android.os.Bundle;


parent" import android.telephony.SmsManager;
import android.view.View;
app:layout_constraintEnd_toEndOf="parent
" import android.widget.Button;

tools:ignore="SpeakableTextPresentCheck" import android.widget.EditText;


/> import android.widget.Toast;
<Button public class MainActivity extends
android:id="@+id/btnSms" AppCompatActivity {

android:layout_width="wrap_content" EditText et1,et2;


Button b1;
smsManager.sendTextMessage(phno,null,m
@Override
sg,null,null);
protected void onCreate(Bundle
Toast.makeText(MainActivity.this,"Sms
savedInstanceState) {
sent successfully",
super.onCreate(savedInstanceState);
Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_main);
}
et1=findViewById(R.id.etPhno);
catch(Exception e)
et2=findViewById(R.id.etmsg);
{
b1=findViewById(R.id.btnSms);
Toast.makeText(MainActivity.this,"Sms
if(ContextCompat.checkSelfPermission(Mai failed to send... try again",
nActivity.this,Manifest.permission.SEN
Toast.LENGTH_LONG).show();
D_SMS)!=
}
}
PackageManager.PERMISSION_GRANTE
D) });
{ }
}
ActivityCompat.requestPermissions(MainA
ctivity.this,new

String[]{Manifest.permission.SEND_SMS},
100);
}
b1.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
try {
String phno= et1.getText().toString();
String msg=et2.getText().toString();
SmsManager smsManager=
SmsManager.getDefault();
a program to convert temperature <Button
from celcius to farenhite and vice android:id="@+id/button"
versa using Toggle button.
android:layout_width="wrap_content"
activity_main.xml android:layout_height="wrap_content"
<?xml version="1.0" encoding="utf-8"?> android:layout_below="@id/togglebutton"
<RelativeLayout android:layout_marginTop="56dp" />
xmlns:android="http://schemas.android.com
/apk/res/android" </RelativeLayout>

MainActivity.java
xmlns:app="http://schemas.android.com/apk
/res-auto" package com.example.p1;
import
xmlns:tools="http://schemas.android.com/to androidx.appcompat.app.AppCompatActivit
ols" y;

android:layout_width="match_parent" import android.os.Bundle;

android:layout_height="match_parent" import android.view.View;

tools:context=".MainActivity"> import android.widget.Button;

<EditText import android.widget.EditText;

android:layout_width="match_parent" import android.widget.Toast;

android:layout_height="wrap_content" import android.widget.ToggleButton;

android:id="@+id/edittext" public class MainActivity extends


AppCompatActivity {
android:hint="Enter the temp"/>
Button b1;
<ToggleButton
EditText et;
android:id="@+id/togglebutton"
ToggleButton tb;
android:layout_width="wrap_content"
Double a;
android:layout_height="wrap_content"
@Override
android:layout_below="@+id/edittext"
protected void onCreate(Bundle
android:layout_marginTop="35dp" savedInstanceState) {
android:textOff="F to C" super.onCreate(savedInstanceState);
android:textOn="C to F" /> setContentView(R.layout.activity_main);
et=findViewById(R.id.edittext); a program to capture an image
b1=findViewById(R.id.button); using camera and display it
tb=findViewById(R.id.togglebutton); activity_main.xml
b1.setOnClickListener(new
<?xml version="1.0" encoding="utf-8"?>
View.OnClickListener() {
<RelativeLayout
@Override
xmlns:android="http://schemas.android.com
public void onClick(View v) { /apk/res/android"

if(tb.isChecked()) xmlns:app="http://schemas.android.com/apk
/res-auto"
{ xmlns:tools="http://schemas.android.com/to
a=Double.parseDouble(String.valueOf(et.ge ols"
tText())); android:layout_width="match_parent"
Double b=a*9/5+32; android:layout_height="match_parent"
String r=String.valueOf(b); android:padding="40dp"
Toast.makeText(MainActivity.this,r+"°F",T android:orientation="horizontal"
oast.LENGTH_SHORT).show();
tools:context=".MainActivity">
}
<TextView
else
android:layout_width="match_parent"
{
a=Double.parseDouble(String.valueOf(et.ge android:layout_height="wrap_content"
tText()));
android:text="CAMERA"
Double b=a-32;
android:id="@+id/text"
Double c=b*5/9;
android:textSize="20dp"
String r=String.valueOf(c);
android:gravity="center"/>
Toast.makeText(MainActivity.this,r+"°C",T
<ImageView
oast.LENGTH_SHORT).show();
android:id="@+id/image"
}}
android:layout_width="match_parent"
});
android:layout_height="wrap_content"
}
android:layout_below="@+id/text"
}
android:layout_marginTop="81dp"
android:src="@drawable/rose"/>
<Button super.onCreate(savedInstanceState);
android:id="@+id/photo" setContentView(R.layout.activity_main);
android:layout_width="wrap_content" b1=findViewById(R.id.photo);
android:layout_height="wrap_content" imageView=findViewById(R.id.image);
android:layout_below="@+id/image" b1.setOnClickListener(new
View.OnClickListener() {
android:layout_centerHorizontal="true"
@Override
android:layout_marginTop="30dp"
public void onClick(View v) {
android:text="TAKE PHOTO" />
Intent i=new
</RelativeLayout>
Intent(MediaStore.ACTION_IMAGE_CAP
TURE);
MainActivity.java startActivityForResult(i,CAMERA_REQUE
ST);
package com.example.ifcdiv;
}
import
androidx.appcompat.app.AppCompatActivit }); }
y;
@Override
import android.content.Intent;
protected void onActivityResult(int
import android.graphics.Bitmap; requestCode, int resultCode, @Nullable
Intent
import android.os.Bundle;
data) {
import android.provider.MediaStore;
super.onActivityResult(requestCode,
import android.view.View;
resultCode, data);
import android.widget.Button;
if (requestCode==CAMERA_REQUEST)
import android.widget.ImageView;
{
public class MainActivity extends
Bitmap image= (Bitmap)
AppCompatActivity {
data.getExtras().get("data");
Button b1;
imageView.setImageBitmap(image);
ImageView imageView;
}
int CAMERA_REQUEST=1;
}
@Override
}
protected void onCreate(Bundle
savedInstanceState) {
program to implement Android @Override
Activity Life Cycle protected void onDestroy() {
package com.example.p1; import super.onDestroy();
androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle; import
android.widget.Toast; public class MainActivity Toast.makeText(getApplicationContext(),"A
extends AppCompatActivity ctivity

{ Destroy",Toast.LENGTH_LONG).show();
@Override }

protected void onCreate(Bundle @Override


savedInstanceState) {
protected void onPause() {
super.onCreate(savedInstanceState);
super.onPause();
setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(),"A
Toast.makeText(getApplicationContext(),"A ctivity
ctivity
Pause",Toast.LENGTH_LONG).show();
created",Toast.LENGTH_LONG).show();
}
}
@Override
@Override
protected void onRestart() {
protected void onStart() {
super.onResume();
super.onStart();
Toast.makeText(getApplicationContext(),"A
Toast.makeText(getApplicationContext(),"A ctivity
ctivity
Restart",Toast.LENGTH_LONG).show();
Started",Toast.LENGTH_LONG).show();
@Override
}
protected void onResume() {
@Override
super.onResume();
protected void onStop() {
super.onStop(); Toast.makeText(getApplicationContext(),"A
ctivity
Toast.makeText(getApplicationContext(),"A Resume",Toast.LENGTH_LONG).show();
ctivity
}
Stop",Toast.LENGTH_LONG).show();
}
}
Design UI using table layout to display buttons with 0 9 numbers on it. Even
display submit and clear button. When user clicks on particular buttons and
later when clicks on submit button, it should display the numbers clicked.
activity_main.xml android:text="1"/>

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

<TableLayout android:layout_width="wrap_content"
xmlns:android="http://schemas.android.com android:layout_height="wrap_content"
/apk/res/android"
android:id="@+id/button2"

xmlns:app="http://schemas.android.com/apk android:text="2" />


/res-auto" </TableRow>
<TableRow
xmlns:tools="http://schemas.android.com/to
ols" android:layout_width="match_parent"

android:layout_width="match_parent" android:layout_height="wrap_content">

android:layout_height="match_parent" <Button

android:stretchColumns="*" android:layout_height="wrap_content"

tools:context=".MainActivity"> android:layout_width="wrap_content"

<TableRow android:id="@+id/button3"

android:layout_width="match_parent" android:text="3"/>

android:layout_height="wrap_content"> <Button

<Button android:layout_height="wrap_content"

android:layout_height="wrap_content" android:layout_width="wrap_content"

android:layout_width="wrap_content" android:id="@+id/button4"

android:id="@+id/button0" android:text="4"/>

android:text="0"/> <Button

<Button android:layout_width="wrap_content"

android:layout_height="wrap_content" android:layout_height="wrap_content"

android:layout_width="wrap_content" android:text="5"

android:id="@+id/button1" android:id="@+id/button5"/>
</TableRow>
<TableRow android:layout_height="wrap_content"
android:layout_width="match_parent" android:layout_width="wrap_content"
android:layout_height="wrap_content"> android:text="Submit"
<Button android:id="@+id/submit"/>
android:layout_height="wrap_content" <Button
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="6" android:layout_width="wrap_content"
android:id="@+id/button6"/> android:text="Clear"
<Button android:id="@+id/clear"/>
android:layout_height="wrap_content" </TableRow>
</TableLayout>
android:layout_width="wrap_content"
android:text="7"
android:id="@+id/button7"/> (Java File)
<Button package com.example.p1;

android:layout_width="wrap_content" import
androidx.appcompat.app.AppCompatActivit
android:layout_height="wrap_content" y;
android:text="8" import android.os.Bundle;
android:id="@+id/button8"/> import android.view.View;
</TableRow> import android.widget.Button;
<TableRow import android.widget.Toast;
android:layout_width="match_parent" public class MainActivity extends
android:layout_height="wrap_content"> AppCompatActivity {

<Button Button button0, button1, button2, button3,


button4, button5, button6,button7,
android:layout_height="wrap_content"
button8, button9,submit,clear;
android:layout_width="wrap_content"
String a=null;
android:text="9"
@Override
android:id="@+id/button9"/>
protected void onCreate(Bundle
<Button savedInstanceState) {
super.onCreate(savedInstanceState); button1.setOnClickListener(new
View.OnClickListener() {
setContentView(R.layout.activity_main);
@Override
button0 = (Button)
findViewById(R.id.button0); public void onClick(View view) {
button1 = (Button) a=button1.getText().toString();
findViewById(R.id.button1);
}
button2 = (Button)
});
findViewById(R.id.button2);
button2.setOnClickListener(new
button3 = (Button)
View.OnClickListener() {
findViewById(R.id.button3);
@Override
button4 = (Button)
findViewById(R.id.button4); public void onClick(View view) {
button5 = (Button) a=button2.getText().toString();
findViewById(R.id.button5);
}
button6 = (Button) });
findViewById(R.id.button6);
button3.setOnClickListener(new
button7 = (Button) View.OnClickListener() {
findViewById(R.id.button7);
@Override
button8 = (Button)
public void onClick(View view) {
findViewById(R.id.button8);
button9 = (Button) a=button3.getText().toString();
findViewById(R.id.button9); }
submit=(Button) });
findViewById(R.id.submit);
button4.setOnClickListener(new
clear=(Button) findViewById(R.id.clear); View.OnClickListener() {
button0.setOnClickListener(new @Override
View.OnClickListener() {
public void onClick(View view) {
@Override
a=button4.getText().toString();
public void onClick(View view) {
}
a=button0.getText().toString();
});
}
button5.setOnClickListener(new
}); View.OnClickListener() {
@Override a=button9.getText().toString();
public void onClick(View view) { }
a=button5.getText().toString(); });
} submit.setOnClickListener(new
View.OnClickListener() {
});
@Override
button6.setOnClickListener(new
View.OnClickListener() { public void onClick(View view) {
@Override
public void onClick(View view) { Toast.makeText(getApplicationContext(),a,
Toast.LENGTH_LONG).show(); }
a=button6.getText().toString();
});
}
}
});
}
button7.setOnClickListener(new
View.OnClickListener() {
@Override Text to Speech
public void onClick(View view) {
Acivity_main.xml
a=button7.getText().toString();
<?xml version="1.0" encoding="utf-8"?>
}
<LinearLayout
});
button8.setOnClickListener(new xmlns:android="http://schemas.android.com
View.OnClickListener() { /apk/res/android"
@Override android:layout_width="match_parent"
public void onClick(View view) { android:layout_height="match_parent"
a=button8.getText().toString(); android:orientation="vertical">
} <EditText
}); android:layout_width="match_parent"
button9.setOnClickListener(new android:layout_height="wrap_content"
View.OnClickListener() {
android:id="@+id/Text"
@Override
android:layout_marginBottom="20dp"
public void onClick(View view) {
android:hint="Enter Any Sentence" import android.os.Bundle;
android:gravity="center" import android.speech.tts.TextToSpeech;
android:textSize="16dp"/> import android.view.View;
import android.widget.Button;
<Button import android.widget.EditText;
android:layout_width="wrap_content" import java.util.Locale;
android:id="@+id/btnText"
android:layout_height="wrap_content" public class MainActivity extends
AppCompatActivity {
android:text="Click Here"
android:layout_gravity="center"/>
EditText Text;
Button btnText;
<TextView
TextToSpeech textToSpeech;
android:id="@+id/textView"
@Override
android:layout_width="match_parent"
protected void onCreate(Bundle
android:layout_height="wrap_content"
savedInstanceState) {
android:layout_marginTop="70dp"
super.onCreate(savedInstanceState);
android:gravity="center_horizontal"
android:text="HELLO WORLD" setContentView(R.layout.activity_main);
Text = findViewById(R.id.Text);
android:textColor="@android:color/holo_gr
btnText = findViewById(R.id.btnText);
een_dark"
textToSpeech = new
android:textSize="36sp" />
TextToSpeech(getApplicationContext(),
</LinearLayout> new TextToSpeech.OnInitListener() {
@Override

MainActivity.java public void onInit(int i) {

Package com.example.tts; if(i!=TextToSpeech.ERROR){

import textToSpeech.setLanguage(Locale.UK);
androidx.appcompat.app.AppCompatActivit }
y;
} });
btnText.setOnClickListener(new android:layout_height="wrap_content"
View.OnClickListener() {
android:layout_alignParentTop="true"
@Override
android:layout_centerHorizontal="true"
public void onClick(View view) {
android:text="Enter number 1"
android:textSize="18sp" />
textToSpeech.speak(Text.getText().toString(
),TextToSpeech.QUEUE_FLUSH,null); <EditText
}}); android:id="@+id/first"
} android:layout_width="wrap_content"
} android:layout_height="wrap_content"

android:layout_below="@id/textview1"
android:layout_alignParentTop="false"
Addition of two numbers
activity_main.xml android:layout_alignParentRight="false"

<?xml version="1.0" encoding="utf-8"?> android:layout_centerHorizontal="true"

<RelativeLayout android:ems="10"
xmlns:android=http://schemas.android.com/ android:inputType="number" />
apk/res/android
<TextView
xmlns:
app="http://schemas.android.com/apk/res- android:id="@+id/textView2"
auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/to
ols" android:layout_below="@id/first"
android:layout_width="match_parent" android:layout_centerHorizontal="true"
android:layout_height="match_parent" android:text="Enter Number 2"
android:layout_centerHorizontal="true" android:textSize="18sp" />
tools:context=".MainActivity"> <EditText
<TextView android:id="@+id/second"
android:id="@+id/textview1" android:layout_width="wrap_content"
android:layout_width="wrap_content" android:layout_height="wrap_content"
import android.widget.EditText;
android:layout_below="@id/textView2"
import android.widget.TextView;
android:layout_centerHorizontal="true"
public class MainActivity extends
android:ems="10" AppCompatActivity {
android:inputType="number" /> EditText firstnum,secondnum;
<Button TextView r;
android:id="@+id/buttonadd" Button bt;
android:layout_width="wrap_content" double a,b,c;
android:layout_height="wrap_content" @Override
android:layout_below="@id/second" protected void onCreate(Bundle
savedInstanceState) {
android:layout_centerHorizontal="true"
super.onCreate(savedInstanceState);
android:text="Add" />
<TextView
setContentView(R.layout.activity_main);
android:id="@+id/result"
firstnum=(EditText)
android:layout_width="wrap_content" findViewById(R.id.first);

android:layout_height="wrap_content" secondnum=(EditText)
findViewById(R.id.second);

android:layout_below="@id/buttonadd" bt=(Button)
findViewById(R.id.buttonadd);
android:layout_centerHorizontal="true"
r=(TextView)
android:textSize="24sp" /> findViewById(R.id.result);
</RelativeLayout> bt.setOnClickListener(new
View.OnClickListener() {
MainActivity.java
@Override
package org.teachics.addition;
public void onClick(View v) {
import
androidx.appcompat.app.AppCompatActivit
y; a=Double.parseDouble(firstnum.getText().to
String());
import android.os.Bundle;
import android.view.View;
b=Double.parseDouble(secondnum.getText(
import android.widget.Button; ).toString());
c=a+b;
r.setText("Sum="+c);
}
});
}
}
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