android - custom AlertDialog view.onClickListener does not work properly -
i'm adding colors.xml
alertdialog
, want respond, when 1 of views clicked. dialog displayed , when performclick()
on 1 of views works. when try run on phone , click hand listener not triggered although should be.
here's code:
my mainactivity.java
alertdialog.builder builder = new alertdialog.builder(this); builder.settitle("pick color"); builder.setcancelable(true); flexboxlayout fl_colors = (flexboxlayout) layoutinflater.inflate(r.layout.colors,null); builder.setview(layoutinflater.inflate(r.layout.colors,null)); final alertdialog alert = builder.create(); for(int i=0;i<fl_colors.getchildcount();i+=1){ view v_color = fl_colors.getchildat(i); log.d("debug",v_color.tostring()); // logs element like: android.view.view{a7d0c87 v.ed..... ......i. 0,0-0,0 #7f0d00a6 app:id/btn_6} v_color.setonclicklistener(new view.onclicklistener(){ @override public void onclick(view v) { string color = (string) v.gettag(); log.d("debug",color); alert.dismiss(); } }); } builder.show();
and colors.xml
<?xml version="1.0" encoding="utf-8"?> <com.google.android.flexbox.flexboxlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" xmlns:app="http://schemas.android.com/apk/res-auto" app:flexwrap="wrap" android:id="@+id/fb_colors"> <view android:layout_width="50dp" android:layout_height="50dp" android:id="@+id/btn_0" android:background="@drawable/circle" android:backgroundtint="#16a085" android:tag="#16a085" android:layout_margin="5dp"/> <view android:layout_width="50dp" android:layout_height="50dp" android:id="@+id/btn_1" android:background="@drawable/circle" android:backgroundtint="#27ae60" android:tag="#27ae60" android:layout_margin="5dp"/> <view android:layout_width="50dp" android:layout_height="50dp" android:id="@+id/btn_2" android:background="@drawable/circle" android:backgroundtint="#2980b9" android:tag="#2980b9" android:layout_margin="5dp"/> <view android:layout_width="50dp" android:layout_height="50dp" android:id="@+id/btn_3" android:background="@drawable/circle" android:backgroundtint="#8e44ad" android:tag="#8e44ad" android:layout_margin="5dp"/> <view android:layout_width="50dp" android:layout_height="50dp" android:id="@+id/btn_4" android:background="@drawable/circle" android:backgroundtint="#f39c12" android:tag="#f39c12" android:layout_margin="5dp"/> <view android:layout_width="50dp" android:layout_height="50dp" android:id="@+id/btn_5" android:background="@drawable/circle" android:backgroundtint="#d35400" android:tag="#d35400" android:layout_margin="5dp"/> <view android:layout_width="50dp" android:layout_height="50dp" android:id="@+id/btn_6" android:background="@drawable/circle" android:backgroundtint="#c0392b" android:tag="#c0392b" android:layout_margin="5dp"/> </com.google.android.flexbox.flexboxlayout>
because not use inflated view. replace
builder.setview(layoutinflater.inflate(r.layout.colors,null));
with
builder.setview(fl_colors);
Comments
Post a Comment