Mad Lab Programs: Program 1: Visiting Card

Download as pdf or txt
Download as pdf or txt
You are on page 1of 48

MAD LAB PROGRAMS:

PROGRAM 1: VISITING CARD


<?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"
android:background="#C6f7fd"
android:paddingLeft="7sp"
android:paddingTop="14sp"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="149dp"
android:layout_height="80dp"
android:fontFamily="cursive"
android:paddingLeft="5sp"
android:paddingTop="10sp"
android:text="Welcome"
android:textColor="@color/teal_700"
android:textSize="40sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.232"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.196" />

<View
android:id="@+id/divider"
android:layout_width="380dp"
android:layout_height="5dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:background="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.282" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:fontFamily="serif"
android:padding="5sp"
android:text="Name:"
android:textSize="22sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.112"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/divider" />

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="36dp"
android:layout_marginTop="60dp"
android:fontFamily="serif"
android:text="Class:"
android:textSize="22sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="120dp"
android:layout_marginTop="20dp"
android:fontFamily="sans-serif-smallcaps"
android:padding="5sp"
android:text="Shreesha"
android:textSize="20sp"
android:textStyle="bold|italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.014"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/divider" />

<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="64dp"
android:fontFamily="sans-serif-smallcaps"
android:padding="5sp"
android:text="6B"
android:textSize="20sp"
android:textStyle="bold|italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView3"
app:layout_constraintTop_toBottomOf="@+id/textView4" />

<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="36dp"
android:layout_marginTop="60dp"
android:fontFamily="serif"
android:text="Phone Number:"
android:textSize="22sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3" />

<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="60dp"
android:fontFamily="sans-serif-smallcaps"
android:padding="5sp"
android:text="9119119110"
android:textSize="20sp"
android:textStyle="bold|italic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.06"
app:layout_constraintStart_toEndOf="@+id/textView6"
app:layout_constraintTop_toBottomOf="@+id/textView5" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click here"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/divider"
app:layout_constraintVertical_bias="0.792" />
<View
android:id="@+id/divider2"
android:layout_width="380dp"
android:layout_height="5dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:background="@color/black"
app:layout_constraintBottom_toTopOf="@+id/button1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.741"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/divider"
app:layout_constraintVertical_bias="0.948" />

<ImageView
android:id="@+id/imageView2"
android:layout_width="168dp"
android:layout_height="173dp"
android:layout_marginStart="40dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintCircleRadius="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="@+id/textView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.011"
app:srcCompat="@drawable/index" />

</androidx.constraintlayout.widget.ConstraintLayout>

----------------------------------------------------------------------------------------------------------------------------- --------

PROGRAM 2: CALCUALOTOR
package com.example.calculator_b2;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.text.DecimalFormat;
import java.util.Stack;

