Android - Popup window not showing in fragment - android

I'm creating a popupWindow but it is not showing on my Fragment when I call it.
Here is my code for the popupWindow:
LayoutInflater layoutInflater =
(LayoutInflater)getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
btnDismiss.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
popupWindow.dismiss();
}});
popupWindow.showAsDropDown(getView());
//popupWindow.showAsDropDown(btnOpenPopup, 50, -30);
//popupWindow.showAsDropDown(getCurrentFocus());
popupView.setOnTouchListener(new OnTouchListener() {
int orgX, orgY;
int offsetX, offsetY;
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
orgX = (int) event.getX();
orgY = (int) event.getY();
break;
case MotionEvent.ACTION_MOVE:
offsetX = (int)event.getRawX() - orgX;
offsetY = (int)event.getRawY() - orgY;
popupWindow.update(offsetX, offsetY, -1, -1, true);
break;
}
return true;
}});

The following code may be work your specification. Call this method from inside onClick(View v) of OnClickListener assigned to the View:
public void showPopup(View anchorView) {
View popupView = getLayoutInflater().inflate(R.layout.popup_layout, null);
PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// Example: If you have a TextView inside `popup_layout.xml`
TextView tv = (TextView) popupView.findViewById(R.id.tv);
tv.setText(....);
// Initialize more widgets from `popup_layout.xml`
....
....
// If the PopupWindow should be focusable
popupWindow.setFocusable(true);
// If you need the PopupWindow to dismiss when when touched outside
popupWindow.setBackgroundDrawable(new ColorDrawable());
int location[] = new int[2];
// Get the View's(the one that was clicked in the Fragment) location
anchorView.getLocationOnScreen(location);
// Using location, the PopupWindow will be displayed right under anchorView
popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY,
location[0], location[1] + anchorView.getHeight());
}
here anchorView is the v from onClick(View v).

Use following code to show Pop Up window.It will work for you.
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
popupWindow.showAtLocation(popupView , Gravity.CENTER, 0, 0);

Here is the sample code to show and hide a popup window.
TextView popupWindowTextView = new TextView(getActivity());
Button popupWindowButton = new Button(getActivity());
LinearLayout layout = new LinearLayout(getActivity());
popupWindowButton.setText("OK");
popupWindowTextView.setText("Popup Window. Click on OK to dismiss.");
popupWindowTextView.setPadding(0, 0, 0, 20);
layout.setOrientation(1);
layout.addView(popupWindowTextView);
layout.addView(popupWindowButton);
int popupWindowWidth = 200;
int popupWindowHeight = 150;
final PopupWindow popupWindow = new PopupWindow(context);
popupWindow.setContentView(layout);
popupWindow.setWidth(popupWidth);
popupWindow.setHeight(popupHeight);
popupWindow.setFocusable(true);
popupWindow.showAtLocation(layout, Gravity.NO_GRAVITY, popupWindowWidth, popupWindowHeight);
popupWindowButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
popupWindow.dismiss();
}
});

