I am new to android development. I have created a popup window which is being called on a button click from the main activity class. I have some Edit text field on my pop window. can i clear all these edit text field on the pop up window through a button click? if yes then how? Thanks in advance!!
Below is what i am trying but it is giving null point exception.
public void initatePopupWindow(View view)
{
try
{
LayoutInflater inflater=(LayoutInflater) ContactdetailsActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout=inflater.inflate(R.layout.popupwindow,(ViewGroup)findViewById(R.id.popuplayout));
pw = new PopupWindow(layout, 500, 500, true);
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
Button b1=(Button)layout.findViewById(R.id.cancel);
b1.setOnClickListener(cancel_button_click_listener);
Button b2=(Button)layout.findViewById(R.id.clear);
b2.setOnClickListener(clear_fields);
}
catch (Exception e)
{
}
}
private OnClickListener clear_fields= new OnClickListener()
{
public void onClick(View v)
{
ViewGroup group= (ViewGroup)findViewById(R.id.popuplayout);
for(int i=0,count=getChildCount(); i<count; ++i)
{
View v1= group.getChildAt(i);
if(v1 instanceof EditText)
{
((EditText)v1).setText("");
}
if(v1 instanceof ViewGroup && (((ViewGroup)v1).getChildCount() > 0))
onClick((ViewGroup)v1);
}
}
};
private OnClickListener cancel_button_click_listener=new OnClickListener()
{
public void onClick(View v)
{
pw.dismiss();
}
};
}
Are you getting your errors on this line?
I think it might be beacuse you need to use the value you called on the layout inflator.
Anyway, I guess you should do instead: Get a reference of all your edit texts and then clear them one by one. Maybe keep an array of edit texts and walk through them and set their texts to “” instead of checking for the type.