Record Program
Record Program
Source code:-
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">
<EditText
android:id="@+id/edt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:hint="0"
android:textColor="#9C27B0"
android:textSize="30sp" />
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tableLayout"
android:layout_margin="35dp">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/button1"
style="@style/Widget.AppCompat.Button.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:layout_marginRight="20dp"
android:text="1" />
<Button
android:id="@+id/button2"
style="@style/Widget.AppCompat.Button.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:layout_marginRight="20dp"
android:text="2" />
<Button
android:id="@+id/button3"
style="@style/Widget.AppCompat.Button.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:layout_marginRight="20dp"
android:text="3" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button4"
style="@style/Widget.AppCompat.Button.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginRight="20dp"
android:text="4" />
<Button
android:id="@+id/button5"
style="@style/Widget.AppCompat.Button.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginRight="20dp"
android:text="5" />
<Button
android:id="@+id/button6"
style="@style/Widget.AppCompat.Button.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginRight="20dp"
android:text="6" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/button7"
style="@style/Widget.AppCompat.Button.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginRight="20dp"
android:text="7" />
<Button
android:id="@+id/button8"
style="@style/Widget.AppCompat.Button.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginRight="20dp"
android:text="8" />
<Button
android:id="@+id/button9"
style="@style/Widget.AppCompat.Button.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginRight="20dp"
android:text="9" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/buttondot"
style="@style/Widget.AppCompat.Button.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginRight="20dp"
android:text="." />
<Button
android:id="@+id/button0"
style="@style/Widget.AppCompat.Button.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginRight="20dp"
android:text="0" />
<Button
android:id="@+id/buttonC"
style="@style/Widget.AppCompat.Button.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginRight="20dp"
android:text="C" />
</TableRow>
</TableLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="220dp"
android:layout_marginTop="87dp"
android:orientation="vertical">
<Button
android:id="@+id/buttonadd"
style="@style/Widget.AppCompat.Button.Small"
android:layout_width="100dp"
android:layout_height="45dp"
android:layout_marginTop="8dp"
android:layout_marginLeft="20dp"
android:text="+" />
<Button
android:id="@+id/buttonsub"
style="@style/Widget.AppCompat.Button.Small"
android:layout_width="100dp"
android:layout_height="45dp"
android:layout_marginTop="45dp"
android:layout_marginLeft="20dp"
android:text="-" />
<Button
android:id="@+id/buttonmul"
style="@style/Widget.AppCompat.Button.Small"
android:layout_width="100dp"
android:layout_height="45dp"
android:layout_marginTop="45dp"
android:layout_marginLeft="20dp"
android:text="*" />
<Button
android:id="@+id/buttondiv"
style="@style/Widget.AppCompat.Button.Small"
android:layout_width="100dp"
android:layout_height="45dp"
android:layout_marginTop="45dp"
android:layout_marginLeft="20dp"
android:text="/" />
</LinearLayout>
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/tableLayout"
>
<Button
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="="
android:layout_marginLeft="35dp"
android:id="@+id/buttonequal"/>
</GridLayout>
</RelativeLayout>
MainActivity.java
package com.example.calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
Source code:-
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:id="@+id/hints"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="*Press START button to start the countdown timer"
android:textSize="20sp" />
<TextView
android:id="@+id/countdown_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text=""
android:textSize="56sp" />
<Button
android:id="@+id/countdown_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/countdown_text"
android:layout_centerHorizontal="true"
android:text="Start"
android:textSize="30sp" />
<Button
android:id="@+id/pauseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/countdown_button"
android:layout_centerHorizontal="true"
android:text="Pause"
android:textSize="24sp"
android:visibility="invisible" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/pauseButton"
android:layout_centerHorizontal="true"
android:text="Reset"
android:visibility="invisible" />
</RelativeLayout>
MainActivity.java:-
package com.example.eventlisteners;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Locale;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
note = (TextView) findViewById(R.id.hints);
countdownText = (TextView) findViewById(R.id.countdown_text);
countdownButton = (Button) findViewById(R.id.countdown_button);
pause = (Button) findViewById(R.id.pauseButton);
reset = (Button) findViewById(R.id.stop);
pause.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
pauseTime();
return false;
}
});
reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
lastTouchTime = currentTouchTime;
currentTouchTime = System.currentTimeMillis();
if (currentTouchTime - lastTouchTime < 250) {
resetted();
lastTouchTime = 0;
currentTouchTime = 0;
}
}
});
countdownButton.setOnClickListener((new View.OnClickListener() {
@Override
public void onClick(View v) {
if (timerRunning) {
pauseTime();
} else {
startTime();
}
}
}));
updateTimer();
}
public void startTime()
{
countDownTimer=new CountDownTimer(timeLeftInMilliseconds,1000) {
@Override
public void onTick(long millisUntilFinished) {
timeLeftInMilliseconds=millisUntilFinished;
updateTimer();
note.setText("*Longpress the Pause Button to pause the Timer");
reset.setVisibility(View.INVISIBLE);
pause.setVisibility(View.VISIBLE);
countdownButton.setVisibility(View.INVISIBLE);
}
@Override
public void onFinish() {
timerRunning=false;
countdownButton.setText("Start");
countdownButton.setVisibility(View.INVISIBLE);
}
}.start();
countdownButton.setText("Pause");
timerRunning=true;
}
public void pauseTime()
{
countDownTimer.cancel();
countdownButton.setText("Start");
timerRunning=false;
note.setText("*Press START button to continue the timer \n * Double click the RESET
button to Reset the timer");
pause.setVisibility(View.INVISIBLE);
reset.setVisibility(View.VISIBLE);
countdownButton.setVisibility((View.VISIBLE));
}
public void updateTimer()
{
int minutes=(int)(timeLeftInMilliseconds/1000)/60;
int seconds=(int)(timeLeftInMilliseconds/1000)%60;
String timeLeftFormatted=
String.format(Locale.getDefault(),"%02d:%02d",minutes,seconds);
countdownText.setText(timeLeftFormatted);
}
public void resetted()
{
timeLeftInMilliseconds=START_TIME_IN_MILLIS;
updateTimer();
reset.setVisibility(View.INVISIBLE);
pause.setVisibility(View.INVISIBLE);
note.setText("* Press START button to start the countdown timer");
}
}
Output:-
Develop an application that uses Adapters and Toast
Source code:-
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">
<LinearLayout
android:id="@+id/linear_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:text="Category:"
android:textSize="20dp" />
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="30dp" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/products"
android:visibility="invisible"/>
<EditText
android:id="@+id/et1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_weight="2"
android:gravity="top"
android:hint="Enter your feedback here" />
<RadioGroup
android:id="@+id/radiogroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Hated it" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Not Bad"/>
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Average"/>
<RadioButton
android:id="@+id/radio3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Good"/>
<RadioButton
android:id="@+id/radio4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Love it! "/>
</RadioGroup>
<RatingBar
android:id="@+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="180dp"
android:text="Submit" />
</RelativeLayout>
MainActivity.java:-
package com.example.adapter_toast;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
brand.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item=parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(),"Product:
"+item,Toast.LENGTH_LONG).show();
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
List<String>categories=new ArrayList<>();
categories.add("Mobile");
categories.add("Laptop");
categories.add("Tab");
categories.add("TV");
categories.add("iPod");
ArrayAdapter<String>dataAdapter=new
ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("category",String.valueOf(spinner.getSelectedItem()));
intent.putExtra("brand",String.valueOf(brand.getSelectedItem()));
intent.putExtra("checked",checked[0]);
intent.putExtra("messages",e.getText().toString());
intent.putExtra("rating",prog);
Toast.makeText(getApplicationContext(),"Feedback:"+e.getText().toString()+"\n
Rating:"+prog+"\n" +checked[0],Toast.LENGTH_LONG).show();
startActivity(intent);
}
});
}
activity_second.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=".SecondActivity">
<TextView
android:id="@+id/txt_bundle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="103dp"
android:textSize="20sp"
android:text="Empty"
android:textColor="@android:color/black"
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
</RelativeLayout>
SecondActivity.java:-
package com.example.adapter_toast;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
TextView textView=(TextView)findViewById(R.id.txt_bundle);
Bundle bundle=getIntent().getExtras();
String data=bundle.get("category").toString();
String product=bundle.get("brand").toString();
Intent intent=getIntent();
String selectedRadioValue=intent.getStringExtra("checked");
String messages=intent.getStringExtra("messages");
float rating=bundle.getFloat("rating");
textView.setText("Category\t\t\t\t: "+data+"\nBrand\t\t\t\t\t\t\t\t:
"+product+"\nSatisfaction\t: "+selectedRadioValue+"\n"+messages+"\nMy Rating\t\t\t:
"+rating);
}
}
strings.xml:-
<resources>
<string name="app_name">Adapter_Toast</string>
<string-array name="Mobile">
<item>Xiaomi Mi</item>
<item>Samsung</item>
<item>Realme</item>
<item>Apple</item>
<item>ONEPLUS</item>
</string-array>
<string-array name="Laptops">
<item>HP</item>
<item>Lenovo</item>
<item>Asus</item>
<item>Dell</item>
<item>MacBook</item>
</string-array>
<string-array name="TV">
<item>Xiaomi Mi</item>
<item>Samsung</item>
<item>BPL</item>
<item>TCL</item>
<item>LG</item>
</string-array>
<string-array name="Tab">
<item>Samsung Galaxy</item>
<item>Honor</item>
<item>iBall</item>
<item>Lenovo</item>
<item>Others</item>
</string-array>
<string-array name="ipod">
<item>Others</item>
</string-array>
</resources>
Output:
Develop an application that makes use of database
Source code:-
Activity_main.xml
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:layout_marginTop="21dp"
android:text="Reg.No."
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView1"
android:layout_marginLeft="40dp"
android:layout_marginTop="-11dp"
android:layout_toRightOf="@+id/textView1"
android:ems="10"
android:inputType="number">
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText1"
android:layout_alignLeft="@+id/textView1"
android:layout_marginTop="35dp"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignLeft="@+id/editText1"
android:layout_alignBottom="@+id/textView2"
android:layout_marginLeft="0dp"
android:layout_marginBottom="2dp"
android:ems="10" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText2"
android:layout_alignLeft="@+id/textView2"
android:layout_marginLeft="0dp"
android:layout_marginTop="35dp"
android:text="Percentage"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText2"
android:layout_alignBottom="@+id/textView3"
android:layout_marginLeft="5dp"
android:layout_marginBottom="-13dp"
android:ems="10"
android:inputType="none" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView3"
android:layout_marginLeft="40dp"
android:layout_marginTop="50dp"
android:text="ADD" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignLeft="@+id/editText3"
android:layout_alignBottom="@+id/button1"
android:layout_marginLeft="35dp"
android:layout_marginBottom="3dp"
android:text="VIEW" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button2"
android:layout_alignBottom="@+id/button2"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"
android:layout_toEndOf="@+id/button2"
android:layout_toRightOf="@+id/button2"
android:text="VIEW ALL" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_alignLeft="@+id/button1"
android:layout_marginLeft="50dp"
android:layout_marginTop="35dp"
android:text="UPDATE" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button4"
android:layout_alignBottom="@+id/button4"
android:layout_marginLeft="50dp"
android:layout_marginBottom="3dp"
android:layout_toRightOf="@+id/button4"
android:text="DELETE" />
</RelativeLayout>
MainActivity.java
package com.example.database;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
EditText name,regno,percentage;
Button btnAdd,btnDelete,btnUpdate,btnView,btnViewAll;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
regno= (EditText)findViewById(R.id.editText1);
name= (EditText)findViewById(R.id.editText2);
percentage=(EditText)findViewById(R.id.editText3);
btnAdd=(Button)findViewById(R.id.button1);
btnView=(Button)findViewById(R.id.button2);
btnViewAll=(Button)findViewById(R.id.button3);
btnUpdate=(Button)findViewById(R.id.button4);
btnDelete=(Button)findViewById(R.id.button5);
db=openOrCreateDatabase("Students", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS student(regno VARCHAR,name
VARCHAR,percentage VARCHAR);");
btnAdd.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View arg0) {
if(regno.getText().toString().trim().length()==0||name.getText().toString().trim().length()==0||pe
rcentage.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter all values");
return;
}
db.execSQL("INSERT INTO student
VALUES('"+regno.getText()+"','"+name.getText()+"','"+percentage.getText()+"');");
showMessage("Success", "Record added");
clearText();
}
});
btnDelete.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
if(regno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Reg. No.");
return; }
Cursor c=db.rawQuery("SELECT * FROM student WHERE
regno='"+regno.getText()+"'", null);
if(c.moveToFirst())
{
db.execSQL("DELETE FROM student WHERE
regno='"+regno.getText()+"'");
showMessage("Success", "Record Deleted");
}else
{
showMessage("Error", "Invalid Reg. No.");
}
clearText();
}
});
btnUpdate.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
if(regno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Reg. No.");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE
regno='"+regno.getText()+"'", null);
if(c.moveToFirst())
{
db.execSQL("UPDATE student SET
name='"+name.getText()+"',percentage='"+percentage.getText()+"' WHERE
regno='"+regno.getText()+"'");
showMessage("Success", "Record Modified");
} else
{
showMessage("Error", "Invalid Reg. No.");
} clearText();
}
});
btnView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
if(regno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Reg. No.");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE
regno='"+regno.getText()+"'", null);
if(c.moveToFirst())
{
name.setText(c.getString(1));
percentage.setText(c.getString(2));
} else
{
showMessage("Error", "Invalid Reg. No.");
clearText();
}
}
});
btnViewAll.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
Cursor c=db.rawQuery("SELECT * FROM student", null);
if(c.getCount()==0)
{
showMessage("Error", "No records found");
return;
}
StringBuffer buffer=new StringBuffer();
while(c.moveToNext())
{
buffer.append("Reg. No : "+c.getString(0)+"\n");
buffer.append("Name : "+c.getString(1)+"\n");
buffer.append("Percentage : "+c.getString(2)+"\n\n");
}
showMessage("Student Details", buffer.toString());
}
});
}
public void showMessage(String title,String message)
{
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
public void clearText()
{
regno.setText("");
name.setText("");
percentage.setText("");
regno.requestFocus();
}
Output:-
Add:
View:
View All:
Update:
Delete:
Develop an application that makes use of RSS Feed.
Source code:-
AndroidManifest.xml
</manifest>
Activity_main.xml
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.java
package com.example.rss_feed;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
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.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new MyAsyncTask().execute();
}
class MyAsyncTask extends AsyncTask<Object,Void, ArrayAdapter>
{
@Override
protected ArrayAdapter doInBackground(Object[]params)
{
headlines=new ArrayList();
links=new ArrayList();
try {
URL url = new URL("https://codingconnect.net/feed/");
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(false);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(getInputStream(url), "UTF_8");
boolean insideItem = false;
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG)
{
if (xpp.getName().equalsIgnoreCase("item"))
{
insideItem = true;
}
else if (xpp.getName().equalsIgnoreCase("title"))
{
if (insideItem) {
headlines.add(xpp.nextText()); //extract the headline
}
}
else if (xpp.getName().equalsIgnoreCase("link"))
{
if (insideItem) {
links.add(xpp.nextText());
}
}
} else if (eventType == XmlPullParser.END_TAG &&
xpp.getName().equalsIgnoreCase("item"))
{
insideItem = false;
}
eventType = xpp.next();
}
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (XmlPullParserException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
}
protected void onPostExecute(ArrayAdapter adapter)
{
adapter=new
ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,headlines);
setListAdapter(adapter);
}
}
@Override
protected void onListItemClick(ListView l, View v,int position,long id)
{
Uri uri=Uri.parse((links.get(position)).toString());
Intent intent=new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
}
public InputStream getInputStream(URL url)
{
try {
return url.openConnection().getInputStream();
}
catch (IOException e)
{
return null;
}
}
}
Output:-
Develop an application that implements Multi threading
Source code:-
Main.xml
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBar1"
android:layout_centerHorizontal="true"
android:text=" "
android:textAppearance="?android:attr/textAppearanceLarge"/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:text="Start Progress"/>
</RelativeLayout>
MainActivity.java
package com.example.multithreading;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ProgressBar p=(ProgressBar)findViewById(R.id.progressBar1);
final TextView t=(TextView)findViewById(R.id.textView1);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Runnable r=new Runnable() {
@Override
public void run() {
for(int i=0;i<=100;i++)
{
final int temp=i;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
p.post(new Runnable() {
@Override
public void run() {
p.setProgress(temp);
t.setText(temp+"%");
}
});
}
}};
new Thread(r).start();
}
});
}
}
Output:-
Develop an application that uses GPS location information
Source code:-
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gps_location">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Activity_main.xml:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click the Button to show Latitude and Longitude" />
<Button
android:id="@+id/GetLoc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get Location" />
</LinearLayout>
MainActivity.java:-
package com.example.gps_location;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
GPSTracker.java:-
package com.example.gps_location;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
import androidx.core.content.ContextCompat;
if(ContextCompat.checkSelfPermission(context,android.Manifest.permission.ACCESS_FINE_
LOCATION)!=PackageManager.PERMISSION_GRANTED)
{
Toast.makeText(context,"Permission Not Granted",Toast.LENGTH_SHORT).show();
return null;
}
LocationManager lm = (LocationManager)
context.getSystemService(context.LOCATION_SERVICE);
boolean isGPSEnabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(isGPSEnabled)
{
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000,1,this);
Location l=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
return l;
}else{
Toast.makeText(context,"Please Enable GPS
location",Toast.LENGTH_LONG).show();
}
return null;
}
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
Output:-
Develop an application that reads and writes data in SD card.
Source code:-
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sdcard">
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-
permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Activity_main.xml:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Filename"
android:id="@+id/filename"
android:layout_marginTop="3dp"
android:gravity="top"/>
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:hint="Enter any Text"
android:id="@+id/et1"
android:layout_marginTop="30dp"
android:layout_weight="2"
android:gravity="top"/>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Write Message"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:onClick="writemessage"/>
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Read Message"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:onClick="readmessage"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="Hello world"
android:id="@+id/tv1"
android:layout_marginTop="10dp"
android:gravity="center"
android:layout_weight="1"
android:textAppearance="?android:textAppearanceLarge"/>
</LinearLayout>
MainActivity.java:-
package com.example.sdcard;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
EditText et,filename;
TextView tv;
String filenames;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et =(EditText)findViewById(R.id.et1);
filename =(EditText)findViewById(R.id.filename);
tv =(TextView)findViewById(R.id.tv1);
tv.setVisibility(View.GONE);//hide TextView
}
public void writemessage(View view) {
String state;//check External device available or not.
state = Environment.getExternalStorageState();//get state of the External storage.
if (Environment.MEDIA_MOUNTED.equals(state)) // is available then if is executed
{
File root= Environment.getExternalStorageDirectory();
File dir=new File(root.getAbsolutePath()+"/MyFile");//Create new Folder
if(!dir.exists())//new folder already available or not
{
dir.mkdir();
}
filenames=filename.getText().toString().trim();
File file=new File(dir,filenames+".txt");
String message=et.getText().toString();
try {
FileOutputStream fos=new FileOutputStream(file);
fos.write(message.getBytes());
fos.close();
et.setText("");
Toast.makeText(getApplicationContext(),"Message saved successfully with
MyFile/"+filenames+".txt",Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
else
{
Toast.makeText(getApplicationContext(),"External storage isn't
found",Toast.LENGTH_LONG).show();
}
}
public void readmessage(View view)
{
String state;//check External device available or not.
state = Environment.getExternalStorageState();//get state of the External storage.
if (Environment.MEDIA_MOUNTED.equals(state)) // is available then if is execution
{
File root = Environment.getExternalStorageDirectory();
File dir=new File(root.getAbsolutePath()+"/MyFile");
filenames=filename.getText().toString().trim();
File file=new File(dir,filenames+".txt");
tv.setText("");
String message;
try {
FileInputStream fis=new FileInputStream(file);
InputStreamReader isr=new InputStreamReader(fis);
BufferedReader br=new BufferedReader(isr);
StringBuffer sb=new StringBuffer();
while ((message=br.readLine())!=null)
{
sb.append(message+"\n");
}
tv.setText(sb.toString());
tv.setVisibility(View.VISIBLE);
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
`Output:-
Develop an application that creates an alert upon receiving a message
Source code:-
Activity_main.xml:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message"
android:textSize="30sp" />
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="30sp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="30dp"
android:layout_gravity="center"
android:text="Notify"
android:textSize="30sp" />
</LinearLayout>
MainActivity.java
package com.knowledgeentertain.notification;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notificationManager=NotificationManagerCompat.from(this);
notify.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String text=e.getText().toString();
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
PendingIntent pending = PendingIntent.getActivity(MainActivity.this, 1,
intent.putExtra(EXTRA_TEXT,text), PendingIntent.FLAG_UPDATE_CURRENT);
Activity_second.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=".SecondActivity">
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Nothing to show Messages"
android:textSize="30sp"
android:text=""/>
</RelativeLayout >
SecondActivity.java
package com.knowledgeentertain.notification;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent=getIntent();
String text;
text=intent.getStringExtra("smsAddress");
TextView tv=(TextView)findViewById(R.id.tv1);
tv.setText(null);
tv.setText(text+"");
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.knowledgeentertain.notification">
<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".SecondActivity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
App.java
package com.knowledgeentertain.notification;
import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
Source code:-
Main.xml
packagein.edu.tpgit.Calculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivityimplements View.OnClickListener
{
private Button[][] buttons=new Button[3][3];
private boolean player1Turn=true;
private int roundCount;
private int player1Points;
private int player2Points;
private TextView textViewPlayer1;
private TextView textViewPlayer2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewPlayer1 =findViewById(R.id.text_view_p1);
textViewPlayer2 =findViewById(R.id.text_view_p2);
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
String buttonID="button_"+i+j;
int resID=getResources().getIdentifier(buttonID,"id",getPackageName());
buttons[i][j]=findViewById(resID);
buttons[i][j].setOnClickListener(this);
}
}
Button buttonReset=findViewById(R.id.button_reset);
buttonReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
resetGame();
}
});
}
@Override
public void onClick(View v) {
if (!((Button) v).getText().toString().equals("")) {
return;
}
if (player1Turn) {
((Button) v).setText("X");
} else {
((Button) v).setText("O");
}
roundCount++;
if(checkForWin())
{
if(player1Turn)
{
player1Wins();
}
else
{
player2Wins();
}
}
else if(roundCount==9)
{
draw();
}
else
{
player1Turn= !player1Turn;
}
}
private boolean checkForWin()
{
String[][] field=new String[3][3];
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
field[i][j]=buttons[i][j].getText().toString();
}
}
for(int i=0;i<3;i++)
{
if(field[i][0].equals(field[i][1])
&&field[i][0].equals(field[i][2])
&& !field[i][0].equals(""))
{
return true;
}
}
for(int i=0;i<3;i++)
{
if(field[0][i].equals(field[1][i])
&&field[0][i].equals(field[2][i])
&& !field[0][i].equals(""))
{
return true;
}
}
if(field[0][0].equals(field[1][1])
&&field[0][0].equals(field[2][2])
&& !field[0][0].equals(""))
{
return true;
}
if(field[0][2].equals(field[1][1])
&&field[0][2].equals(field[2][0])
&& !field[0][2].equals(""))
{
return true;
}
return false;
}
private void player1Wins()
{
player1Points++;
Toast.makeText(this,"Player 1 Wins!!",Toast.LENGTH_SHORT).show();
updatePointText();
resetBoard();
}
private void player2Wins()
{
player2Points++;
Toast.makeText(this,"Player 2 Wins!!",Toast.LENGTH_SHORT).show();
updatePointText();
resetBoard();
}
private void draw()
{
Toast.makeText(this,"Draw match",Toast.LENGTH_SHORT).show();
resetBoard();
}
private void updatePointText()
{
textViewPlayer1.setText("Player 1:"+player1Points);
textViewPlayer2.setText("Player 2:"+player2Points);
}
private void resetBoard()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
buttons[i][j].setText("");
}
}
roundCount=0;
player1Turn=true;
}
private void resetGame()
{
player1Points=0;
player2Points=0;
updatePointText();
resetBoard() } }
Output:-
Develop an application that implements Native Calculator
Source code:-
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp">
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal"
android:textSize="20sp" />
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp">
<Button
android:id="@+id/Add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="+"
android:textSize="30sp"/>
<Button
android:id="@+id/Sub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="-"
android:textSize="30sp"/>
<Button
android:id="@+id/Mul"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="*"
android:textSize="30sp"/>
<Button
android:id="@+id/Div"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="/"
android:textSize="30sp"/>
</LinearLayout>
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="The Answer is"
android:textSize="30sp"
android:gravity="center"/>
</LinearLayout>
MainActivity.java
package com.example.native_calculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// set a listener
Add.setOnClickListener(this);
Sub.setOnClickListener(this);
Mul.setOnClickListener(this);
Div.setOnClickListener(this);
}
@Override
public void onClick (View v)
{
float num1 = 0;
float num2 = 0;
float result = 0;
String oper = "";
// defines the button that has been clicked and performs the corresponding operation
// write operation into oper, we will use it later for output
switch (v.getId())
{
case R.id.Add:
oper = "+";
result = num1 + num2;
break;
case R.id.Sub:
oper = "-";
result = num1 - num2;
break;
case R.id.Mul:
oper = "*";
result = num1 * num2;
break;
case R.id.Div:
oper = "/";
result = num1 / num2;
break;
default:
break;
}
// form the output line
Result.setText(num1 + " " + oper + " " + num2 + " = " + result);
}
}
Output:
Develop an application that implements Multithreading using images
Source code:-
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_gravity="center"
android:layout_margin="50dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10dp"
android:text="Start" />
</LinearLayout>
MainActivity.java
package com.example.multithreading_images;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
Source code:-
Activity_main.xml
<TextView
android:id="@+id/countdown_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text=""
android:textSize="20sp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/countdown_text"
android:layout_centerHorizontal="true"
android:text="Close App"
android:textSize="30sp" />
</RelativeLayout>
MainActivity.java
package com.example.alertdialogbox;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
Source code:-
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">
<ToggleButton
android:text="Convert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/toggleButton"
android:textOn="C->F"
android:textOff="F->C"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal|numberSigned"
android:hint="Give the temperature"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp"
android:id="@+id/editText" />
<Button
android:text="Submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/toggleButton"
android:layout_alignStart="@+id/toggleButton"
android:layout_marginTop="42dp"
android:id="@+id/button"
android:layout_alignLeft="@+id/toggleButton" />
</RelativeLayout>
MainActivity.java
package com.example.temp_conversion;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ToggleButton;
Button b1;
EditText et;
ToggleButton tb;
Double a;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et=(EditText) findViewById(R.id.editText);
b1=(Button) findViewById(R.id.button);
tb=(ToggleButton) findViewById(R.id.toggleButton);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(et.getText().toString().isEmpty())
{
Toast.makeText(MainActivity.this,"Please enter the
temperature",Toast.LENGTH_SHORT).show();
}
else if(tb.isChecked())
{
a=Double.parseDouble(String.valueOf(et.getText()));
Double b=a*9/5+32;
String r=String.valueOf(b);
Toast.makeText(MainActivity.this,r+"°F",Toast.LENGTH_SHORT).show();
}
else
{
a=Double.parseDouble(String.valueOf(et.getText()));
Double b=a-32;
Double c=b*5/9;
String r=String.valueOf(c);
Toast.makeText(MainActivity.this,r+"°C",Toast.LENGTH_SHORT).show();
}
}
});
}
}
Output:
Develop an Android program to implement Date Picker
Source code:-
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">
<DatePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="-25dp"
android:scaleX=".9"
android:scaleY=".7"
android:calendarTextColor="#44ff00"
android:datePickerMode="calendar"
android:id="@+id/datePicker" />
<Button
android:text="Display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/datePicker"
android:layout_marginTop="-25dp"
android:layout_centerHorizontal="true"
android:id="@+id/button" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button"
android:layout_marginTop="20dp"
android:textSize="24sp"
android:id="@+id/textview"
android:gravity="center"/>
</RelativeLayout>
MainActivity.java
package com.example.date_picker;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
DatePicker dp;
Button b1;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dp=(DatePicker) findViewById(R.id.datePicker);
b1=(Button) findViewById(R.id.button);
tv=(TextView) findViewById(R.id.textview);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer sb=new StringBuffer();
sb.append(dp.getDayOfMonth()+"/");
sb.append((dp.getMonth()+1)+"/");
sb.append(dp.getYear());
tv.setText(sb.toString());
}
});
}
}
Output: