0

I was having some problem when trying to dim the background of Activity without dialog for a few seconds.

So what I am trying to do is when button on click, it will dim the background for a few seconds. Then at the same time, my another intent is loading up the content. Once the content finished loaded, the it will shift to another page. Here is the codes:

ivTwitterShare.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            WindowManager.LayoutParams windowManager = getWindow().getAttributes();
            windowManager.dimAmount = 0.75f;
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            // Create intent using ACTION_VIEW and a normal Twitter url
            tweetUrl = String
                    .format("",
                            urlEncode("Come join us for "
                                    + txtEventDtlName.getText().toString()),
                            urlEncode("at "
                                    + txtEventDtlAddr.getText().toString()
                                    + " on "
                                    + txtEventDtlDate.getText().toString()));

            Thread newThread = new Thread() {
                @Override
                public void run() {
                    try {
                        super.run();
                        sleep(10000);
                    } catch (Exception e) {

                    } finally {
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri
                                .parse(tweetUrl));
                        // Narrow down to official Twitter app
                                                    startActivity(intent);                      }
                }
            };
            newThread.start();
        }
    });

However, I am getting error message at the getWindow(): The method getWindow() is undefined for the type new View.OnClickListener(){}

Any ideas? Thanks in advance.

QWERTY
  • 2,303
  • 9
  • 44
  • 85

2 Answers2

1

You should use YourActivityClassName.this.getWindow()

Update

OK, I know you just want to dim the back and then start a activity, so you don't need the animation below.

The problem occurs in:

WindowManager.LayoutParams windowManager = getWindow().getAttributes();
windowManager.dimAmount = 0.75f;

you should pust windowManager into the attr of Window. It's same to all LayoutParams.

WindowManager.LayoutParams windowManager = getWindow().getAttributes();
windowManager.dimAmount = 0.75f;
getWindow().setAttributes(windowManager);

Here is the source code of setAttributes:

    /**
 * Specify custom window attributes.  <strong>PLEASE NOTE:</strong> the
 * layout params you give here should generally be from values previously
 * retrieved with {@link #getAttributes()}; you probably do not want to
 * blindly create and apply your own, since this will blow away any values
 * set by the framework that you are not interested in.
 *
 * @param a The new window attributes, which will completely override any
 *          current values.
 */
public void setAttributes(WindowManager.LayoutParams a) {
    mWindowAttributes.copyFrom(a);
    if (mCallback != null) {
        mCallback.onWindowAttributesChanged(mWindowAttributes);
    }
}

It shows you need to call it to tell the view change.

-------------------------------------old answer-----------------

Use xml to define the animation of dialog (share_fade_in.xml):

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">

    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:duration="200"
    />
</set>

This is fade in , and for fade out ,you should exchange the android:fromAlpha and android:toAlpha's value.

Define Style for your dialog in style.xml

<style name="AnimBottom" parent="@android:style/Animation">
    <item name="android:windowEnterAnimation">@anim/share_fade_in</item>
    <item name="android:windowExitAnimation">@anim/share_fade_out</item>
</style>

And then Apply to your dialog:

this.getWindow().setWindowAnimations(R.style.AnimBottom);
zzy
  • 1,771
  • 1
  • 13
  • 48
  • From the codes I provided, do you have any ideas how should I dim the background and at the same time, execute the thread? – QWERTY Dec 26 '14 at 08:41
  • @Denise You can simply use Dialog to show your dialog and use Animation to dim the back. – zzy Dec 26 '14 at 08:44
  • Nope I do not want a dialog. I just wanted to dim the background and start the loading of another page. Then once all finish loaded up, then move to another page – QWERTY Dec 26 '14 at 08:45
  • I am using a thread because if I did not do so, it will shows me a white and blank activity for a few seconds before my twitter intent shows up – QWERTY Dec 26 '14 at 08:46
0
getWindow().setBackgroundDrawable(new ColorDrawable(Color.argb(150, 0, 0, 0)));

for dim the activity do as above.

KomalG
  • 808
  • 9
  • 18