Practical No 15

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

Practical No 15

Q1.
activity_main.xml
<?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"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="309dp"
android:layout_height="603dp"
android:layout_marginStart="16dp"
android:layout_marginTop="40dp"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello world , Toast
Example" />

<Button
android:id="@+id/btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.example.practical15;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.*;
import android.widget.*;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = findViewById(R.id.btn);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View view =
getLayoutInflater().inflate(R.layout.toast_ayout,(ViewGroup)
findViewById(R.id.layoyt_toast));
Toast toast = new Toast(getApplicationContext());
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_VERTICAL,0,0);
toast.setView(view);
toast.show();

}
});
}
}
Q2.
activity_main.xml
<?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">
<LinearLayout
android:layout_width="309dp"
android:layout_height="603dp"
android:layout_marginTop="40dp"
android:orientation="vertical"
android:gravity="center_horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<CheckBox
android:id="@+id/ch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pizza"/>
<CheckBox
android:id="@+id/ch2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cofffe"/>
<CheckBox
android:id="@+id/ch3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Burger"/>
<Space
android:layout_width="match_parent"
android:layout_height="190dp"/>
<Button
android:id="@+id/btn"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="ORDER"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java
package com.example.practical15;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = findViewById(R.id.btn);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox ch1 = findViewById(R.id.ch1);
CheckBox ch2 = findViewById(R.id.ch2);
CheckBox ch3 = findViewById(R.id.ch3);
String str =
Math.calculate(ch1.isChecked(),ch2.isChecked(),ch3.isChecked());
if(ch1.isChecked()||ch2.isChecked()||ch3.isChecked())
Toast.makeText(MainActivity.this,str,Toast.LENGTH_LONG).show();
}
});
}
}

You might also like