Create pop ups for each GridView project – Android

I'm developing an Android application for the hotel's food ordering system. In my project, I have a GridView. When I click the button on the project in GridView, I want a pop-up window containing some text fields, buttons, etc

I got help from this document example

The above document example can work as a single application. This is only an example of creating a basic pop-up window. But I need to use it on GridView

My job - >

Instead of the main.xml and popupwindowdemoactivity.java of the document (which is the main activity), I use grid_ Single.xml and its implementation file grid_ single.java.

But it doesn't work for me. When I click the button of each project, the pop-up window doesn't display as I want. Therefore, please help me solve this problem, or give me a working example of the pop-up window of grid view project. I have attached my file. Waiting for your help, thank you!

Grid_ single.java

    Button btnClosePopup;
    Button btnCreatePopup;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_single);
        btnCreatePopup = (Button) findViewById(R.id.addToCart);
        btnCreatePopup.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                initiatePopupWindow();
            }
        });


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

    }

    private PopupWindow pwindo;
    private void initiatePopupWindow() {
        try {
            // We need to get the instance of the LayoutInflater
            LayoutInflater inflater = (LayoutInflater) Grid_Single_Popup.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.screen_popup, (ViewGroup) findViewById(R.id.popup_element));
            pwindo = new PopupWindow(layout, 300, 370, true);
            pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);

            btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
            btnClosePopup.setOnClickListener(cancel_button_click_listener);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private OnClickListener cancel_button_click_listener = new OnClickListener() {
        public void onClick(View v) {
            pwindo.dismiss();

        }
    };

grid_ single.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:layout_marginTop="15dp"
              android:padding="5dp"
              android:id="@+id/grid_single_back"
        >
    <ImageView
            android:id="@+id/grid_image"
            android:layout_width="150dp"
            android:layout_height="150dp">
    </ImageView>

    <TextView
            android:id="@+id/grid_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            android:textSize="24dp" >
    </TextView>
    <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="35dp"
            android:text="Cutomize &amp; Add"
            android:id="@+id/addToCart"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="5dp"
            android:layout_marginRight="5dp"
            android:textSize="18dp"
            android:padding="5sp"/>

</LinearLayout>

Thank you in advance

resolvent:

This is useful to me. Please check it once

In the grid adapter:

public CustomGrid(String[] web, int[] Imageid, Grid_single activity) {
    this.Imageid = Imageid;
    this.web = web;
    this.activity = activity;
}

In the getview method of the adapter, keep the following:

grid = inflater.inflate(R.layout.grid_single, null);
TextView textView = (TextView) grid.findViewById(R.id.grid_text);
ImageView imageView = (ImageView) grid.findViewById(R.id.grid_image);
Button btnPopup = (Button) grid.findViewById(R.id.btnPopup);
btnPopup.setOnClickListener(activity);

Grid_ Single should implement onclicklistener and keep it as follows:

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btnPopup:
        initiatePopupWindow(web[grid.getPositionForView(v)]);
        break;
    default:
        break;
    }
}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>