Reputation: 23
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..
ActivityMain.xml - the is the ExpandableListView
<TextView
android:id="@+id/Header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorACD_gray"
android:fontFamily="@font/sourcesanspro_bold"
android:padding="10dp"
android:text="@string/acd_keyconfig"
android:textAlignment="viewStart"
android:textColor="@color/white"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="@+id/line"
android:layout_width="wrap_content"
android:layout_height="5dp"
android:layout_below="@id/Header"
android:layout_alignLeft="@+id/Header"
android:layout_alignRight="@+id/Header"
android:background="@color/colorACD_orange"
android:orientation="horizontal" />
<ExpandableListView
android:id="@+id/lvExp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/line"
android:layout_alignParentBottom="false"
android:divider="@color/colorACD_orange"
android:dividerHeight="2dp"
android:groupIndicator="@drawable/settings_selector" />
List_Item.xml - there is the button
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/lblListItem"
android:layout_width="144dp"
android:layout_height="42dp"
android:layout_marginStart="0dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="0dp"
android:layout_marginEnd="0dp"
android:layout_marginBottom="0dp"
android:gravity="center"
android:paddingLeft="25dp"
android:background="@color/colorACD_white"
android:textColor="@color/colorACD_black"
android:textSize="18sp"
android:fontFamily="@font/sourcesansproregular"/>
<Button
android:id="@+id/button1"
android:layout_width="115dp"
android:layout_height="45dp"
android:layout_marginLeft="250dp"
android:fontFamily="@faont/robotoregular"
android:text="@string/edit"
android:onClick="clickFunc"/>
<LinearLayout
android:id="@+id/line"
android:layout_width="391dp"
android:layout_height="10dp"
android:orientation="horizontal" />
- 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
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