public class MainActivity extends AppCompatActivity {


Button
badd,bsub,bmul,bdiv,bper,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,bac,bpm,beq,bdec;
TextView textView;
Stack<Double> ns = new Stack<>();
Stack<String> os = new Stack<>();
boolean decFlag = false;
StringBuffer sb = new StringBuffer();
double op1, op2, res;
String operand;
DecimalFormat form = new DecimalFormat("#");

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
badd = findViewById(R.id.btn_plus);
bsub = findViewById(R.id.btn_minus);
bmul = findViewById(R.id.btn_mul);
bdiv = findViewById(R.id.btn_div);
bper = findViewById(R.id.btn_per);
bac = findViewById(R.id.btn_ac);
bpm = findViewById(R.id.btn_pm);
beq = findViewById(R.id.btn_eq);
bdec = findViewById(R.id.btn_dec);
b0 = findViewById(R.id.btn0);
b1 = findViewById(R.id.btn1);
b2 = findViewById(R.id.btn2);
b3 = findViewById(R.id.btn3);
b4 = findViewById(R.id.btn4);
b5 = findViewById(R.id.btn5);
b6 = findViewById(R.id.btn6);
b7 = findViewById(R.id.btn7);
b8 = findViewById(R.id.btn8);
b9 = findViewById(R.id.btn9);
textView = findViewById(R.id.txtView);

bpm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
op1 = Double.parseDouble(textView.getText().toString());
op1*=-1;
textView.setText(String.valueOf(op1));
}
});
b0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sb.append("0");
textView.setText(sb.toString());
}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sb.append("1");
textView.setText(sb.toString());
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sb.append("2");
textView.setText(sb.toString());
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sb.append("3");
textView.setText(sb.toString());
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sb.append("4");
textView.setText(sb.toString());
}
});
b5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sb.append("5");
textView.setText(sb.toString());
}
});
b6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sb.append("6");
textView.setText(sb.toString());
}
});
b7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sb.append("7");
textView.setText(sb.toString());
}
});
b8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sb.append("8");
textView.setText(sb.toString());
}
});
b9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sb.append("9");
textView.setText(sb.toString());
}
});
badd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
op1 = Double.parseDouble(textView.getText().toString());
ns.push(op1);
os.push("+");
textView.setText("");
sb.delete(0,sb.length());
decFlag=false;
}
});
bsub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
op1 = Double.parseDouble(textView.getText().toString());
ns.push(op1);
os.push("-");
textView.setText("");
sb.delete(0,sb.length());
decFlag=false;
}
});
bmul.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
op1 = Double.parseDouble(textView.getText().toString());
ns.push(op1);
os.push("*");
textView.setText("");
sb.delete(0,sb.length());
decFlag=false;
}
});
bper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
op1 = Double.parseDouble(textView.getText().toString());
ns.push(op1);
os.push("%");
textView.setText("");
sb.delete(0,sb.length());
decFlag=false;
}
});
bdiv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
op1 = Double.parseDouble(textView.getText().toString());
ns.push(op1);
os.push("/");
textView.setText("");
sb.delete(0,sb.length());
decFlag=false;
}
});
bdec.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!decFlag){
decFlag=true;
sb.append(".");
textView.setText(sb.toString());
}
}
});
beq.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
operand = os.pop();
op2 = Double.parseDouble(textView.getText().toString());
sb.delete(0,sb.length());
op1 = ns.pop();
switch (operand){
case "+":
res = op1 + op2;
break;
case "-":
res = op1 - op2;
break;
case "/":
res = op1 / op2;
break;
case "*":
res = op1 * op2;
break;
case "%":
res = op1 % op2;
break;
}
textView.setText(String.valueOf(Math.round(res * 100.0) / 100.0));
}
});
bac.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
decFlag=false;
operand = "";
op1 = 0;
op2 = 0;
sb.delete(0,sb.length());
while(!ns.empty()) ns.pop();
while(!os.empty()) os.pop();
textView.setText("");
}
});
}
}
----------------------------------------------------------------------------------------------------------------------------- --------

PROGRAM 3: SIGNUP ACTIVITY

package com.example.signup_b2;

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 java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity implements


View.OnClickListener {
EditText userName, password;
Button signUpBtn;
String name, pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userName = findViewById(R.id.username);
password = findViewById(R.id.password);
signUpBtn = findViewById(R.id.btn);
signUpBtn.setOnClickListener(this);
}

