0% found this document useful (0 votes)
9 views4 pages

Customized Dialogbox

The document contains XML layout files for a dialog box and a main activity in an Android application. The dialog box includes a title, an image, and two buttons (Yes and No) with corresponding actions, while the main activity has a button to show the dialog. The Java code in MainActivity handles the dialog display and button click events, showing Toast messages for user interactions.

Uploaded by

yayan zoft
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Customized Dialogbox

The document contains XML layout files for a dialog box and a main activity in an Android application. The dialog box includes a title, an image, and two buttons (Yes and No) with corresponding actions, while the main activity has a button to show the dialog. The Java code in MainActivity handles the dialog display and button click events, showing Toast messages for user interactions.

Uploaded by

yayan zoft
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

============================== dialog_box.

xml
============================================================

<?xml version="1.0" encoding="utf-8"?>


<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_gravity="center"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="30dp"

>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#160090">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SELECT"
android:gravity="center_horizontal"
android:textSize="20sp"
android:padding="10dp"
android:textColor="@color/white"
android:background="#B300FF"/>

<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@android:drawable/btn_star_big_on"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2"
android:layout_marginTop="50dp">

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/noBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="No"
android:textColor="@color/white"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="20dp"
android:background="#FF0000"/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/yesBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Yes"
android:textColor="@color/white"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:background="#359900"/>

</LinearLayout>

</LinearLayout>

</androidx.cardview.widget.CardView>

===================================================================================
===========================================
============================== 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<androidx.appcompat.widget.AppCompatButton
android:id="@+id/showDialogBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Dialog"
android:textAllCaps="false"
android:textColor="@color/white"
android:background="#009030"
android:padding="15dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
===================================================================================
===========================================
============================== MainActivity.java ==============================

package com.signup.customizeddialog;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.Objects;

public class MainActivity extends AppCompatActivity {


AppCompatButton showDialogBtn;
Dialog dialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v,
insets) -> {
Insets systemBars =
insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right,
systemBars.bottom);
return insets;
});

dialog = new Dialog(this);


showDialogBtn = findViewById(R.id.showDialogBtn);

showDialogBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showDialog();
}
});

private void showDialog() {


dialog.setContentView(R.layout.dialog_box);
dialog.setCanceledOnTouchOutside(false);
Objects.requireNonNull(dialog.getWindow()).setBackgroundDrawable(new
ColorDrawable(Color.TRANSPARENT));
dialog.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT);
dialog.show();

AppCompatButton yesbtn = dialog.findViewById(R.id.yesBtn);


AppCompatButton nobtn = dialog.findViewById(R.id.noBtn);

yesbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "Yes Button Pressed..",
Toast.LENGTH_SHORT).show();
// dialog.dismiss();
}
});

nobtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "No Button Pressed..",
Toast.LENGTH_SHORT).show();
// dialog.dismiss();

}
});

}
}

You might also like