Skip to content

Instantly share code, notes, and snippets.

@AndrewReitz
Created February 7, 2017 16:05
Show Gist options
  • Save AndrewReitz/872e75ee900f6fcb093e9f5d32bf9097 to your computer and use it in GitHub Desktop.
Save AndrewReitz/872e75ee900f6fcb093e9f5d32bf9097 to your computer and use it in GitHub Desktop.
DrawableStartButton, and Icon Drawable for Android
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="drawableStartButton" format="reference" />
<declare-styleable name="DrawableStartButton">
<attr name="drawableStart" />
<attr name="text" />
</declare-styleable>
</resources>
/** A button that allows you to add a drawable to the start of the text. */
public class DrawableStartButton extends FrameLayout {
/** The textview that sits on top of the framelayout that contains the image and the text. */
@BindView(R.id.drawable_start_textview) TextView textview;
public DrawableStartButton(Context context, AttributeSet attrs) {
// Trick the framelayout to look like a button
this(context, attrs, R.attr.drawableStartButton);
}
public DrawableStartButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
inflate(context, R.layout.view_drawable_start_button, this);
ButterKnife.bind(this);
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs, R.styleable.DrawableStartButton,
defStyleAttr, R.style.Theme_Button_Secondary);
Drawable drawable = a.getDrawable(R.styleable.DrawableStartButton_drawableStart);
CharSequence text = a.getText(R.styleable.DrawableStartButton_text);
textview.setText(text);
textview.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
a.recycle();
}
}
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
>
<TextView
android:id="@+id/drawable_start_textview"
style="@style/Theme.Button.Secondary"
android:theme="@style/Theme.Button.Secondary"
android:clickable="false"
android:background="@null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="10dp"
android:layout_gravity="center"
tools:text="Testing"
/>
</merge>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment