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 17 package com.example.android.supportv4.widget; 18 19 import android.app.Activity; 20 import android.os.Bundle; 21 import android.view.View; 22 import android.view.View.OnClickListener; 23 import android.view.ViewTreeObserver; 24 import android.widget.Button; 25 import android.widget.TextView; 26 27 import androidx.core.widget.ContentLoadingProgressBar; 28 29 import com.example.android.supportv4.R; 30 31 /** 32 * Demonstrates how to use the ContentLoadingProgressBar. By default, the 33 * developer should start the ContentLoadingProgressBar with visibility of 34 * "gone" or "invisible". The ContentLoadingProgressBar will be shown after the 35 * default delay for at least a minimum time regardless of when the "hide" 36 * button is pressed. 37 */ 38 public class ContentLoadingProgressBarActivity extends Activity implements 39 OnClickListener, ViewTreeObserver.OnGlobalLayoutListener { 40 41 private Button mShowButton; 42 private Button mHideButton; 43 private ContentLoadingProgressBar mBar; 44 private long mShowTime; 45 private long mHideTime; 46 private TextView mShowText; 47 private TextView mShowTextDone; 48 private TextView mHideText; 49 private TextView mHideTextDone; 50 private int mLastVisibility; 51 private long mVisibilityChangedTime; 52 53 @Override onCreate(Bundle savedInstanceState)54 protected void onCreate(Bundle savedInstanceState) { 55 super.onCreate(savedInstanceState); 56 setContentView(R.layout.content_loading_progressbar); 57 58 mBar = (ContentLoadingProgressBar)findViewById(R.id.progressbar); 59 mShowButton = (Button)findViewById(R.id.show); 60 mShowButton.setOnClickListener(this); 61 mHideButton = (Button)findViewById(R.id.hide); 62 mHideButton.setOnClickListener(this); 63 64 mShowText = (TextView)findViewById(R.id.show_text); 65 mShowTextDone = (TextView)findViewById(R.id.show_text_done); 66 mHideText = (TextView)findViewById(R.id.hide_text); 67 mHideTextDone = (TextView)findViewById(R.id.hide_text_done); 68 69 mLastVisibility = mBar.getVisibility(); 70 71 mBar.getViewTreeObserver().addOnGlobalLayoutListener(this); 72 } 73 74 @Override onClick(View v)75 public void onClick(View v) { 76 switch (v.getId()) { 77 case R.id.show: 78 mBar.show(); 79 mShowTime = System.currentTimeMillis(); 80 mShowText.setText("Show clicked at " + mShowTime); 81 break; 82 case R.id.hide: 83 mBar.hide(); 84 mHideTime = System.currentTimeMillis(); 85 mHideText.setText("Hide clicked at " + mHideTime); 86 break; 87 } 88 } 89 90 @Override onGlobalLayout()91 public void onGlobalLayout() { 92 final int visibility = mBar.getVisibility(); 93 94 if (mLastVisibility != visibility) { 95 if (visibility == View.VISIBLE) { 96 mVisibilityChangedTime = System.currentTimeMillis(); 97 mShowTextDone.setText("Shown at " 98 + (mVisibilityChangedTime - mShowTime)); 99 } else { 100 mHideTextDone.setText("Hidden after " 101 + (System.currentTimeMillis() - mVisibilityChangedTime)); 102 } 103 mLastVisibility = mBar.getVisibility(); 104 } 105 } 106 } 107