1

I'm trying to get a pop-up window (modal window) open when I click a button. I have no idea how it works in Java, I've seen a lot of tutorials and can't figure it out. Here is my button.

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    android:background="@color/white"
>

    <LinearLayout
        android:layout_margin="50dp"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <LinearLayout
        android:layout_gravity="center"
        android:orientation="vertical"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"/>
    <Button
        //This is the button that should open a modal window
        android:id="@+id/btnfruit"
        android:background="@mipmap/iconfruit"
        android:layout_width="100dp"
        android:layout_height="100dp" />
    <Button
        android:layout_marginTop="100dp"
        android:background="@mipmap/vegetable"
        android:layout_width="100dp"
        android:layout_height="100dp" />
    <Button
        android:layout_marginTop="200dp"
        android:background="@mipmap/protein"
        android:layout_width="100dp"
        android:layout_height="100dp" />

    <Button
        android:layout_marginTop="300dp"
        android:background="@mipmap/dairy"
        android:layout_width="100dp"
        android:layout_height="100dp" />
    <Button
        android:layout_marginTop="400dp"
        android:background="@mipmap/grain"
        android:layout_width="100dp"
        android:layout_height="100dp" />
    <ImageView
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"/>
</RelativeLayout>

this is the modal window.

        <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:layout_gravity="center"
        android:orientation="vertical"
        android:padding="5dp"
     >
        <TextView

            android:layout_width="30dp"
            android:layout_height="30dp"
            android:text="X"
            android:textStyle="bold"
            android:layout_gravity="end"
            android:gravity="center"/>
        <Button
            android:id="@+id/btnaddfruit"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:text="Add Item"/>
    </LinearLayout>

I guess my question is, what goes on MainActivity.java or can I code it straight to the button? Thank you in advance for your time and answers.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

In whatever class your Button object is in, I would assume MainActivity, create a PopupWindow object (you will have to import the class. Inside of the onClick() method of your button, initialize it and set the location

PopupWindow myWindow = new PopupWindow(View content, int height, int width, true);
myWindow.showAtLocation(View content, Gravity.CENTER, 0, 0);

Obviously you need to fill in the unfilled parameters with your own values. In order to get the View object needed, you will have to create a new View for the pop up window using a LayoutInflater.