Is There a Simple Example of the Popupwindow Class Using Android V2.0

Is there a simple example of the PopupWindow class using Android v2.0?

I created a working example based on this Google Groups post.

To create a simple working PopupWindow, you'll need to do the following:

  1. Create a layout XML which describes the View that will be rendered within the PopupWindow.
  2. Invoke the PopupWindow by inflating the layout XML, and assign the appropriate "parent view" to the pop-up.

popup_example.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Test Pop-Up"
/>

</LinearLayout>

Java code:

    LayoutInflater inflater = (LayoutInflater)
this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
PopupWindow pw = new PopupWindow(
inflater.inflate(R.layout.popup_example, null, false),
100,
100,
true);
// The code below assumes that the root container has an id called 'main'
pw.showAtLocation(this.findViewById(R.id.main), Gravity.CENTER, 0, 0);

How to instantiate PopupWindow?

The point of the APIs is to provide technical documentation. Their primary purpose is as a reference material, not as a learning tool. That said you can learn a lot from reading them. In general I find that googleing for tutorial tends to get me started in the right direction. There are lots of examples of PopupWindows in android, check out this StackOverflow post: Is there a simple example of the PopupWindow class using Android v2.0?

How to create a popup window (PopupWindow) in Android

Here, I am giving you a demo example. See this and customize it according to your need.

public class ShowPopUp extends Activity {
PopupWindow popUp;
boolean click = true;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
popUp = new PopupWindow(this);
LinearLayout layout = new LinearLayout(this);
LinearLayout mainLayout = new LinearLayout(this);
TextView tv = new TextView(this);
Button but = new Button(this);
but.setText("Click Me");
but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (click) {
popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
popUp.update(50, 50, 300, 80);
click = false;
} else {
popUp.dismiss();
click = true;
}
}
});

LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
tv.setText("Hi this is a sample text for popup window");
layout.addView(tv, params);
popUp.setContentView(layout);
// popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
mainLayout.addView(but, params);
setContentView(mainLayout);
}
}

Hope this will solve your issue.

How to implement view/interface like this in android?

It could be simply implemented using android-popupwindow.

Examples:

  • http://mrbool.com/how-to-implement-popup-window-in-android/28285
  • Is there a simple example of the PopupWindow class using Android v2.0?

Basic Steps:

  1. Create a layout in XML for the menu.
  2. Inflate that layout and apply to the PopupWindow.
  3. Acquire a reference to that button. (i.e. Add to Groups);
  4. Find its absolute coordination on the window.
  5. Use showAtLocation to show the menu in the appropriate location.

Note: AFAIK, there's a bunch of libraries out there which fit your requirements. So, don't reinvent the wheel.

Android PopupWindow dismiss interrupts the ripple effect half way through

I had this issue and struggled 2 weeks to fix it. Yes dismiss Popup Window is culprit, action dismiss() stops/clears all the pending animations.

Here is what I did to show the ripple effect on clicking the item in my custom PopupWindow:

    // Define click listener for the ViewHolder's View.
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewCompat.postOnAnimationDelayed(v, new Runnable()
{
@Override
public void run()
{
Log.i("test", "Position: "+position);
// Perform Action
}
}, 150); // Delay the onClick action

}
});

This will delay the onClick action by 150 microseconds, which will allow ripple effect to take place before dismiss() is triggered the Popup Window.

You must adjust onClick delay/duration and the dismiss fade animation duration to make it work as expected.

Android Pop up window

May be AlertDialog can solve your problem you can full screen dialog window it just look like a window it is an alternative

Android popup window dismissal

This is because the popup window does not respond to onTouch or onKey events unless it has a background that != null. Check out some code I wrote to help with this. In the basic case you can to call PopupWindow#setBackgroundDrawable(new BitmapDrawable()) to force it to act the way you expect. You won't need your own onKey listener. You might also need to call PopupWindow#setOutsideTouchable(true) if you want it to go away when the user clicks outside of the window boundaries.

Extended esoteric answer:

The reason the background cannot be null is because of what happens in PopupWindow#preparePopup. If it detects background != null it creates an instance of PopupViewContainer and calls setBackgroundDrawable on that and puts your content view in it. PopupViewContainer is basically a FrameLayout that listens for touch events and the KeyEvent.KEYCODE_BACK event to dismiss the window. If background == null, it doesn't do any of that and just uses your content view. You can, as an alternative to depending on PopupWindow to handle that, extend your root ViewGroup to behave the way you want.

Show popup window from another class Android

You need to call a method to actually show the popup on some event action or whenever you need it. Here are the different methods from the docs

Here is one example of using showAtLocation().

showAsDropDown(View anchor) may be the simplest depending on your needs. Just pass it the view you want it to attach to. Though, the other two give you more flexibility on where it shows.

How does a popup window on Android work if needed in non-Activity class?

You will have to do it in the Activity. You have two options:

1) When you know the game is over, have a method in your "controller" class that returns whether or not the popup should be displayed. Something like isGameWon(). Call this from your Activity and respond accordingly.

2) Keep a reference to your Activity in your controller class, or allow access to your Activity as a singleton object. When the game ends, check if the player won in your controller class, and if so, call a function in your Activity to display the popup, like showGameWonPopup().

You can't display a popup outside of the UI Activity. Your two classes need to communicate about the end result of the game and respond accordingly.



Related Topics



Leave a reply



Submit