QWERTY
QWERTY

Reputation: 2315

Android getWindow() undefined when trying to dim activity background

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.

Upvotes: 0

Views: 1712

Answers (2)

KomalG
KomalG

Reputation: 778

getWindow().setBackgroundDrawable(new ColorDrawable(Color.argb(150, 0, 0, 0)));

for dim the activity do as above.

Upvotes: 0

zzy
zzy

Reputation: 1793

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);

Upvotes: 1

Related Questions