Skip to content

Instantly share code, notes, and snippets.

@ozodrukh

ozodrukh/ActionView.java

Last active Jul 8, 2016
Embed
What would you like to do?
Simple MenuItemView with progress bar
package io.codetail.trade.activities.preview;
import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v4.widget.TextViewCompat;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import io.codetail.trade.R;
import io.codetail.trade.utils.AndroidUtils;
public class ActionView extends LinearLayout {
private final ProgressBar progressBar;
private final ImageView imageView;
private final TextView textView;
public ActionView(Context context) {
this(context, null);
}
public ActionView(Context context, AttributeSet attrs) {
super(context, attrs);
if (isInEditMode()) {
AndroidUtils.setDensity(context.getResources().getDisplayMetrics().density);
}
/** Set selectable background */
TypedArray theme = context.getTheme()
.obtainStyledAttributes(new int[] { android.R.attr.selectableItemBackground });
if (theme.hasValue(0)) {
setBackgroundResource(theme.getResourceId(0, -1));
}
theme.recycle();
AndroidUtils.setPadding(this, AndroidUtils.dp(12), 0);
setGravity(Gravity.CENTER);
setOrientation(VERTICAL);
inflate(context, R.layout.custom_action_view_base, this);
imageView = (ImageView) getChildAt(0);
textView = (TextView) getChildAt(1);
progressBar = (ProgressBar) getChildAt(2);
progressBar.setVisibility(View.GONE);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ActionView);
if (a.hasValue(R.styleable.ActionView_textAppearance)) {
TextViewCompat.setTextAppearance(textView, a.getResourceId(0, -1));
}
if (a.hasValue(R.styleable.ActionView_src)) {
imageView.setImageResource(a.getResourceId(R.styleable.ActionView_src, -1));
imageView.setImageDrawable(DrawableCompat.wrap(imageView.getDrawable()));
if (a.hasValue(R.styleable.ActionView_iconTint)) {
DrawableCompat.setTint(imageView.getDrawable(),
a.getColor(R.styleable.ActionView_iconTint, -1));
}
}
textView.setText(a.getText(R.styleable.ActionView_text));
a.recycle();
}
public void showProgressBar() {
setEnabled(false);
progressBar.setVisibility(View.VISIBLE);
imageView.setVisibility(View.GONE);
textView.setVisibility(View.GONE);
}
public void hideProgressBar() {
setEnabled(true);
progressBar.setVisibility(View.GONE);
textView.setVisibility(View.VISIBLE);
imageView.setVisibility(View.VISIBLE);
}
}
<declare-styleable name="ActionView">
<attr format="reference" name="textAppearance"/>
<attr format="color" name="iconTint"/>
<attr format="reference" name="src"/>
<attr format="string" name="text"/>
</declare-styleable>
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
style="@style/TextAppearance.ActionText"
/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Widget.AppCompat.ProgressBar"
/>
</merge>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment