I can't show a popup window below the ImageView that called - android

I have a ListView of items, each item have an ImageView by clicking on it displays a PopupWindow. The problem is I can not show the window below the ImageView that called. The OnClickListener of the ImageView that call the pw is implemented inside getView() method of my listadapter class.
How I can do to show the pw below the respective ImageView?
ListView
I tried to do it using this code, but it did not work. onGlobalLayout() is never called.
#Override public View getView(int position, View convertView, final ViewGroup parent) {
...
// ImageView that show the PopupWindow
holder.menuOverflow = (ImageView) row.findViewById(R.id.iv_menu_overflow_item_listview_seccion);
holder.menuOverflow.setTag(position);
holder.menuOverflow.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
ViewTreeObserver vto = (holder.menuOverflow).getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
y = (int) (holder.menuOverflow).getY();
}
});
...
// Show the PopupWindow
pw.showAtLocation(parent, Gravity.RIGHT, 0, y);
// Animation
Animation anim = AnimationUtils.loadAnimation(activity, R.anim.grow_from_top);
anim.setDuration(100);
popupView.startAnimation(anim);
}
});
return row;
}

Solved by me:
int[] _location = new int[2];
// Point (x, y) stored in array _location
// _location[0] = x, _location[1] = y.
v.getLocationOnScreen(_location);
//Initialize the Point with x, and y positions
Point p = new Point();
p.x = _location[0];
p.y = _location[1];
int OFFSET_Y = 30;
// muestra la popup window
pw.showAtLocation(activity.findViewById(R.id.sliding_panel_layout), Gravity.NO_GRAVITY, p.x, p.y + OFFSET_Y);

Related

How can I load WebView in custom popup window in android?

I have Create popup.xml and I want to load my webpage in WebView from assets folder.
Here is my popup.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/pp"
android:orientation="vertical" >
<WebView
android:id="#+id/webview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Here is my activity file
How to load my webpage in webview with popup window?
Is there any solution?
public class MainActivity extends ActionBarActivity {
Point p;
public int a, b, c, d;
ImageButton au, gc, cert, busa;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
au = (ImageButton) findViewById(R.id.au);
gc = (ImageButton) findViewById(R.id.gc);
cert = (ImageButton) findViewById(R.id.cert);
busa = (ImageButton) findViewById(R.id.busa);
au.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (p != null) {
showPopup(MainActivity.this, p);
}
}
});
}
// Get the x and y position after the button is draw on screen
// (It's important to note that we can't get the position in the onCreate(),
// because at that stage most probably the view isn't drawn yet, so it will
// return (0, 0))
#Override
public void onWindowFocusChanged(boolean hasFocus) {
int[] location = new int[2];
// Get the x, y location and store it in the location[] array
// location[0] = x, location[1] = y.
// 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) {
int popupWidth = 720;
int popupHeight = 380;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context
.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
// Some offset to align the popup a bit to the right, and a bit down,
// relative to button's position.
int OFFSET_X = 130;
int OFFSET_Y = 100;
// 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);
// Getting a reference to Close button, and close the popup when
// clicked.
}
}
Webview
webview1.loadURL("the url goes here");

PopWindow always show at last in ListView...!

I working on android PopupWindow. In this application I create 1 custom ListView. Now I want to show PopupWindow when user click on TextView of custom ListView.
My problem is :
PopupWindow always show at last TextView even if I click on first or other TextView . How can I resolve this.??
TextView of ListView.
holder.end.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int[] location = new int[2];
holder.end.getLocationOnScreen(location);
p = new Point();
p.x = location[0];
p.y = location[1];
if (p != null){
showPopup(context,p);
holder.popupText.setText(holder.end.getText().toString());
}
}
});
My popupWindow function.
private void showPopup(final Activity context, Point p) {
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout)context.findViewById(R.id.layoutPopup);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);
holder.popupText = (TextView) layout.findViewById(R.id.showPopUp);
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(LinearLayout.LayoutParams.WRAP_CONTENT);
popup.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
popup.setFocusable(true);
int OFFSET_X = 30;
int OFFSET_Y = 30;
popup.setBackgroundDrawable(new BitmapDrawable());
popup.showAtLocation(layout.findViewById(R.id.showPopUp), Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
}
Tray changing your click listener with this code. It should work.
holder.end.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int[] location = new int[2];
v.getLocationOnScreen(location);
p = new Point();
p.x = location[0];
p.y = location[1];
if (p != null){
showPopup(context,p);
holder.popupText.setText(holder.end.getText().toString());
}
}
});

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;
}

PopupWindow or PopupMenu? Or something else?

