i have created a button and set onClicklistener on it as well but i can’t get it to open a popup. i have coded the xml and java code of it which can be seen below. When i click on it it clicks but doesn’t do anything.
xml code for the popup
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:id = "@+id/popup"
android:orientation = "vertical">
<TextView
android:id = "@+id/popUpTitle"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "@string/popup" />
<TextView
android:id = "@+id/popUpContent"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "@string/popcontent"
android:layout_marginBottom="47dp" />
<Button
android:id="@+id/popUpButtonClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/close" />
java code for button to create a popup when clicked
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId())
{
case R.id.buttonCreate:
Intent i = new Intent(CreateQuestions.this, UploadQuestions.class);
startActivity(i);
break;
case R.id.buttonStartQuiz:
if (p2 != null)
{
showPopup(CreateQuestions.this, p2);
}
}
method to create the popup
//method that displays the pop up
}
private void showPopup(final CreateQuestions createQuestions1, Point p2) {
// TODO Auto-generated method stub
int popupWidth = 200;
int popupHeight = 150;
LinearLayout viewGroup = (LinearLayout) createQuestions1.findViewById (R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) createQuestions1.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup, viewGroup);
//method to create the popup
final PopupWindow popupWindow = new PopupWindow(createQuestions1);
popupWindow.setContentView(layout);
popupWindow.setWidth(popupWidth);
popupWindow.setHeight(popupHeight);
popupWindow.setFocusable(true);
//some offseta to align the pop up relative to the button's positions
int OFFSET_X = 30;
int OFFSET_Y = 30;
//clear the default background
popupWindow.setBackgroundDrawable(new BitmapDrawable());
//displaying the pop up at the specified location and offsets
popupWindow.showAtLocation(layout, Gravity.NO_GRAVITY, p2.x + OFFSET_X, p2.y + OFFSET_Y);
//getting the reference to Close button and close the popup when clicked.
close = (Button) layout.findViewById(R.id.popUpButtonClose);
close.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
// TODO Auto-generated method stub
popupWindow.dismiss();
}
});
}
Any guidance help and tip would be really appreciated.
hummmm. . . .
It looks like you dont have put the clickListener for the button.
Have you put this code on onCreate ??
or
Also check for the value of p2 before below code
It seems like you are not initialize the variable. So please check it.
If not then put it and see. If still not solve your issue then let me know.