final PopupWindow popupWindow = new PopupWindow(
popupView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Check the LayoutParams you are using is matching with the parent of the view.
e.g. use if the parent is a LinearLayout, use LinearLayout.LayoutParams.WRAP_CONTENT

The root of R.layout.popup should have
android:layout_width="wrap_content"
android:layout_height="wrap_content"

Just use the update like below immediately after the showAsDropDown :
pop.showAsDropDown(anchor);
pop.update(0,0, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

Related

Aligning popup window near the button clicked

I have a custom popup window by a layout. I have to give a x,y coordinates to appear popup window after a_btn click. This can be different locations in different phones.
But I want to show the popup window always above and touching the the a_btn
How can I implement this.Help me
My code for showing the popup window :
a_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater lInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popup_view = lInflater.inflate(R.layout.popup_a, null);
final PopupWindow popup = new PopupWindow(popup_view,FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT,true);
popup.setFocusable(true);
popup.setBackgroundDrawable(new ColorDrawable());
popup.showAtLocation(relative, Gravity.NO_GRAVITY, coordinateTop, 100);
//popup.showAsDropDown(location_popup_view, 2, 2);
}
});
Use this code will help you
a_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Rect r = locateView(v);
LayoutInflater lInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popup_view = lInflater.inflate(R.layout.popup_a, null);
final PopupWindow popup = new PopupWindow(popup_view,FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT,true);
popup.setFocusable(true);
popup.setBackgroundDrawable(new ColorDrawable());
popup.showAtLocation(layout, Gravity.TOP | Gravity.LEFT, r.right, r.bottom);
}
});
public static Rect locateView(View v) {
int[] loc_int = new int[2];
if (v == null)
return null;
try {
v.getLocationOnScreen(loc_int);
} catch (NullPointerException npe) {
return null;
}
Rect location = new Rect();
location.left = loc_int[0];
location.top = loc_int[1];
location.right = loc_int[0] + v.getWidth();
location.bottom = loc_int[1] + v.getHeight();
return location;
}
I used popup.showAsDropDown(a_btn,0,0);
instead of
popup.showAtLocation(relative, Gravity.NO_GRAVITY, coordinateTop, 100);
and gave xoff and yoff.
a_btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater lInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popup_view = lInflater.inflate(R.layout.popup_a, null);
final PopupWindow popup = new PopupWindow(popup_view,200,200,true);
popup.setFocusable(true);
popup.setBackgroundDrawable(new ColorDrawable());
popup.showAsDropDown(a_btn,0,0);
}
});

Android Programmatic Popup ScrollView TextView

Whats wrong with this code?
It only shows the grey popup window, no ScrollView or TextView appear.
However, if I add the button denoted by the false branch, the button appears.
Here I am adding more text, because SO won't let me post without more text.
Thanks!
static PopupWindow DebugScreen( Activity a )
{
final PopupWindow popUp = new PopupWindow( a );
final View cv = a.getWindow().getDecorView().findViewById( android.R.id.content );
FrameLayout.LayoutParams p = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, Gravity.TOP );
FrameLayout fl = new FrameLayout( a );
fl.setLayoutParams(p);
final ScrollView sv = new ScrollView( a );
final TextView tv = new TextView( a );
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) (cv.getHeight() * 0.9), Gravity.TOP);
tv.setText("Hello World");
tv.setMovementMethod(LinkMovementMethod.getInstance());
tv.setLayoutParams( lp );
sv.addView(tv, lp);
if ( false ) {
final Button b;
b = new Button(a);
b.setText("abc123");
b.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
fl.addView(b);
}
popUp.setContentView(fl);
popUp.showAtLocation(cv, Gravity.BOTTOM, 0, 0);
popUp.setFocusable(false);
popUp.setOutsideTouchable(true);
popUp.setTouchable(true);
popUp.setTouchInterceptor(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if ( event.getAction() == MotionEvent.ACTION_OUTSIDE) {
popUp.dismiss();
return true;
}
return false;
}
});
popUp.update( 0, 0, (int) (cv.getWidth() * 0.9), cv.getHeight() );
return( popUp );
}
You missed fl.addView(sv); to add ScrollView to FrameLayout. Add it before popUp.setContentView(fl); to see the TextView.

How to center PopupWindow?