I'm trying to create very similar layout like Google Play Books. I need to create an PopupMenu or PopupWindow, which will be displayed at the same position like in the picture below.
I am not sure, if it is PopupMenu or PopupWindow or something else. This "window" is displayed when I click on the item from menu.
In case, that I change an orientation to landscape mode, in ActionBar is a new icon, which displays this "window". The picture below shows this situation:
I have two questions:
Is this a PopupMenu, PopupWindow or something else?
How can I determine the position of this "window" in case, that it is the PopupWindow and I need to show it?
You have to create an OnClickListener like this:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int[] location = new int[2];
v.getLocationOnScreen(location);
// Initialize the Point with x, and y positions
Point point = new Point();
point.x = location[0];
point.y = location[1];
popupWindow(v, point);
}
});
The method for popupWindow(View view, Point point):
private ArrayAdapter<String> adapterTypeSelection;
private void popupWindow(View v, Point p) {
// int popupwindowWidth =
// UnitConverterClass.convertDpToPx(180,getActivity());
int popupwindowHeight = LinearLayout.LayoutParams.WRAP_CONTENT;
LayoutInflater layoutInflater = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(
R.layout.dashboard_profile_popup_window, null);
// Creating the PopupWindow
final PopupWindow pwindow = new PopupWindow(getActivity());
pwindow.setContentView(layout);
// pwindow.showAsDropDown(v);
// pwindow.setWidth(popupwindowWidth);
pwindow.setHeight(popupwindowHeight);
pwindow.setFocusable(true);
String[] types = {"hello", "hi"};
adapterTypeSelection = new ArrayAdapter<String>(getActivity(),
R.layout.<your layout for the popupwindow>,
R.id.textView, types);
ListView listview = (ListView) pwindow.getContentView().findViewById(
R.id.listview_popwindow);
listview.setAdapter(adapterTypeSelection);
listview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView temp = (TextView) parent.getChildAt(position)
.findViewById(R.id.textView);
if (temp.getText()
.toString()
.equals("hello"))) {
//hello
} else {
//hi
}
pwindow.dismiss();
}
});
pwindow.setOnDismissListener(new OnDismissListener() {
#Override
public void onDismiss() {
//TODO dismiss settings
}
});
pwindow.setWidth(<width>);
pwindow.setBackgroundDrawable(<resource for background>);
// int OFFSET_X = UnitConverterClass.convertDpToPx(180, getActivity());
// int OFFSET_Y = UnitConverterClass.convertDpToPx(30, getActivity());
// pwindow.showAtLocation(layout, Gravity.NO_GRAVITY,
// p.x + OFFSET_X, p.y + OFFSET_Y);
pwindow.showAtLocation(layout, Gravity.NO_GRAVITY, p.x, p.y);
}
Hope this helps to make a PopupWindow in the position you want to show it.

Adding Buttons dynamically to a Android popup Window

I need help with creating a dynamic array of 4 EditTexts inside a pop-up window. The implementation of the pop-up window with the dynamically EditText will work great if the pop-up was it's own activity;however, a pop up should not have it's own activity but use the activity of the class that it is in. Can you please shed some light on this. I believe this has to deal with application context. Here is the code.
public class SideBucket extends Activity {
//The "x" and "y" position of the "Show Button" on screen.
private Point p;
private String[] _hold;
private LinearLayout viewGroup;
private TextView tv;
private View layout;
private EditText[] _edText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_side);
tv = (TextView) findViewById(R.id.textView1);
this.buttonActions();//explicit call to this class.
onPause();
}
#Override
protected void onPause(){
super.onPause();
buttonActions();
}
public void buttonActions(){
// Array for buttons that initially show up on the main Activity.
final Button[] b = {(Button) findViewById(R.id.button1), (Button) findViewById(R.id.side_next)};
for(int i=0;i<b.length;i++){
b[i].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//this button is in the main activity
and when pressed the popped out window shows up.
if(b[0].isPressed()){
//Open popup window
if(p != null)
{showPopup(SideBucket.this, p);}
//for(int t=0;t<_edText.length;t++){
//_edText[t].setText(_hold[t]);
//}
}//end of if statement
// this button is in the main activity
of the sideBucket mxl file and it switches intents.
if(b[1].isPressed()) {
Intent intent = new
Intent(SideBucket.this,FrontBucket.class);
startActivity(intent);
}
//end for view v
}});
}//for loop
}
// Get the x and y position after the button is draw on screen
// (It's important to note that we can't get the position in the onCreate(),
// because at that stage most probably the view isn't drawn yet, so it will return (0, 0))
#Override
public void onWindowFocusChanged(boolean hasFocus) {
int[] location = new int[2];
Button button = (Button) findViewById(R.id.button1);
// Get the x, y location and store it in the location[] array
// location[0] = x, location[1] = y.
button.getLocationOnScreen(location);
//Initialize the Point with x, and y positions
p = new Point();
p.x = location[0];
p.y = location[1];
}
public void showPopup(final Activity context, Point p) {
int popupWidth = 230;
int popupHeight = 330;
// Inflate the popup_layout.xml
viewGroup = (LinearLayout) context.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater)
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = layoutInflater.inflate(R.layout.popup_layout,viewGroup);
//===============================//
//sets dimensions
LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);
//goal how can we get layout to view the editText
_edText = new EditText[4];
for (int i = 0;i< _edText.length;i++) {
_edText[i] = new EditText(getApplicationContext());
btn[i].setText(btn[i].toString());
// _edText[i].setTextColor(Color.parseColor("#000000"));// why is the color parsed
_edText[i].setLayoutParams(param);
viewGroup.addView(_edText[i]);
viewGroup.addView(_edText[i]);
}
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(layout,350,350,true); // context argument was taken out of this method.
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
// Some offset to align the popup a bit to the right, and a bit
down, relative to button's position.
int OFFSET_X = -100;
int OFFSET_Y = 100;
// 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);
//Refference to editText in popup in an array.
// this array should be changed later on to create something more
dynamically robust.
//========================================================//
//Button calculate = (Button) layout.findViewById(R.id.close_calc);
//button inside of pop-up layout.
//calculate.setOnClickListener(new OnClickListener() {
// #Override
// public void onClick(View v) {
// _hold = new String[4];
// for(int i=0;i<_hold.length;i++){
// _hold[i] = _edText[i].getText().toString();
// }
// tv.setText("Result1:"+_hold[0]+"\n"+"Result2:"+_hold[1]+"
\n"+"Result3:"+_hold[2]+"\n"+"Result4:"+_hold[3]);
// popup.dismiss();
//}
//});
}
}

Categories

Resources