skokan
skokan

Reputation: 23

Java onclick event open new popup

I have a MainActivity.xml, Where I have ExpandableListView, In The ExpandableListView I have List of groups, every group have some items, behind the items I have button, I need to create onclick event on the button, I need to click on the button, and then open a small popup window, I have created a xml file with the popup, how to create the onclick event, im trying it for 1 week, but nothing works. where I must create the onclick..

 - ExpandableListAdapter.java
``` package com.example.test;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context _context;
    private List<String> _listDataHeader; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> _listDataChild;

    public ExpandableListAdapter(Context context, List<String> listDataHeader,
                                 HashMap<String, List<String>> listChildData) {
        this._context = context;
        this._listDataHeader = listDataHeader;
        this._listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);


        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }

        TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null);
        lblListHeader.setText(headerTitle);

        return convertView;
    }



    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


}
 ```
  

Upvotes: 0

Views: 919

Answers (1)

SlothCoding
SlothCoding

Reputation: 1706

You need to create onClick event inside getChildView() method. Just like you use setText() for your child TextView you can use that for the button too. Like this:

@Override
public View getChildView(int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    final String childText = (String) getChild(groupPosition, childPosition);


    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_item, null);
    }

    TextView txtListChild = (TextView) convertView.findViewById(R.id.lblListItem);
    txtListChild.setText(childText);

    Button button = convertView.findViewById(R.id.button1);
    button.setOnClickListener(); <-------------- HERE
    return convertView;
}

But there is option for ExpandableListView to use listener for OnChildClickListener. So, basically, you handle child click events there. You can read more about that here:

You can also find many examples here on SO or just google it.


EDIT: How to create a custom popup dialog

To call the custom popup you want to create you first need to create a custom dialog class like this:

public class PopupDialog extends Dialog {

    private Context context;

    public PopupDialog(@NonNull Context context) {
        super(context, R.style.NoActionBarDialog);
        this.context = context;
    }

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

        TextView text = findViewById(R.id.popup_text);
        //find your views like this and use them as you want

    }
    
    @Override
    public void onBackPressed() {
        //do nothing <----- only this if you want to forbid the user to exit the dialog with the back button, else don't override this method
    }
}

Now when you have this custom popup dialog you can call it like this inside your onClick method:

Button button = convertView.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                PopupDialog popup_dialog = new PopupDialog (_context);
                popup_dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                popup_dialog.setContentView(R.layout.popup_dialog_layout);
                Window help_window = popup_dialog.getWindow();
                help_window.setLayout(AbsListView.LayoutParams.MATCH_PARENT, AbsListView.LayoutParams.WRAP_CONTENT);
                popup_dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                popup_dialog.setCanceledOnTouchOutside(false);
                popup_dialog.show();
            }
        });

And add this to your theme.xml or styles.xml

<!-- Dialog Theme -->
<style name="NoActionBarDialog" parent="Theme.MaterialComponents.Light.Dialog.Bridge">
    <item name="windowActionBar">false</item>
    <item name="android:windowMinWidthMajor">97%</item>
    <item name="android:windowMinWidthMinor">97%</item>
    <item name="windowNoTitle">true</item>
    <item name="android:colorBackground">@android:color/transparent</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowActionBarOverlay">false</item>
    <item name="android:windowCloseOnTouchOutside">false</item>
</style>

Upvotes: 1

Related Questions