Add Controls To Custom Dialog Programmatically
Solution 1:
The problems in your code were pretty obvious:
In your layout file you use
ViewGroupwhich is an abstract class(the root of all layouts in Android) and which can't be instantiated so it will most likely be the reason for that inflate exception you talk about. Use one of the subclasses ofViewGroup, likeLinearLayout,RelativeLayoutetc, which one fits you better.Even after doing the modification I wrote above your code will still bot work. First the
ViewGroupclass doesn't have anaddmethod, you're probably referring to one of theaddViewmethods. Second thedlgViewwill benullbecause at that moment theDialogisn't displayed so there is noViewto find. You can do it by posting aRunnableon one of your views to delay setting the views until theDialogis shown:final Dialog res = builder.create(); oneOfYourViews.post(new Runnable() { @Override public void run() { ViewGroup dlgView = (ViewGroup) res.findViewById(R.id.dlg_view); MyControl myControl = new MyControl(context); dlgView.addView(myControl); } });
Code addition:
View contentView = inflater.inflate(R.layout.geomap_menu, null)
ViewGroup dlgView = (ViewGroup) contentView.findViewById(R.id.dlg_view);
MyControl myControl = new MyControl(this);
dlgView.addView(myControl); // or add the other views in the loop as many as you want
builder.setView(contentView);
// rest of your code
Post a Comment for "Add Controls To Custom Dialog Programmatically"