I created an Activity in which i add a button that throws a popup when is clicked. Here is the code of showPopup() method:
private void showPopup() {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup_layout, (ViewGroup) findViewById(R.id.popup_element), false);
final PopupWindow pwindo = new PopupWindow(layout, LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, true);
Button btnAgree = (Button) layout.findViewById(R.id.ok);
btnAgree.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pwindo.dismiss();
}
});
pwindo.showAtLocation(layout, Gravity.CENTER, 0, 0);
}
}
I would center it both vertically and orizzontally. I tried several ways that i see here on SO but none worked. Why i always get the popup window at the top of the screen?
you can use setHorizontalOffset:
ListPopupWindow popupWindow = new ListPopupWindow(this);
// Position
popupWindow.setAnchorView(btn);
popupWindow.setPromptPosition (ListPopupWindow.POSITION_PROMPT_ABOVE);
popupWindow.setHorizontalOffset((btn.layoutParams.width - popupWindow.getWidth())/2);
this code will show perfect popup in center of you specific view
// click event of specific view
#Override
public void onClick(View v) {
super.onClick(v);
if (v == lin_filterActionbar) {
PopupWindow popupwindow_obj; // create object
popupwindow_obj = dialog_AllergiesSelect(RR_HomeActivity.this,lin_filterActionbar);
popupwindow_obj.showAsDropDown(lin_filterActionbar);
}
}
private PopupWindow dialog_AllergiesSelect(Context context,LinearLayout lin) {
final PopupWindow dialog_AllergiesSelect = new PopupWindow(this);
View v = View.inflate(context, R.layout.dialog_filter_actionbar, null);
dialog_AllergiesSelect.setContentView(v);
dialog_AllergiesSelect.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
dialog_AllergiesSelect.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
dialog_AllergiesSelect.setFocusable(true);
// TextView lis_testSelection = (TextView) v.findViewById(R.id.dialogfilter_txt_filter);
// LinearLayout.LayoutParams lin_laLayoutParams = new LinearLayout.LayoutParams((int) (sunApplication.getDeviceWidth() - (sunApplication.getDeviceScale() * dialog_width_dvide_allerges_spinner_width)), (int) (sunApplication.getDeviceHeight() - (sunApplication.getDeviceScale() * dialog_width_dvide_allerges_height)));
// lis_testSelection.setLayoutParams(lin_laLayoutParams);
// Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
int OFFSET_X = 0;
int OFFSET_Y = 0;
// Clear the default translucent background
dialog_AllergiesSelect.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
//dialog_AllergiesSelect.showAtLocation(v, Gravity.C, p.x + OFFSET_X, p.y + OFFSET_Y);
Rect location = locateView(lin);
dialog_AllergiesSelect.showAtLocation(v, Gravity.TOP|Gravity.CENTER, 0, location.bottom);
// Getting a reference to Close button, and close the popup when clicked.
return dialog_AllergiesSelect;
}
public static Rect locateView(View v)
{
int[] loc_int = new int[2];
if (v == null) return null;
try
{
v.getLocationOnScreen(loc_int);
} catch (NullPointerException npe)
{
//Happens when the view doesn't exist on screen anymore.
return null;
}
Rect location = new Rect();
location.left = loc_int[0];
location.top = loc_int[1];
location.right = location.left + v.getWidth();
location.bottom = location.top + v.getHeight();
return location;
}

Add Views programmatically to LinearLayout after it got inflated

