Assignment No. 1 Activity Life Cycle
Assignment No. 1 Activity Life Cycle
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/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Activity 1"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click"
android:onClick="clickMe"/>
</LinearLayout>
MainActivity.java
package com.example.myfirstapp_btech;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(this,"onCreate method invoked ",Toast.LENGTH_LONG).show();
}
@Override
protected void onStop() {
super.onStop();
Toast.makeText(this,"onstop method invoked ",Toast.LENGTH_LONG).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
Toast.makeText(this,"ondestroy method invoked ",Toast.LENGTH_LONG).show();
}
@Override
protected void onPause() {
super.onPause();
Toast.makeText(this,"onpause method invoked ",Toast.LENGTH_LONG).show();
}
@Override
protected void onResume() {
super.onResume();
Toast.makeText(this,"onResume method invoked ",Toast.LENGTH_LONG).show();
}
@Override
protected void onStart() {
super.onStart();
Toast.makeText(this,"onstart method invoked ",Toast.LENGTH_LONG).show();
}
@Override
protected void onRestart() {
super.onRestart();
Toast.makeText(this,"onrestart method invoked ",Toast.LENGTH_LONG).show();
}
activity_main2.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=".Main2Activity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity 2"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Click"
android:onClick="clickMe"/>
</LinearLayout>
Main2Activity.java
package com.example.myfirstapp_btech;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toast.makeText(this,"onCreate 2 method invoked ",Toast.LENGTH_LONG).show();
}
@Override
protected void onStop() {
super.onStop();
Toast.makeText(this,"onstop 2 method invoked ",Toast.LENGTH_LONG).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
Toast.makeText(this,"ondestroy 2 method invoked ",Toast.LENGTH_LONG).show();
}
@Override
protected void onPause() {
super.onPause();
Toast.makeText(this,"onpause 2 method invoked ",Toast.LENGTH_LONG).show();
}
@Override
protected void onResume() {
super.onResume();
Toast.makeText(this,"onResume 2 method invoked ",Toast.LENGTH_LONG).show();
}
@Override
protected void onStart() {
super.onStart();
Toast.makeText(this,"onstart 2 method invoked ",Toast.LENGTH_LONG).show();
}
@Override
protected void onRestart() {
super.onRestart();
Toast.makeText(this,"onrestart 2 method invoked ",Toast.LENGTH_LONG).show();
}
Manifest file
<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=".Main2Activity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</manifest>
XML: activity_main
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="tmu.ac.in.btechcs.Programe2.Program2Activity">
<EditText
android:textSize="16sp"
android:id="@+id/et_temp"
android:hint="Enter Temp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:textSize="16sp"
android:id="@+id/tv_reault"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RadioGroup
android:id="@+id/rbg"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:text="Celcius"
android:id="@+id/rb_cel"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RadioButton
android:text="Farahnite"
android:id="@+id/rb_far"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RadioGroup>
<Button
android:layout_gravity="center_horizontal"
android:textSize="16sp"
android:text="Convert"
android:id="@+id/bt_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
JAVA: MainActivity.java
package tmu.ac.in.btechcs.Programe2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import tmu.ac.in.btechcs.R;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_program2);
value=findViewById(R.id.et_temp);
group=findViewById(R.id.rbg);
hit=findViewById(R.id.bt_submit);
result=findViewById(R.id.tv_reault);
hit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String uvalue=value.getText().toString();
int value=Integer.parseInt(uvalue);
int bid=group.getCheckedRadioButtonId();
if(bid==R.id.rb_cel){
convertcel(value);
}
if(bid==R.id.rb_far){
convertfar(value);
}
});
}
}
private void convertfar(int value) {
float farvalue=(value*5/9)+32;
result.setText(farvalue+"");
}
}
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">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:hint="Enter First Value"
android:id="@+id/et_value1"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<EditText
android:hint="Enter Second value"
android:id="@+id/et_value2"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_marginTop="16dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/bt_plus"
android:textSize="16sp"
android:text="+"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bt_minus"
android:textSize="16sp"
android:text="-"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bt_multi"
android:textSize="16sp"
android:text="*"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="@+id/bt_div"
android:textSize="16sp"
android:text="/"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:layout_marginTop="16dp"
android:text="Result"
android:layout_gravity="center_horizontal"
android:textSize="16sp"
android:id="@+id/tv_result"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.java
package com.example.mycalculator;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
value1=findViewById(R.id.et_value1);
value2=findViewById(R.id.et_value2);
add=findViewById(R.id.bt_plus);
sub=findViewById(R.id.bt_minus);
multi=findViewById(R.id.bt_multi);
div=findViewById(R.id.bt_div);
Result=findViewById(R.id.tv_result);
add.setOnClickListener(this);
sub.setOnClickListener(this);
multi.setOnClickListener(this);
div.setOnClickListener(this);
}
@Override
public void onClick(View view) {
float val1=Float.parseFloat(value1.getText().toString());
float val2=Float.parseFloat(value2.getText().toString());
float finalresult;
switch (view.getId()){
case R.id.bt_plus:{
finalresult=val1+val2;
Result.setText(finalresult+"");
break;
}
case R.id.bt_minus:{
finalresult=val1-val2;
Result.setText(finalresult+"");
break;
}
case R.id.bt_multi:{
finalresult=val1*val2;
Result.setText(finalresult+"");
break;
}
case R.id.bt_div:{
finalresult=val1/val2;
Result.setText(finalresult+"");
break;
}
}
4.Create the app shown below. Users are initially presented with an "unhappy"
character with the corresponding text "I'm so hungry". After hitting the button
"EAT COOKIE", the character becomes "happy" with corresponding text "I'm so
full".
Java :EmojiActivity.java
package tmu.ac.in.btechcs.P4;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import tmu.ac.in.btechcs.R;
public class EmojiActivity extends AppCompatActivity {
ImageView imageView;
TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_emoji);
imageView=findViewById(R.id.iv_first);
text=findViewById(R.id.tv_emoji);
}
public void change(View view) {
//imageView.setImageResource(R.drawable.imageone);
//text.setText("Now I am using Mask");
Intent intent=new Intent(EmojiActivity.this,SecondEmojiActivity.class);
startActivity(intent);
}
}
-
Second_emoji.java
package tmu.ac.in.btechcs.P4;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import tmu.ac.in.btechcs.R;
public class SecondEmojiActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_emoji);
}
}
XML activity_tip
<?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="tmu.ac.in.btechcs.P5.TipActivity">
<TextView
android:layout_margin="16dp"
android:textSize="15sp"
android:background="@color/tipblue"
android:text="Enter your bill Amount"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:layout_margin="16dp"
android:hint="Enter bill Amount"
android:id="@+id/et_bill"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_margin="16dp"
android:textSize="15sp"
android:background="@color/tipgreen"
android:text="Enter tip Percentage"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:layout_margin="16dp"
android:hint="Enter Tip Percentage"
android:id="@+id/et_percent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:onClick="Calculate"
android:layout_margin="16dp"
android:textColor="@color/tipgreen"
android:background="@color/tipbutton"
android:text="CALCULATE"
android:id="@+id/bt_calculatebill"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_margin="16dp"
android:textSize="20sp"
android:id="@+id/tv_billresult"
android:text="Result:"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
JAVA:TipActivity.java
package tmu.ac.in.btechcs.P5;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import tmu.ac.in.btechcs.R;
public class TipActivity extends AppCompatActivity {
EditText bill,percentage;
TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tip);
bill=findViewById(R.id.et_bill);
percentage=findViewById(R.id.et_percent);
result=findViewById(R.id.tv_billresult);
}
public void Calculate(View view) {
float fbill=Float.parseFloat(bill.getText().toString());//200
float fpercentage=Float.parseFloat(percentage.getText().toString());//5
float finalbill=(fbill*fpercentage)/100;
result.setText(finalbill+"");
}
}
-
Activity_chronometer
<?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="tmu.ac.in.btechcs.ChronoMeterActivity">
<Chronometer
android:layout_margin="16dp"
android:layout_gravity="center_horizontal"
android:id="@+id/chrono"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_margin="16dp"
android:textSize="15sp"
android:text="Start"
android:id="@+id/bt_start"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_margin="16dp"
android:textSize="15sp"
android:text="Stop"
android:id="@+id/bt_stop"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_margin="16dp"
android:textSize="15sp"
android:text="ReStart"
android:id="@+id/bt_restart"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_margin="16dp"
android:textSize="15sp"
android:text="SetFormat"
android:id="@+id/bt_setformat"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_margin="16dp"
android:textSize="15sp"
android:text="ClearFormat"
android:id="@+id/bt_clear"
android:layout_width="match_parent"
android:layout_height="wrap_content" /
Java: chronometeractivity.java
package tmu.ac.in.btechcs;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
public class ChronoMeterActivity extends AppCompatActivity implements
View.OnClickListener {
Chronometer meter;
Button start,stop,restart,setformat,clearformat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chrono_meter);
meter=findViewById(R.id.chrono);
start=findViewById(R.id.bt_start);
stop=findViewById(R.id.bt_stop);
restart=findViewById(R.id.bt_restart);
setformat=findViewById(R.id.bt_setformat);
clearformat=findViewById(R.id.bt_clear);
start.setOnClickListener(this);
stop.setOnClickListener(this);
restart.setOnClickListener(this);
setformat.setOnClickListener(this);
clearformat.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if(view.getId()==R.id.bt_start){
meter.start();
}
if(view.getId()==R.id.bt_stop){
meter.stop();
}
if(view.getId()==R.id.bt_restart){
meter.setBase(SystemClock.elapsedRealtime());
}
if(view.getId()==R.id.bt_setformat){
meter.setFormat("Time(%s)");
}
if(view.getId()==R.id.bt_clear){
meter.setFormat(null);
}
}
}
-
Assignment No7
Automorphic number Armstrong Number Disarium Number
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:hint="Enter number"
android:id="@+id/et_number"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="Automorphic"
android:id="@+id/Automorphic"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="Armstrong"
android:id="@+id/Armstrong"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:text="Disarium"
android:id="@+id/Disarium"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity.java
package com.example.p7btech;
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;
EditText value;
Button Automorphic,Armstrong,Disarium;
@Override
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
value=findViewById(R.id.et_number);
Automorphic=findViewById(R.id.Automorphic);
Armstrong=findViewById(R.id.Armstrong);
Disarium=findViewById(R.id.Disarium);
Armstrong.setOnClickListener(this);
Automorphic.setOnClickListener(this);
Disarium.setOnClickListener(this);
@Override
switch (view.getId()){
case R.id.Automorphic:{
boolean check=
checkAutomorphic(Integer.parseInt(value.getText().toString()));
if(check)
break;
case R.id.Armstrong:{
boolean check=
checkArmstrong(Integer.parseInt(value.getText().toString()));
if(check)
else
break;
case R.id.Disarium:{
boolean
check=checkDisarium(Integer.parseInt(value.getText().toString()));
if(check)
else
break;
while (N > 0) {
if (N % 10 != sq % 10)
return false;
N /= 10;
sq /= 10;
return true;
if(square.endsWith(str_num))
return true;
else
return false;
} */
private boolean checkArmstrong(int x) {
int n = order(x);
int temp = x;
double sum = 0;
while (temp != 0) {
sum=sum+ Math.pow(r,n);
// Count digits in n.
// power of position
int x = n;
while (x!=0)
{
// Get the rightmost digit
int r = x%10;
// the positions
x = x/10;
int result=1;
if( y == 0)
return 1;
if (y%2 == 0)
while (y != 0)
result *= x;
--y;
}
return result;
int n = 0;
while (x != 0)
n++;
x = x/10;
return n;