XML File: - Java File

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

XML FILE: - JAVA FILE: -

<?xml version="1.0" encoding="utf-8"?> package com.example.myapplication;// MainActivity.java


<RelativeLayout import androidx.appcompat.app.AppCompatActivity;
xmlns:android="http://schemas. import android.os.Bundle;
android.com/apk/res/android" import android.view.View;
xmlns:tools="http://schemas. import android.widget.Button;
android.com/tools" public class MainActivity extends AppCompatActivity {
android:layout_width="match_parent" @Override
android:layout_height="match_parent" protected void onCreate(Bundle savedInstanceState) {
android:padding="16dp" super.onCreate(savedInstanceState);
tools:context=".MainActivity"> setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
<Button button.setOnClickListener(new View.OnClickListener() {
android:id="@+id/button" @Override
android:layout_width="wrap_content" public void onClick(View v) {
android:layout_height="wrap_content" // Call the method to show custom toast
android:text="Show Custom Toast" showCustomToast();
android:layout_centerInParent="true"/> }
});
</RelativeLayout> }
private void showCustomToast() {
Custom_Toast_layout.xml CustomToast.showToast(getApplicationContext(), "Custom
<?xml version="1.0" encoding="utf-8"?> toast message");
<LinearLayout }
xmlns:android="http://schemas. }
android.com/apk/res/android" CustomToast.java
android:id="@+id/customToastLayout" package com.example.myapplication;// CustomToast.java
android:layout_width="wrap_content" import android.content.Context;
android:layout_height="wrap_content" import android.view.LayoutInflater;
android:background="@drawable import android.view.View;
/ic_launcher_background" import android.widget.ImageView;
android:orientation="horizontal" import android.widget.TextView;
android:padding="16dp"> import android.widget.Toast;
public class CustomToast {
<ImageView public static void showToast(Context context, String message) {
android:id="@+id/imageView" LayoutInflater inflater = (LayoutInflater)
android:layout_width="wrap_content" context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
android:layout_height="wrap_content" View layout = inflater.inflate(R.layout.custom_toast_layout,
android:src="@drawable null); ImageView imageView =
/ic_launcher_foreground" /> layout.findViewById(R.id.imageView);
TextView textView = layout.findViewById(R.id.textView);
<TextView imageView.setImageResource(R.drawable.ic_launcher_foreground);
android:id="@+id/textView" textView.setText(message);
android:layout_width="wrap_content" Toast toast = new Toast(context);
android:layout_height="wrap_content" toast.setDuration(Toast.LENGTH_LONG);
android:textColor="#FFFFFF" toast.setView(layout);
android:text="Custom Toast" /> toast.show();
}
</LinearLayout> }
XML FILE: - Custom_Toast_Layout.xml
<RelativeLayout xmlns:android="http://schemas. <?xml version="1.0" encoding="utf-8"?>
android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.
xmlns:tools="http://schemas. android.com/apk/res/android"
android.com/tools" android:id="@+id/customToastLayout"
android:layout_width="match_parent" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="wrap_content"
android:padding="16dp" android:background="#ADA0A0"
tools:context=".MainActivity"> android:orientation="horizontal"
<CheckBox android:padding="16dp">
android:id="@+id/checkBox1" <TextView
android:layout_width="wrap_content" android:id="@+id/textView"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:text="Pizza" android:layout_height="wrap_content"
android:layout_marginTop="20dp"/> android:textColor="#FF0000"
<CheckBox android:text="Custom Toast" />
android:id="@+id/checkBox2"
android:layout_width="wrap_content" </LinearLayout>
android:layout_height="wrap_content"
android:text="Burger"
android:layout_below="@id/checkBox1"
android:layout_marginTop="20dp"/>
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Coffe"
android:layout_below="@id/checkBox2"
android:layout_marginTop="20dp"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Order"
android:layout_below="@id/checkBox3"
android:layout_marginTop="20dp"/>
</RelativeLayout>
MainActivity.java CustomToast.java
package com.example.myapplication; package com.example.myapplication;
import import android.content.Context;
androidx.appcompat.app.AppCompatActivity; import android.view.LayoutInflater;
import android.os.Bundle; import android.view.View;
import android.view.View; import android.widget.TextView;
import android.widget.Button; import android.widget.Toast;
import android.widget.CheckBox; public class CustomToast {
public class MainActivity extends public static void showToast(Context context, String
AppCompatActivity { message) {
LayoutInflater inflater = (LayoutInflater)
CheckBox checkBox1, checkBox2, checkBox3; context.getSystemService(Context.LAYOUT_INFLATER_SERVICE
);
@Override View layout =
protected void onCreate(Bundle inflater.inflate(R.layout.custom_toast_layout, null);
savedInstanceState) { TextView textView = layout.findViewById(R.id.textView);
super.onCreate(savedInstanceState); textView.setText(message);
setContentView(R.layout.activity_main); Toast toast = new Toast(context);
toast.setDuration(Toast.LENGTH_LONG);
checkBox1 = findViewById(R.id.checkBox1); toast.setView(layout);
checkBox2 = findViewById(R.id.checkBox2); toast.show();
checkBox3 = findViewById(R.id.checkBox3); }
}
Button button = findViewById(R.id.button);
button.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View v) {
showCustomToast();
}
});
}
private void showCustomToast() {
StringBuilder message = new
StringBuilder("Selected Checkboxes:\n");
if (checkBox1.isChecked()) {
message.append("- Pizza\n");
}
if (checkBox2.isChecked()) {
message.append("- Burger\n");
}
if (checkBox3.isChecked()) {
message.append("- Coffee\n");
}

CustomToast.showToast(getApplicationContext(
), message.toString());
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy