blob: 126a33d67685d713eac36384f9d6d2c079a84ef7 [file] [log] [blame]
// Copyright 2018 The Feed Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.android.libraries.feed.sharedstream.ui;
import android.content.Context;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.view.View;
import android.support.v4.widget.CircularProgressDrawable;
/** View compatible with KitKat that shows a Material themed spinner. */
public class MaterialSpinnerView extends AppCompatImageView {
private final CircularProgressDrawable spinner;
public MaterialSpinnerView(Context context) {
this(context, null);
}
public MaterialSpinnerView(Context context, /*@Nullable*/ AttributeSet attrs) {
this(context, attrs, 0);
}
// In android everything is initialized properly after the constructor call, so it is fine to do
// this work after.
@SuppressWarnings({"nullness:argument.type.incompatible", "nullness:method.invocation.invalid"})
public MaterialSpinnerView(Context context, /*@Nullable*/ AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
spinner = new CircularProgressDrawable(context);
spinner.setStyle(CircularProgressDrawable.DEFAULT);
setImageDrawable(spinner);
spinner.setColorSchemeColors(context.getResources().getColor(R.color.google_blue_500));
if (getVisibility() == View.VISIBLE) {
spinner.start();
}
}
@Override
public void setVisibility(int visibility) {
super.setVisibility(visibility);
if (spinner.isRunning() && getVisibility() != View.VISIBLE) {
spinner.stop();
} else if (!spinner.isRunning() && getVisibility() == View.VISIBLE) {
spinner.start();
}
}
}