ContentLoadingProgressBarActivity.java revision bd6d975bf288efaa3392eb73d98579af3e7e4688
1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.example.android.supportv4.widget;
18
19import android.app.Activity;
20import android.widget.Button;
21import android.widget.LinearLayout;
22import android.widget.ProgressBar;
23import android.widget.TextView;
24import android.os.Bundle;
25import android.os.Handler;
26import android.support.v4.widget.ContentLoadingProgressBar;
27import android.view.View;
28import android.view.View.OnClickListener;
29import android.view.ViewTreeObserver;
30import android.view.Window;
31
32import com.example.android.supportv4.R;
33
34/**
35 * Demonstrates how to use the ContentLoadingProgressBar. By default, the
36 * developer should start the ContentLoadingProgressBar with visibility of
37 * "gone" or "invisible". The ContentLoadingProgressBar will be shown after the
38 * default delay for at least a minimum time regardless of when the "hide"
39 * button is pressed.
40 */
41public class ContentLoadingProgressBarActivity extends Activity implements
42        OnClickListener, ViewTreeObserver.OnGlobalLayoutListener {
43
44    private Button mShowButton;
45    private Button mHideButton;
46    private ContentLoadingProgressBar mBar;
47    private long mShowTime;
48    private long mHideTime;
49    private TextView mShowText;
50    private TextView mShowTextDone;
51    private TextView mHideText;
52    private TextView mHideTextDone;
53    private int mLastVisibility;
54    private long mVisibilityChangedTime;
55
56    @Override
57    protected void onCreate(Bundle savedInstanceState) {
58        super.onCreate(savedInstanceState);
59        setContentView(R.layout.content_loading_progressbar);
60
61        mBar = (ContentLoadingProgressBar)findViewById(R.id.progressbar);
62        mShowButton = (Button)findViewById(R.id.show);
63        mShowButton.setOnClickListener(this);
64        mHideButton = (Button)findViewById(R.id.hide);
65        mHideButton.setOnClickListener(this);
66
67        mShowText = (TextView)findViewById(R.id.show_text);
68        mShowTextDone = (TextView)findViewById(R.id.show_text_done);
69        mHideText = (TextView)findViewById(R.id.hide_text);
70        mHideTextDone = (TextView)findViewById(R.id.hide_text_done);
71
72        mLastVisibility = mBar.getVisibility();
73
74        mBar.getViewTreeObserver().addOnGlobalLayoutListener(this);
75    }
76
77    @Override
78    public void onClick(View v) {
79        switch (v.getId()) {
80            case R.id.show:
81                mBar.show();
82                mShowTime = System.currentTimeMillis();
83                mShowText.setText("Show clicked at " + mShowTime);
84                break;
85            case R.id.hide:
86                mBar.hide();
87                mHideTime = System.currentTimeMillis();
88                mHideText.setText("Hide clicked at " + mHideTime);
89                break;
90        }
91    }
92
93    @Override
94    public void onGlobalLayout() {
95        final int visibility = mBar.getVisibility();
96
97        if (mLastVisibility != visibility) {
98            if (visibility == View.VISIBLE) {
99                mVisibilityChangedTime = System.currentTimeMillis();
100                mShowTextDone.setText("Shown at "
101                    + (mVisibilityChangedTime - mShowTime));
102            } else {
103                mHideTextDone.setText("Hidden after "
104                    + (System.currentTimeMillis() - mVisibilityChangedTime));
105            }
106            mLastVisibility = mBar.getVisibility();
107        }
108    }
109}
110