Skip to content

Instantly share code, notes, and snippets.

@laaptu
Last active June 14, 2018 07:56
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laaptu/ee2d30c46e14743172cb to your computer and use it in GitHub Desktop.
Save laaptu/ee2d30c46e14743172cb to your computer and use it in GitHub Desktop.
Using Styles values as custom attributes
<declare-styleable name="ButtonProgress">
<attr name="btnProgressStyle" format="reference|integer" />
</declare-styleable>
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/progress_circular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<TextView
android:id="@+id/txt_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"/>
</merge>
package com.zala.widgets;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.zala.R;
import com.zala.utils.ResourceUtils;
import com.zala.widgets.base.InflatableView;
import butterknife.InjectView;
/**
* Created by laaptu on 6/9/15.
*/
public class ButtonProgress extends InflatableView {
@InjectView(R.id.progress_circular)
ProgressBar progressBar;
@InjectView(R.id.txt_btn)
TextView txtBtn;
private static int DEFAULT_TEXT_SIZE;
private static int DEFAULT_TEXT_COLOR;
private static int DEFAULT_BACKGROUND_BG = R.drawable.white_bg;
private static int DEFAULT_GRAVITY = Gravity.LEFT;
private static final int[] ATTRS = new int[]{android.R.attr.textSize,
android.R.attr.textColor,
android.R.attr.gravity, android.R.attr.background, android.R.attr.text};
/**
* Keep in mind these values must be in ascending order i.e from smaller to greater
* If it is not in ascending order ,your values declared won't be guaranteed to come<br/>
* So what you can do is just put attributes out there and then do Arrays.sort(ATTRS)
* and see how value comes, and you must put values in that order
* http://stackoverflow.com/questions/19034597/get-multiple-style-attributes-with-obtainstyledattributes?lq=1*/
/**
* This is similar to
* <declare-styleable name="ButtonProgress">
* <attr name="android:textSize" />
* <attr name="android:textColor" />
* <attr name="android:background" />
* </declare-styleable>
* <p/>
* which creates an array with following attributes
*/
@Override
public int getLayoutId() {
return R.layout.button_progress;
}
public ButtonProgress(Context context) {
super(context);
}
public ButtonProgress(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ButtonProgress(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void init(Context context, AttributeSet attrs) {
super.init(context, attrs);
DEFAULT_TEXT_SIZE = ResourceUtils.getDimension(R.dimen.button_text_size_progress);
DEFAULT_TEXT_COLOR = ResourceUtils.getColor(R.color.zala_black);
if (attrs != null) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ButtonProgress);
int styleId = typedArray.getResourceId(R.styleable.ButtonProgress_btnProgressStyle, -1);
typedArray.recycle();
if (styleId > 0) {
typedArray = context.getTheme().obtainStyledAttributes(styleId, ATTRS);
txtBtn.setTextSize(typedArray.getDimensionPixelSize(0, DEFAULT_TEXT_SIZE));
txtBtn.setTextColor(typedArray.getColor(1, DEFAULT_TEXT_COLOR));
txtBtn.setGravity(typedArray.getInt(2, DEFAULT_GRAVITY));
txtBtn.setBackgroundResource(typedArray.getResourceId(3, DEFAULT_BACKGROUND_BG));
txtBtn.setText(typedArray.getString(4));
}
}
}
}
<com.zala.widgets.ButtonProgress
android:layout_width="match_parent"
android:layout_height="48dp"
zala:btnProgressStyle="@style/WhiteButtonStyle" />
<style name="WhiteButtonStyle">
<item name="android:textColor">@color/purple</item>
<item name="android:textSize">@dimen/button_text_size_progress</item>
<item name="android:gravity">center</item>
<item name="android:text">@string/go</item>
<item name="android:background">@drawable/white_bg</item>
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment