In my application, I have a button initially on the screen, and in onclick
of the button, a popup window should open. In the popup window, I have an imagebutton
This will not give error in Inflater and work properly:
LayoutInflater layoutInflater = getLayoutInflater();
View pview = layoutInflater.inflate(R.layout.popup_example, (ViewGroup)findViewById(R.layout.main));
PopupWindow pw = new PopupWindow(pview);
pw.showAtLocation(v, Gravity.LEFT,0,0);
pw.update(8,-70,150,270);
//if onclick written here, it gives null pointer exception.
ImageButton img=(ImageButton)pview.findViewById(R.id.home);
img.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent.....
}
});
best solution :) Pass onCclickMethod from xml
<ImageButton
android:id="@+id/save_btn"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
**android:onClick="onClick"**
android:contentDescription="@string/save"
android:src="@drawable/save" />
it wroked from me..
You have to find the button into the Popup view:
View pview = inflater.inflate(R.layout.popup_example,(ViewGroup)findViewById(R.layout.main));
PopupWindow pw = new PopupWindow(pview);
pw.showAtLocation(v, Gravity.LEFT,0,0);
pw.update(8,-70,150,270);
//if onclick written here, it gives null pointer exception.
ImageButton img=(ImageButton)pview.findViewById(R.id.home);
img.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent.....
}
});
In main activity button onClick you can use:
popupWindow.getContentView().findViewById(R.id.buttonInPopup).setOnClickListener(new OnClickListener(...)