I want to add some LinearLayouts to an existing Linearlayout.
The xml of the Activity looks as followed:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popupLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPopUp"
android:gravity="center"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="none" >
<LinearLayout
android:id="#+id/ll_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
The code of the Activity:
public class MainActivity extends Activity {
private Point p;
private PopupWindow popup;
private LinearLayout myLInearLayout;
private TextView valueTV;
private Button valueB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button popUpButton = (Button) findViewById(R.id.open);
popUpButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (p != null)
showPopup(MainActivity.this, p);
}
});
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
int[] location = new int[2];
Button button = (Button) findViewById(R.id.open);
button.getLocationOnScreen(location);
// Initialize the Point with x, and y positions
p = new Point();
p.x = location[0];
p.y = location[1];
}
// The method that displays the popup.
private void showPopup(final Activity context, Point p) {
Rect rectgle = new Rect();
Window window = getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int popupWidth = this.getResources().getDisplayMetrics().widthPixels;
int popupHeight = this.getResources().getDisplayMetrics().heightPixels / 4;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context
.findViewById(R.id.popupLinearLayout);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);
// Creating the PopupWindow
popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setOutsideTouchable(true);
popup.setFocusable(true);
popup.setAnimationStyle(R.style.PopupWindowAnimation);
// Some offset to align the popup a bit to the right, and a bit down,
// relative to button's position.
int OFFSET_X = 0;
int OFFSET_Y = 0;
// Clear the default translucent background
popup.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y
+ OFFSET_Y);
// add LInearLayout
myLInearLayout = (LinearLayout) findViewById(R.id.ll_horizontal);
// add LayoutParams
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.MATCH_PARENT);
myLInearLayout.setOrientation(LinearLayout.HORIZONTAL);
// add textView
valueTV = new TextView(this);
valueTV.setText("The developer world is yours");
valueTV.setId(5);
valueTV.setLayoutParams(params);
// add Button
valueB = new Button(this);
valueB.setText("thedeveloperworldisyours");
valueB.setId(5);
// add the textView and the Button to LinearLayout
myLInearLayout.addView(valueTV);
myLInearLayout.addView(valueB);
}
#Override
public void onBackPressed() {
if (popup != null && popup.isShowing()) {
popup.dismiss();
popup = null;
} else {
super.onBackPressed();
}
}
As you can see I want to add a Button and a TextView after I inflate a View and show it with an Animation in a PopupWindow. I would like to add some Views to the LinearLayout inside the ScrollView. This is for testing purposes, later I want to Add full LinearLayouts to the LinearLayout inside the ScrollView.
The Animation works perfectly. I just can't add some Views programmatically. Everything I get is a NullPointerException in the Line where I try to add the views.
I appreciate your help.
Try this way,hope this will help you to solve your problem.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button popUpButton = (Button) findViewById(R.id.open);
int[] location = new int[2];
popUpButton.getLocationOnScreen(location);
// Initialize the Point with x, and y positions
p = new Point();
p.x = location[0];
p.y = location[1];
popUpButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (p != null)
showPopup(p);
}
});
}
// The method that displays the popup_layout.
private void showPopup(Point p) {
Rect rectgle = new Rect();
Window window = getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int popupWidth = this.getResources().getDisplayMetrics().widthPixels;
int popupHeight = this.getResources().getDisplayMetrics().heightPixels / 4;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) findViewById(R.id.popupLinearLayout);
View layout = LayoutInflater.from(this).inflate(R.layout.popup_layout, viewGroup);
// Creating the PopupWindow
popup = new PopupWindow(this);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setOutsideTouchable(true);
popup.setFocusable(true);
// popup.setAnimationStyle(R.style.PopupWindowAnimation);
// Some offset to align the popup_layout a bit to the right, and a bit down,
// relative to button's position.
int OFFSET_X = 0;
int OFFSET_Y = 0;
// Clear the default translucent background
popup.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup_layout at the specified location, + offsets.
popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
// add LInearLayout
myLInearLayout = (LinearLayout) layout.findViewById(R.id.ll_horizontal);
// add LayoutParams
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT);
myLInearLayout.setOrientation(LinearLayout.HORIZONTAL);
// add textView
valueTV = new TextView(this);
valueTV.setText("The developer world is yours");
valueTV.setId(5);
valueTV.setLayoutParams(params);
// add Button
valueB = new Button(this);
valueB.setText("thedeveloperworldisyours");
valueB.setId(5);
// add the textView and the Button to LinearLayout
myLInearLayout.addView(valueTV);
myLInearLayout.addView(valueB);
}
I figured out how to do it. I just had to inflate the popup.xml to be able to get a reference to the LinearLayout I wanted to customize.
Here is the code if someone is facing the same problem:
public class MainActivity extends Activity {
private Point p;
private PopupWindow popup;
private Button popUpButton;
private TextView valueTV;
private Button valueB;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
popUpButton = (Button) findViewById(R.id.open);
popUpButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (p != null)
showPopup(MainActivity.this, p);
}
});
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
int[] location = new int[2];
Button button = (Button) findViewById(R.id.open);
button.getLocationOnScreen(location);
// Initialize the Point with x, and y positions
p = new Point();
p.x = location[0];
p.y = location[1];
}
// The method that displays the popup.
private void showPopup(final Activity context, Point p) {
Rect rectgle = new Rect();
Window window = getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int popupWidth = this.getResources().getDisplayMetrics().widthPixels;
int popupHeight = this.getResources().getDisplayMetrics().heightPixels / 4;
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View contentView = inflater.inflate(R.layout.popup_layout, null);
ViewGroup dlgView = (ViewGroup) contentView
.findViewById(R.id.ll_horizontal);
// Creating the PopupWindow
popup = new PopupWindow(context);
popup.setContentView(contentView);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
popup.setAnimationStyle(R.style.PopupWindowAnimation);
// Some offset to align the popup a bit to the right, and a bit down,
// relative to button's position.
int OFFSET_X = 0;
int OFFSET_Y = 0;
// Clear the default translucent background
popup.setBackgroundDrawable(new BitmapDrawable());
// add textView
valueTV = new TextView(this);
valueTV.setText("The developer world is yours");
valueTV.setId(5);
// add Button
valueB = new Button(this);
valueB.setText("thedeveloperworldisyours");
valueB.setId(5);
// add the textView and the Button to LinearLayout
dlgView.addView(valueTV);
dlgView.addView(valueB);
// Displaying the popup at the specified location, + offsets.
popup.showAtLocation(popUpButton, Gravity.NO_GRAVITY, p.x + OFFSET_X,
p.y + OFFSET_Y);
}
}

