Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Practical 15,16 MAD

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Practical 15

<?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">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World Toast Example"
android:textSize="18dp"
android:paddingLeft="10dp"
android:paddingTop="10dp"
/>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Toast"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:textSize="20dp"
android:onClick="showToast"/>

</LinearLayout>

Java code
package com.example.toastmsg;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1=(Button) findViewById(R.id.btn1);

}
public void showToast(View v){
Toast.makeText(this, "Message fo you:\nYou have a new Mail!!",
Toast.LENGTH_LONG).show();
}
}

q.2
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout 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">

<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="6dp"
android:layout_y="15dp"
android:text="Coffee"
android:paddingLeft="5dp"
android:textSize="17dp"/>

<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="6dp"
android:layout_y="77dp"
android:text="Pizza"
android:paddingLeft="5dp"
android:textSize="17dp"/>

<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="5dp"
android:layout_y="152dp"
android:text="Burger"
android:paddingLeft="5dp"
android:textSize="17dp"/>

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="148dp"
android:layout_y="232dp"
android:text="Order"
android:textSize="25dp"
android:onClick="showorder"
/>
</AbsoluteLayout>

Java code
package com.example.toastmessage2;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


CheckBox c1,c2,c3;
Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
c1=(CheckBox) findViewById(R.id.checkBox);
c2=(CheckBox) findViewById(R.id.checkBox2);
c3=(CheckBox) findViewById(R.id.checkBox3);
b1=(Button) findViewById(R.id.button);
}
public void showorder(View v){
String result="";
int total=0;
if(c1.isChecked()){
result+=c1.getText().toString()+" :70 Rs\n";
total+=70;
}
if(c2.isChecked()){
result+=c2.getText().toString()+" :150 Rs\n";
total+=150;
}
if(c3.isChecked()){
result+=c3.getText().toString()+" :180 Rs\n";
total+=180;
}
if(result!=null){
Toast.makeText(this, "Ordered Items are:\n"+result, Toast.LENGTH_LONG).show();
Toast.makeText(this, "Total Amount is:"+total, Toast.LENGTH_LONG).show();

}
}
}

Practical no.16
package com.example.activitylifecycle;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("LifeCycle","Activity Created");
Toast.makeText(this, "On create called", Toast.LENGTH_SHORT).show();
}
protected void onStart(){
super.onStart();
Log.d("LifeCycle","Activity Created");
Toast.makeText(this, "On Create Called", Toast.LENGTH_SHORT).show();
}
protected void onResume(){
super.onResume();
Log.d("LifeCycle","Activity Created");
Toast.makeText(this, "On Resume Called", Toast.LENGTH_SHORT).show();
}
protected void onPause(){
super.onPause();
Log.d("LifeCycle","Activity Created");
Toast.makeText(this, "On Pause Called", Toast.LENGTH_SHORT).show();
}
protected void onStop(){
super.onStop();
Log.d("LifeCycle","Activity Created");
Toast.makeText(this, "On Stop Called", Toast.LENGTH_SHORT).show();
}
protected void onDestroy(){
super.onDestroy();
Log.d("LifeCycle","Activity Created");
Toast.makeText(this, "On Destroy Called", Toast.LENGTH_SHORT).show();
}
}

You might also like