show pop up like gmail in android [closed]

2020-07-14 06:19发布

问题:

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 7 years ago.

I want to show an alert in my android just like gmail notification in desktop

how should I approach.

Solution made it showing like this

回答1:

Dialogs cannot be triggered without an activity..

So ,you can create an activity with dialog theme.

    <activity android:theme="@android:style/Theme.Dialog" />

Now from your service ..just call this activity when your notification arrives... And it will pop up like a dialog...

Edit:

While calling your activity:

   startActivity(intent);
   overridePendingTransition(R.anim.enter_anim, R.anim.right_exit_anim);

Now create the two anim files in a seperate folder called anim in the resource dir.

enter_anim:

   <?xml version="1.0" encoding="utf-8"?>
   <set xmlns:android="http://schemas.android.com/apk/res/android"    
        android:interpolator="@android:anim/accelerate_decelerate_interpolator">
   <translate
    android:fromYDelta="20%p" //this takes val from 0(screenbottom) to 100(screentop).
    android:toYDelta="0%p"  //this takes val from 0(screenbottom) to 100(screentop).
    android:duration="700"   //transition timing
    />
   </set>

exit_anim:

   <?xml version="1.0" encoding="utf-8"?>
   <set xmlns:android="http://schemas.android.com/apk/res/android"    
        android:interpolator="@android:anim/accelerate_decelerate_interpolator">
   <translate
    android:fromYDelta="0%p" //this takes val from 0(screenbottom) to 100(screentop).
    android:toYDelta="20%p"  //this takes val from 0(screenbottom) to 100(screentop).
    android:duration="700"   //transition timing
    />
   </set>

EDIT 2:

Create an activity.. design it..then go to your manifest file.. And under your activity tag.. add:

    <activity android:theme="@android:style/Theme.Dialog" />

Now your activity will look like a dialog...

EDIT 3:

Now add the following function in your activity(dialog) after onCreate():

    @Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();

    View view = getWindow().getDecorView();
    WindowManager.LayoutParams lp = (WindowManager.LayoutParams) view.getLayoutParams();
    lp.gravity = Gravity.RIGHT | Gravity.BOTTOM;//setting the gravity just like any view
    lp.x = 10;
    lp.y = 10;
    lp.width = 200;
    lp.height = 100;
    getWindowManager().updateViewLayout(view, lp);
}

We are overriding the attach window to specify the activity location on the screen.

Now your activity will be placed in the right side bottom of the screen.

EDIT 4:

Now to give your specified co ordinate points for the dialog, use the lp.x and lp.y...