Ex 15
Ex 15
com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- Button's for simple and custom Toast -->
<Button
android:id="@+id/simpleToast"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp"
android:background="#f00"
android:text="Simple Toast"
android:textColor="#fff"
android:textSize="20sp" />
<Button
android:id="@+id/customToast"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="@+id/simpleToast"
android:layout_centerHorizontal="true"
android:layout_margin="50dp"
android:background="#0f0"
android:text="Custom Toast"
android:textColor="#fff"
android:textSize="20sp" />
</RelativeLayout>
package com.example.helloworld;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Button;
import android.view.ViewGroup;
import androidx.appcompat.app.AppCompatActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get the reference of Button's
simpleToast = (Button) findViewById(R.id.simpleToast);
customToast = (Button) findViewById(R.id.customToast);
// perform setOnClickListener event on simple Toast Button
simpleToast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// initiate a Toast with message and duration
Toast toast = Toast.makeText(getApplicationContext(), "Simple Toast In
Android", Toast.LENGTH_LONG); // initiate the Toast with context, message and duration
for the Toast
toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
// set gravity for the Toast.
toast.show(); // display the Toast
}
});
// perform setOnClickListener event on custom Toast Button
customToast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Retrieve the Layout Inflater and inflate the layout from xml
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.activity_toast_custom_view,
(ViewGroup) findViewById(R.id.toast_layout_root));
// get the reference of TextView and ImageVIew from inflated layout
TextView toastTextView = (TextView)
layout.findViewById(R.id.toastTextView);
ImageView toastImageView = (ImageView)
layout.findViewById(R.id.toastImageView);
// set the text in the TextView
toastTextView.setText("Custom Toast In Android");
// set the Image in the ImageView
}
});
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/custom_toast_background"
android:orientation="horizontal"
android:padding="8dp"
>
<!-- ImageVView and TextView for custom Toast -->
<ImageView
android:id="@+id/toastImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp" />
<TextView
android:id="@+id/toastTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF" />
</LinearLayout>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</shape>
package com.example.helloworld;
import androidx.appcompat.app.AppCompatActivity;
import android.os.*;
import android.view.*;
import android.widget.*;
CheckBox cb1,cb2,cb3;
Button b1 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cb1=(CheckBox)findViewById(R.id.cbi1);
cb2=(CheckBox)findViewById(R.id.cbi2);
cb3=(CheckBox)findViewById(R.id.cbi3);
b1=(Button)findViewById(R.id.bi1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
if(cb1.isChecked()) {
r.append("\n").append(cb1.getText()).append("150Rs");
sum += 150;
}
if(cb2.isChecked()) {
r.append("\n").append(cb2.getText()).append("50Rs");
sum += 50;
}
if(cb3.isChecked()) {
r.append("\n").append(cb3.getText()).append("100Rs");
sum += 100;
}
r.append("\nTotal:").append(sum);
toastTextView.setText(r.toString());
toast.show();
}
});
}
}
<LinearLayout
android:id="@+id/linear"
android:layout_width="409dp"
android:layout_height="729dp"
android:orientation="vertical"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp">
<CheckBox
android:id="@+id/cbi1"
android:layout_width="match_parent"
android:layout_height="77dp"
android:text="Pizza" />
<CheckBox
android:id="@+id/cbi2"
android:layout_width="match_parent"
android:layout_height="82dp"
android:text="Coffee" />
<CheckBox
android:id="@+id/cbi3"
android:layout_width="match_parent"
android:layout_height="64dp"
android:text="Burger" />
<Button
android:id="@+id/bi1"
android:layout_width="match_parent"
android:layout_height="196dp"
android:text="Submit" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/custom_toast_background"
android:orientation="horizontal"
android:padding="8dp"
>
<!-- ImageVView and TextView for custom Toast -->
<ImageView
android:id="@+id/toastImageView"
android:layout_width="110dp"
android:layout_height="83dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/download" />
<TextView
android:id="@+id/toastTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFF" />
</LinearLayout>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</shape>