@Override
public void onClick(View view) {
if(validate()) {

Intent intent = new Intent(this, LoginActivity.class);


// name = userName.getText().toString();
// pass = password.getText().toString();
Bundle bundle = new Bundle();
bundle.putString("name", name);
bundle.putString("pass", pass);
intent.putExtras(bundle);
startActivity(intent);
}
else {
if(name.length()!=4)
userName.setError("4 characters only, no Special Characters
allowed");
if(pass.length()!=8)
password.setError("8 characters");
}
}

private boolean validate() {


name = userName.getText().toString();
pass = password.getText().toString();
name = name.trim();
pass=pass.trim();
return ((validateUN()) && (validatePW()));
}

private boolean validatePW() {


if(TextUtils.isEmpty(pass)){
password.setError("Password cant be empty");
return false;
}
else{
String pattern = "[\\p{Alnum}\\{Punct}]{8}";
Pattern p= Pattern.compile(pattern);
Matcher m =p.matcher(pass);
return m.matches();
}
}

private boolean validateUN() {


if(TextUtils.isEmpty(name)){
password.setError("Name field cant be empty");
return false;
}
else{
String pattern = "\\p{Alpha}{4}";
Pattern p= Pattern.compile(pattern);
Matcher m =p.matcher(name);
return m.matches();
}
}

package com.example.signup_b2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;

public class LoginActivity extends AppCompatActivity implements


View.OnClickListener {

EditText userName, password;


Button signInBtn;
String uname, upass, lname, lpass;
int count = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
userName = findViewById(R.id.username2);
password = findViewById(R.id.password2);
signInBtn = findViewById(R.id.btn2);
signInBtn.setOnClickListener(this);
}

@Override
public void onClick(View view) {
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
uname = bundle.getString("name");
upass = bundle.getString("pass");

if(validate()) {
WebView webView = new WebView(this);
setContentView(webView);
webView.loadUrl("https://google.com");
CookieManager.getInstance().removeAllCookies(null);
CookieManager.getInstance().flush();
}
else {
if(count<=0){
signInBtn.setEnabled(false);
return;
}
if (uname.length() != lname.length()) userName.setError("Username is
not there");
if (upass.length() != lpass.length()) {
count--;
password.setError("Number of attempts left: " + count);
}
}
}

private boolean validate() {


lname = userName.getText().toString();
lpass = password.getText().toString();
lname = lname.trim();
lpass = lpass.trim();
if(TextUtils.isEmpty(lname) || TextUtils.isEmpty(lpass)) return false;
else if(TextUtils.equals(uname, lname) && TextUtils.equals(upass, lpass))
return true;
else return false;
}
}
--------------------------------------------------------------------------------------------------------

PROGRAM 4: WALLPAPER
package com.example.wallpaper_b2;

import androidx.appcompat.app.AppCompatActivity;

import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.BoringLayout;
import android.util.DisplayMetrics;
import android.view.View;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

Bitmap bitmap[], bm;


DisplayMetrics displayMetrics;
WallpaperManager wallpaperManager;

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

public void changeEvent(View view) {


new Thread(new Runnable() {
@Override
public void run() {
bitmap = new Bitmap[]{
BitmapFactory.decodeResource(getResources(),
R.drawable.wp1),
BitmapFactory.decodeResource(getResources(),
R.drawable.wp2),
BitmapFactory.decodeResource(getResources(),
R.drawable.wp3),
BitmapFactory.decodeResource(getResources(),
R.drawable.wp4),
BitmapFactory.decodeResource(getResources(), R.drawable.wp5)
};
displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int width = displayMetrics.widthPixels;
int height = displayMetrics.heightPixels;
for( int i=0; i<bitmap.length; i++){
bitmap[i]=Bitmap.createScaledBitmap(bitmap[i],width,height,false);
}

for(int i=0; i< bitmap.length;i++){


bm = bitmap[i];
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}

Handler handler = new Handler(Looper.getMainLooper());


wallpaperManager =
WallpaperManager.getInstance(getApplicationContext());
handler.post(new Runnable() {
@Override
public void run() {
try {
wallpaperManager.setBitmap(bm);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
}).start();

}
}

--------------------------------------------------------------------------------------------------------

PROGRAM 5: COUNTER

package com.example.counter_b2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

int count=0;
volatile boolean isStopped = true;
TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView2);
}

public void startCount(View view) {


isStopped = false;
count = 0;
new Thread(new Runnable() {
@Override
public void run() {
while(!isStopped){
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
count++;
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
textView.setText(String.valueOf(count));
}
});
}
}
}).start();
}

public void stopCount(View view) {


isStopped = true;
}
}
----------------------------------------------------------------------------------------------------------------------------- -------

PROGRAM 6:XML PARSER AND JSON DATA


package com.example.parser_b2;

import androidx.appcompat.app.AppCompatActivity;

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

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

Button jsonBtn, xmlBtn;


TextView jsonTV, xmlTV;

XmlPullParserFactory pullParserFactory;
XmlPullParser parser;
InputStream is;