popup dismiss android

I am implementing a popup in android. I can dismiss it when I click outside of the popup, but I want to dismiss it when I click on same imageview/button as well.
1.on click on button1 it opens pop up
2.If I click outside it closes pop up
3.If I click on button1(when pop up is open), it closes pop and reopens it again
what I want is
If I click on button1(when pop up is open), it just closes pop and donot reopen unless second click.
Is this possible?
NOTE: I donot want to have a close button inside popup window.
pop_one.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
pop_one.getLocationOnScreen(location);
p = new Point();
p.x = location[0];
p.y = location[1];
popupshow(one_one_text,pop_one);
}
});
And the method is:
public void popupshow(String pop_text, ImageView pop_one) {
int width = display.getWidth(); // deprecated
System.out.println("jsfjsfjnsdf"+width);
int new_width = width-(width/6) ;
LayoutInflater layoutInflater = (LayoutInflater) getActivity()
.getBaseContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(
R.layout.career_options_popup_short, null);
final PopupWindow popupWindow = new PopupWindow(popupView, new_width,
LayoutParams.WRAP_CONTENT);
TextView popup_text = (TextView) popupView
.findViewById(R.id.popup_text);
popup_text.setMinimumWidth(300);
popup_text.setText(pop_text);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
popupWindow.showAsDropDown(popupView,p.x/10, p.y +p.x/8);
System.out.println("value of x" + p.x+" "+p.y);
//System.out.println("value of new width " + new_width+" get width "+popupWindow.getWidth()+" text box width "+popup_text.getWidth()+"pop view"+popupView.getWidth());
popupWindow.setFocusable(true);
}
Regards,
Asmi
I think this will solve your problem call this on button click
popupWindow.dismiss();
You need to set the property setOutsideTouchable(false) of popup window
what you need is a boolean isShowing set this boolean to be true in the function popupshow
like this
public void popupshow(String pop_text, ImageView pop_one) {
isShowing=true;
int width = display.getWidth(); // deprecated
System.out.println("jsfjsfjnsdf"+width);
int new_width = width-(width/6) ;
LayoutInflater layoutInflater = (LayoutInflater) getActivity()
.getBaseContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(
R.layout.career_options_popup_short, null);
final PopupWindow popupWindow = new PopupWindow(popupView, new_width,
LayoutParams.WRAP_CONTENT);
TextView popup_text = (TextView) popupView
.findViewById(R.id.popup_text);
popup_text.setMinimumWidth(300);
popup_text.setText(pop_text);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
popupWindow.showAsDropDown(popupView,p.x/10, p.y +p.x/8);
System.out.println("value of x" + p.x+" "+p.y);
//System.out.println("value of new width " + new_width+" get width "+popupWindow.getWidth()+" text box width "+popup_text.getWidth()+"pop view"+popupView.getWidth());
popupWindow.setFocusable(true);
}
now when you click on button1 just check for this boolean
pop_one.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
if(isShowing)
{
popupWindow.dismiss();
isShowing=false;
}
else
{
pop_one.getLocationOnScreen(location);
p = new Point();
p.x = location[0];
p.y = location[1];
popupshow(one_one_text,pop_one);
}
}
});

Categories

Resources