I need to get the onclick event only on the button, but when the user clicks on the view (windowManager), I want to pass the touch to the fragment behind. My problem is that I get the onclick of the button but I don't know how to pass the event to the view behind. Any suggestions?
public class ReconnectionLinkedInOverlayView extends View {
private final Context mContext;
private final ViewGroup mPopupLayout;
private Button refresh;
private WindowManager mWinManager;
public ReconnectionLinkedInOverlayView(Activity activity) {
super(activity.getApplicationContext());
mContext = activity.getApplicationContext();
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
|WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
|WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
mWinManager = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mPopupLayout = (RelativeLayout) inflater.inflate(R.layout.notification_linkedin_reconnection_card, null);
mPopupLayout.setVisibility(GONE);
refresh = (Button) mPopupLayout.findViewById(R.id.notification_linked_in_reconnection_button);
Typeface face = Typeface.createFromAsset(activity.getAssets(),
"fonts/OpenSans-Regular.ttf");
refresh.setTypeface(face);
refresh.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
hide();
}
});
mPopupLayout.setTouchInterceptor(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
Log.i(TAG, "PopupWindow :: onTouch()");
return false;
}
});
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
mWinManager.addView(mPopupLayout, params);
mPopupLayout.setVisibility(GONE);
}
/**
* Shows view
*/
public void show() {
final Animation in = AnimationUtils.loadAnimation(this.mContext, android.R.anim.fade_in);
in.setDuration(1000);
mPopupLayout.setVisibility(VISIBLE);
mPopupLayout.startAnimation(in);
}
/**
* Hides view
*/
public void hide() {
final Animation in = AnimationUtils.loadAnimation(this.mContext, android.R.anim.fade_out);
in.setDuration(1000);
if(mPopupLayout != null)
mWinManager.removeView(mPopupLayout);
mPopupLayout.setVisibility(GONE);
}
}
Related
I am trying to block status bar and I am successful in doing that by adding a view to status bar. I am trying to remove the added view when onClick is detected, but the view still remains and I am not able to remove the view. Can any one please let me know what am I missing. The current code is as shown below:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
manager = ((WindowManager) getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE));
localLayoutParams = new WindowManager.LayoutParams();
localLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
localLayoutParams.gravity = Gravity.TOP;
localLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
// this is to enable the notification to recieve touch events
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
// Draws over status bar
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
localLayoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
localLayoutParams.height = (int) (50 * getResources()
.getDisplayMetrics().scaledDensity);
localLayoutParams.format = PixelFormat.TRANSPARENT;
view = new customViewGroup(this);
manager.addView(view, localLayoutParams);
// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
// WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
m_activateButton = (Button)findViewById(R.id.button);
m_activateButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(manager != null){
manager.removeView(view);
}
}});
}
CustomView Class:
public class customViewGroup extends ViewGroup {
public customViewGroup(Context context) {
super(context);
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.v("customViewGroup", "**********Intercepted");
return true;
}
}
In an android application I have to show an internal notification that comes over the top of every screen in my app . I have used WindowManager. But it doesn't seem to work as expected. Sometimes I see the first notification but the second is not shown though when debugged ,step by step execution of the following method is visible. Can't figure out why is this happening. Help!!
public void addNotification(String message, Bundle bundle) {
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
lp.gravity = Gravity.END | Gravity.BOTTOM;
lp.x = getResources().getInteger(R.integer.notification_side_margin);
lp.y = getResources().getInteger(R.integer.notification_side_margin);
// Initialise container view first in which every notification is to be
// added.
if (notificationConatinerView == null) {
notificationConatinerView = getLayoutInflater().inflate(
R.layout.custom_notification_main_layout, null);
notificationContainer = (LinearLayout) notificationConatinerView
.findViewById(R.id.notification_container);
notificationContainer.setPadding(
(int) getResources().getDimension(R.dimen.mini_margin),
(int) getResources().getDimension(R.dimen.mini_margin),
(int) getResources().getDimension(R.dimen.mini_margin),
(int) getResources().getDimension(R.dimen.mini_margin));
windowManager.addView(notificationConatinerView, lp);
// Add that container to the window manager
windowManager.updateViewLayout(notificationConatinerView, lp);
}
final View view = getLayoutInflater().inflate(
R.layout.notification_custom, null);
view.setTag(bundle);
view.setId(notificationContainer.getChildCount());
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Bundle bundle = (Bundle) v.getTag();
setClickForNotifications(bundle);
}
});
final ImageView crossImage = (ImageView) view
.findViewById(R.id.notification_cross);
crossImage.setTag(view);
TextView messageTextView = (TextView) view
.findViewById(R.id.message_text);
messageTextView.setText(message);
crossImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (notificationContainer.getChildCount() == 1) {
showNotificationExitingView(v);
} else {
if (!isFinishing())
notificationContainer.removeView((View) crossImage
.getTag());
}
}
});
LinearLayout.LayoutParams childLP = new LinearLayout.LayoutParams(
(int) getResources().getDimension(
R.dimen.notification_view_width),
RelativeLayout.LayoutParams.WRAP_CONTENT);
if (notificationContainer.getChildCount() == getResources().getInteger(
R.integer.no_of_notifications)) {
notificationContainer.removeViewAt(notificationContainer
.getChildCount() - 1);
}
notificationContainer.addView(view, 0, childLP);
notificationContainer.updateViewLayout(view, childLP);
Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
if (notificationContainer.getChildCount() == 1) {
showNotificationExitingView(view);
} else {
if (!isFinishing())
notificationContainer.removeView(view);
}
}
}, 10000);
try {
windowManager.updateViewLayout(notificationConatinerView, lp);
} catch (Exception e) {
windowManager.addView(notificationConatinerView, lp);
windowManager.updateViewLayout(notificationConatinerView, lp);
}
if (notificationContainer.getChildCount() == 1) {
showNotificationEnteringAnimation();
}
}
Thanks!!
use
windowManager = (WindowManager) getApplicationContext().getSystemService(WINDOW_SERVICE);
instead of
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
I am trying to display a webview inside popupwindow which is being displayed by a service. When a user
touch on an image displayed by a service then a popupwindow should appear displaying a webview in it.
The code for the whole process is given below:
ChatHeadService.java
public class ChatHeadService extends Service {
private WindowManager windowManager;
float screenWidth=0, screenHeight=0;
private ImageView chatHead;
WindowManager.LayoutParams params;
WebView popUpWebView;
private static final int MAX_CLICK_DURATION = 200;
private long startClickTime;
private GestureDetector gestureDetector;
#Override public IBinder onBind(Intent intent) {
// Not used
return null;
}
#Override public void onCreate() {
super.onCreate();
screenWidth= getResources().getDisplayMetrics().widthPixels;
screenHeight= getResources().getDisplayMetrics().heightPixels;
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 0;
chatHead = new ImageView(this);
chatHead.setImageResource(R.drawable.ic_launcher);
windowManager.addView(chatHead, params);
addChatHeadListener();
}
public void addChatHeadListener(){
chatHead.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
showPopUpWindow();
}
});
}
public void showPopUpWindow(){
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popupwindow, null);
final PopupWindow popupWindow = new PopupWindow(popupView,
(int)screenWidth,
(int)screenHeight);
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable (new BitmapDrawable());
popUpWebView = (WebView) popupView.findViewById(R.id.idWebView1);
popUpWebView.getSettings().setJavaScriptEnabled(true);
popUpWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
popupWindow.showAsDropDown(chatHead, 0, 0);
popUpWebView.loadUrl("http://wwww.google.co.in");
}
}
And here is my xml file
popupwindow.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff" >
<WebView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:id="#+id/idWebView1"/>
</LinearLayout>
Problem is nothing displayed when i clicked on the icon but when i use textview instead of webview
it diplayed textview successfully.
I am using Slider like gmail or Facbook in android this is fine. What i have done is i just created a new activity and given animation to it so it looks like slider. Now what i want is when user click any button in slider a new activity should open and perform task what i specify in it. But i am not getting how to pass click event in slider.
My code
public class PlayAudio extends Activity implements OnCompletionListener,
SeekBar.OnSeekBarChangeListener {
boolean alreadyShowing = false;
private int windowWidth;
private int windowHeight;
private PopupWindow fadePopup;
private Animation ta;
private RelativeLayout baseView;
LayoutInflater inflater;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
Display display = getWindowManager().getDefaultDisplay();
windowWidth = display.getWidth();
windowHeight = display.getHeight();
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
findViewById(R.id.imageView1).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (!alreadyShowing) {
alreadyShowing = true;
openSlidingMenu();
}
}
});
findViewById(R.id.imageView3).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (!alreadyShowing) {
alreadyShowing = true;
openSlidingMenu();
}
}
});
}
private void openSlidingMenu() {
showFadePopup();
int width = (int) (windowWidth * 0.8f);
translateView((float) (width));
#SuppressWarnings("deprecation")
int height = LayoutParams.FILL_PARENT;
// creating a popup
final View layout = inflater.inflate(R.layout.option_popup_layout,
(ViewGroup) findViewById(R.id.popup_element));
final PopupWindow optionsPopup = new PopupWindow(layout, width, height,
true);
optionsPopup.setBackgroundDrawable(new PaintDrawable());
optionsPopup.showAtLocation(layout, Gravity.NO_GRAVITY, 0, 0);
optionsPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
public void onDismiss() {
// Removing the fade effect
fadePopup.dismiss();
// to clear the previous animation transition in
cleanUp();
// move the view out
translateView(0);
// to clear the latest animation transition out
cleanUp();
// resetting the variable
alreadyShowing = false;
}
});
}
private void showFadePopup() {
final View layout = inflater.inflate(R.layout.fadepopup,
(ViewGroup) findViewById(R.id.fadePopup));
fadePopup = new PopupWindow(layout, windowWidth, windowHeight, false);
fadePopup.showAtLocation(layout, Gravity.NO_GRAVITY, 0, 0);
}
private void translateView(float right) {
ta = new TranslateAnimation(0f, right, 0f, 0f);
ta.setDuration(100);
ta.setFillEnabled(true);
ta.setFillAfter(true);
baseView = (RelativeLayout) findViewById(R.id.Player_Slider);
baseView.startAnimation(ta);
baseView.setVisibility(View.VISIBLE);
}
private void cleanUp() {
if (null != baseView) {
baseView.clearAnimation();
baseView = null;
}
if (null != ta) {
ta.cancel();
ta = null;
}
fadePopup = null;
}
}
Here in click event i am calling openSlidingMenu function which open's new activity option_popup_layout (http://i.share.pho.to/fb125f6f_o.png)
Now what i want is when i click any of the image in option_popup_layout a new activity should open and i want perform some operation over there but i am not getting where i have to use click event to call new activity.
I have tried some piece of code to put click event inside creating function as well as other method too but not getting work.
Please help me to solve this. Thanks in advance.
I have a LinearLayout into my PopupView, and a button into this LinearLayout. Event listener of this button works correctly, but animation of pressing (highlighting) is not starting. What I did wrong? Please, gelp!
public class PopupView extends View
{
private PopupWindow popUp;
private Button buttonOk;
private OnClickListener onClick;
private LayoutParams params;
private int popUpWidth = 0;
private int popUpHeight = 0;
private int popUpX, popUpY;
public PopupView(Context context, View parent)
{
super(context);
popUp = new PopupWindow(context);
popUpLayout = new LinearLayout(context);
buttonOk = new Button(context);
buttonOk.setText("OK");
buttonOk.setId(1);
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
popUpLayout.setOrientation(LinearLayout.VERTICAL);
popUp.setContentView(popUpLayout);
onClick = new OnClickListener()
{
public void onClick (View v)
{
switch (v.getId())
{
case 1:
System.out.println("OK pressed!");
break;
case 2:
break;
}
}
};
buttonOk.setOnClickListener(onClick);
popUpLayout.removeAllViews();
popUpLayout.addView(buttonOk, params);
popUpWidth = 200;
popUpHeight = 400;
popUpX = 300;
popUpY = 300;
popUp.showAtLocation(parent, Gravity.NO_GRAVITY, popUpX, popUpY);
popUp.update(popUpX, popUpY, popUpWidth, popUpHeight);
}
}
In default PopupView is not focusble. Need to call method popup.setFocusable(true). Thanks for all!