RequestQueue requestQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xmlBtn = findViewById(R.id.XMLbutton);
jsonBtn = findViewById(R.id.JSONbutton);
xmlTV = findViewById(R.id.XMLTextView);
jsonTV = findViewById(R.id.JSONTextView);

xmlBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
parseXMLFile();
}
});

jsonBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
parseJSONFile();
}
});
requestQueue = Volley.newRequestQueue(this);
}

private void parseJSONFile() {


StringBuilder sb = new StringBuilder();
String url = "https://api.npoint.io/332dbe8ec2cb456b9757";
JsonObjectRequest request = new
JsonObjectRequest(Request.Method.GET, url, null, new
Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("cities");
for (int i=0; i< jsonArray.length();i++){
JSONObject city = jsonArray.getJSONObject(i);
sb.append("City Name:
").append(city.getString("cityName")).append("\n").
append("Latitude:
").append(city.getString("latitude")).append("\n").
append("Longitude:
").append(city.getString("longitude")).append("\n").
append("Temperature:
").append(city.getString("temperature")).append("\n").
append("Humidity:
").append(city.getString("humidity")).append("\n");
}
jsonTV.setText(sb.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
jsonTV.setText("Error Displaying");
}
});
requestQueue.add(request);
}

private void parseXMLFile() {


try{
pullParserFactory = XmlPullParserFactory.newInstance();
parser = pullParserFactory.newPullParser();
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES,
false);
is= getAssets().open("cities.xml");
parser.setInput(is, null);
parseXMLData(parser);
} catch (XmlPullParserException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

private void parseXMLData(XmlPullParser parser) {


ArrayList<City> cities = new ArrayList<>();
City cur_city = null;
String ele_name = null;
try{
int event_type = parser.getEventType();
while(event_type!=XmlPullParser.END_DOCUMENT){
if(event_type==XmlPullParser.START_TAG){
ele_name = parser.getName();
if(ele_name.equals("city")){
cur_city = new City();
cities.add(cur_city);
}
else if(ele_name.equals("name")){
cur_city.cityName= parser.nextText();
}
else if(ele_name.equals("latitude")){
cur_city.latitude= parser.nextText();
}
else if(ele_name.equals("longitude")){
cur_city.longitude= parser.nextText();
}
else if(ele_name.equals("temperature")){
cur_city.temperature= parser.nextText();
}
else if(ele_name.equals("humidity")){
cur_city.humidity= parser.nextText();
}
}
event_type =parser.next();
}
}catch (XmlPullParserException e){
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
printXMLData(cities);
}

private void printXMLData(ArrayList<City> cities) {


StringBuilder sb = new StringBuilder();
for(City city:cities){
sb.append("City Name: ").append(city.cityName).append("\n").
append("Latitude: ").append(city.latitude).append("\n").
append("Longitude: ").append(city.longitude).append("\n").
append("Temperature: ").append(city.temperature).append("\n").
append("Humidity: ").append(city.humidity).append("\n");
}
xmlTV.setText(sb.toString());
}
}

public class City {


public String cityName, latitude, longitude, temperature, humidity;
}

--------------------------------------------------------------------------------------------------------

PROGRAM 7: TEXT TO SPEECH


package com.example.texttospeech_b2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {


Button speakBtn, volUp, volDown;
SeekBar pitchSB, speedSB;
EditText speechText;
TextToSpeech textToSpeech;
AudioManager audioManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// getActionBar().hide();

speakBtn = findViewById(R.id.button);
pitchSB = findViewById(R.id.seekPitch);
speedSB = findViewById(R.id.seekSpeed);
speechText = findViewById(R.id.editText);
volUp = findViewById(R.id.volumeUp);
volDown = findViewById(R.id.volumeDown);
audioManager = (AudioManager)
getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
textToSpeech = new TextToSpeech(this, new
TextToSpeech.OnInitListener() {
@Override
public void onInit(int i) {
if(i == TextToSpeech.SUCCESS){
int avail = textToSpeech.isLanguageAvailable(Locale.UK);
if(avail!=TextToSpeech.LANG_NOT_SUPPORTED){
speakBtn.setEnabled(true);
}
else {
speakBtn.setEnabled(false);
}
}
}
});
speakBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
speak();
}
});
volUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
audioManager.adjustVolume(AudioManager.ADJUST_RAISE,
AudioManager.FLAG_PLAY_SOUND);
}
});
volDown.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
audioManager.adjustVolume(AudioManager.ADJUST_LOWER,
AudioManager.FLAG_PLAY_SOUND);
}
});
}

