Skip to content

Instantly share code, notes, and snippets.

@slaypni
Created October 2, 2012 07:45
Show Gist options
  • Save slaypni/3817116 to your computer and use it in GitHub Desktop.
Save slaypni/3817116 to your computer and use it in GitHub Desktop.
A class for showing a view as pop-up [Android]
package com.example.utils;
import android.app.Activity;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
public class PBPopupBanner {
private Activity originalActivity_;
private FrameLayout baseLayout_;
private RelativeLayout backgroundLayout_;
private View contentView_;
public PBPopupBanner(Activity activity)
{
originalActivity_ = activity;
baseLayout_ = new FrameLayout(originalActivity_);
backgroundLayout_ = new RelativeLayout(originalActivity_);
backgroundLayout_.setBackgroundColor(Color.argb(128, 0, 0, 0));
backgroundLayout_.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
backgroundLayout_.setGravity(Gravity.CENTER);
final PBPopupBanner self = this;
backgroundLayout_.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
self.dismiss();
}
});
}
public PBPopupBanner(Activity activity, View view)
{
this(activity);
setView(view);
}
public void setView(View view)
{
contentView_ = view;
}
public void show()
{
ViewGroup contentRoot = (ViewGroup)originalActivity_.findViewById(android.R.id.content);
int count = contentRoot.getChildCount();
for (int i = 0; i < count; i++){
View view = contentRoot.getChildAt(i);
contentRoot.removeView(view);
baseLayout_.addView(view);
}
baseLayout_.addView(backgroundLayout_);
backgroundLayout_.addView(contentView_);
contentRoot.addView(baseLayout_);
}
public void dismiss()
{
ViewGroup contentRoot = (ViewGroup)originalActivity_.findViewById(android.R.id.content);
contentRoot.removeView(baseLayout_);
backgroundLayout_.removeView(contentView_);
baseLayout_.removeView(backgroundLayout_);
int count = baseLayout_.getChildCount();
for (int i = 0; i < count; i++){
View view = baseLayout_.getChildAt(i);
baseLayout_.removeView(view);
contentRoot.addView(view);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment