Exchange

Show Popup Dialog in Android Studio

 

Create Popup Layout xml File

in shipping_address_popup.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="300dp"
android:layout_height="500dp"
android:layout_gravity="center"
android:background="@drawable/circle_background"
android:elevation="5dp"
android:padding="5dp">

<ImageView
android:id="@+id/imageCloseAp"
android:layout_width="40dp"
android:layout_height="40dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_close" />

<EditText
android:id="@+id/et_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageCloseAp" />

<EditText
android:id="@+id/et_address"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Address"
android:gravity="start|top"
android:inputType="textMultiLine"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_name" />

<EditText
android:id="@+id/et_district"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="textPersonName"
android:hint="District"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_address" />

<EditText
android:id="@+id/et_contactnumber"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="Contact Mobile"
android:inputType="phone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_district" />

<Button
android:id="@+id/btn_save_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:text="Save"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_contactnumber" />

</androidx.constraintlayout.widget.ConstraintLayout>


Create a Parent Activity

in activity_profile.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=".ProfileActivity">

<Button
android:id="@+id/btn_add_shipping_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Shipping Address"

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

in ProfileActivity.java


public class ProfileActivity extends AppCompatActivity {

Dialog myAddress;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);

//String uid = FirebaseAuth.getInstance().getUid();

myAddress = new Dialog(this);

findViewById(R.id.btn_add_shipping_address).setOnClickListener(view -> {
showAddressPopup();
});

}

private void showAddressPopup() {
ImageView imageView;

myAddress.setContentView(R.layout.shipping_address_popup);
imageView = (ImageView) myAddress.findViewById(R.id.imageCloseAp);

imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myAddress.dismiss();
}
});

myAddress.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
myAddress.show();
myAddress.setCanceledOnTouchOutside(false);
}


}



Post a Comment

0 Comments