private void speak() {


String speech = speechText.getText().toString();
float picth = pitchSB.getProgress()/50;
float speed = speedSB.getProgress()/50;

if(picth<0.1) picth=0.1f;
if(speed<0.1) speed=0.1f;

textToSpeech.setPitch(picth);
textToSpeech.setSpeechRate(speed);

textToSpeech.speak(speech, TextToSpeech.QUEUE_FLUSH, null, null);


}

@Override
protected void onDestroy() {
if(textToSpeech!=null){
textToSpeech.stop();
textToSpeech.shutdown();
}
super.onDestroy();
}
}

--------------------------------------------------------------------------------------------------------

PROGRAM 8: CALL AND SAVE


package com.example.callandsave_b2;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

ImageButton imageButton;
EditText editText;
Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,bsave,bhash,bstar,bcall;
StringBuilder sb = new StringBuilder();
String phoneNumber;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

imageButton = findViewById(R.id.imageButton);
bsave = findViewById(R.id.save);
bhash = findViewById(R.id.hash);
bcall = findViewById(R.id.callBtn);
bstar = findViewById(R.id.asterisk);
b0 = findViewById(R.id.no0);
b1 = findViewById(R.id.no1);
b2 = findViewById(R.id.no2);
b3 = findViewById(R.id.no3);
b4 = findViewById(R.id.no4);
b5 = findViewById(R.id.no5);
b6 = findViewById(R.id.no6);
b7 = findViewById(R.id.no7);
b8 = findViewById(R.id.no8);
b9 = findViewById(R.id.no9);
editText = findViewById(R.id.number);

if(ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.CALL_PHONE)!=
PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this, new
String[]{Manifest.permission.CALL_PHONE},PackageManager.PERMISSION_GR
ANTED);
}
b0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sb.append("0");
editText.setText(sb.toString());
}
});
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sb.append("1");
editText.setText(sb.toString());
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sb.append("2");
editText.setText(sb.toString());
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sb.append("3");
editText.setText(sb.toString());
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sb.append("4");
editText.setText(sb.toString());
}
});
b5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sb.append("5");
editText.setText(sb.toString());
}
});
b6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sb.append("6");
editText.setText(sb.toString());
}
});
b7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sb.append("7");
editText.setText(sb.toString());
}
});
b8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sb.append("8");
editText.setText(sb.toString());
}
});
b9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sb.append("9");
editText.setText(sb.toString());
}
});
bcall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(validate()){
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+
editText.getText().toString())));
}
}
});
bsave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(validate()){
Intent intent = new Intent(ContactsContract.Intents.Insert.ACTION);
intent.setType(ContactsContract.RawContacts.CONTENT_TYPE);

intent.putExtra(ContactsContract.Intents.Insert.PHONE,editText.getText().toStr
ing());
startActivity(intent);
}
}
});
bstar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sb.append("*");
editText.setText(sb.toString());
}
});
bhash.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sb.append("#");
editText.setText(sb.toString());
}
});
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(validate()){
sb = new StringBuilder(phoneNumber);
if(sb.length()==1){
sb.deleteCharAt(0);
}
else {
sb.deleteCharAt(sb.length()-1);
}
editText.setText(sb.toString());
}
}
});
}

private boolean validate() {


phoneNumber = editText.getText().toString().trim();
if(phoneNumber.isEmpty()){
Toast.makeText(MainActivity.this,"Phone Number field can't be
empty",Toast.LENGTH_SHORT);
return false;
}
else {
editText.setText(phoneNumber);
return true;
}
}
}

You might also like