Skip to content

Commit

Permalink
Handle drawable vector for Pre Lollipop
Browse files Browse the repository at this point in the history
  • Loading branch information
itchix committed Mar 24, 2017
1 parent 76bd60b commit 8109cb4
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 8 deletions.
@@ -0,0 +1,64 @@
package openfoodfacts.github.scrachx.openfood.utils;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v7.content.res.AppCompatResources;
import android.support.v7.widget.AppCompatEditText;
import android.util.AttributeSet;

import openfoodfacts.github.scrachx.openfood.R;

/**
* Based on <a href="http://stackoverflow.com/questions/35761636/is-it-possible-to-use-vectordrawable-in-buttons-and-textviews-using-androiddraw</a>
*/
public class CustomEditTextView extends AppCompatEditText {

public CustomEditTextView(Context context) {
super(context);
}
public CustomEditTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initAttrs(context, attrs);
}
public CustomEditTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initAttrs(context, attrs);
}

void initAttrs(Context context, AttributeSet attrs) {
if (attrs != null) {
TypedArray attributeArray = context.obtainStyledAttributes(
attrs,
R.styleable.CustomEditTextView);

Drawable drawableLeft = null;
Drawable drawableRight = null;
Drawable drawableBottom = null;
Drawable drawableTop = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
drawableLeft = attributeArray.getDrawable(R.styleable.CustomEditTextView_drawableLeftCompat);
drawableRight = attributeArray.getDrawable(R.styleable.CustomEditTextView_drawableRightCompat);
drawableBottom = attributeArray.getDrawable(R.styleable.CustomEditTextView_drawableBottomCompat);
drawableTop = attributeArray.getDrawable(R.styleable.CustomEditTextView_drawableTopCompat);
} else {
final int drawableLeftId = attributeArray.getResourceId(R.styleable.CustomEditTextView_drawableLeftCompat, -1);
final int drawableRightId = attributeArray.getResourceId(R.styleable.CustomEditTextView_drawableRightCompat, -1);
final int drawableBottomId = attributeArray.getResourceId(R.styleable.CustomEditTextView_drawableBottomCompat, -1);
final int drawableTopId = attributeArray.getResourceId(R.styleable.CustomEditTextView_drawableTopCompat, -1);

if (drawableLeftId != -1)
drawableLeft = AppCompatResources.getDrawable(context, drawableLeftId);
if (drawableRightId != -1)
drawableRight = AppCompatResources.getDrawable(context, drawableRightId);
if (drawableBottomId != -1)
drawableBottom = AppCompatResources.getDrawable(context, drawableBottomId);
if (drawableTopId != -1)
drawableTop = AppCompatResources.getDrawable(context, drawableTopId);
}
setCompoundDrawablesWithIntrinsicBounds(drawableLeft, drawableTop, drawableRight, drawableBottom);
attributeArray.recycle();
}
}
}
Expand Up @@ -7,9 +7,7 @@
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.VectorDrawable;
import android.os.Build;
import android.support.annotation.DrawableRes;
import android.support.graphics.drawable.VectorDrawableCompat;
Expand Down
9 changes: 5 additions & 4 deletions app/src/main/res/layout/activity_login.xml
Expand Up @@ -2,6 +2,7 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">

<include layout="@layout/toolbar" />
Expand All @@ -19,14 +20,14 @@
android:layout_width="match_parent"
android:layout_height="wrap_content">

<EditText
<openfoodfacts.github.scrachx.openfood.utils.CustomEditTextView
android:id="@+id/editTextLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="@dimen/spacing_normal"
android:layout_marginRight="@dimen/spacing_normal"
android:drawableLeft="@drawable/ic_person_grey_24dp"
app:drawableLeftCompat="@drawable/ic_person_grey_24dp"
android:drawablePadding="10dip"
android:ems="10"
android:hint="@string/hintLogin"
Expand All @@ -40,14 +41,14 @@
android:layout_width="match_parent"
android:layout_height="wrap_content">

<EditText
<openfoodfacts.github.scrachx.openfood.utils.CustomEditTextView
android:id="@+id/editTextPass"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="@dimen/spacing_normal"
android:layout_marginRight="@dimen/spacing_normal"
android:drawableLeft="@drawable/ic_vpn_key_grey_24dp"
app:drawableLeftCompat="@drawable/ic_vpn_key_grey_24dp"
android:drawablePadding="10dip"
android:ems="10"
android:hint="@string/hintPass"
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/layout/fragment_find_product.xml
Expand Up @@ -14,15 +14,15 @@
android:layout_marginStart="@dimen/spacing_normal"
app:srcCompat="@drawable/img_scan" />

<EditText
<openfoodfacts.github.scrachx.openfood.utils.CustomEditTextView
android:id="@+id/editTextBarcode"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_marginLeft="@dimen/spacing_normal"
android:layout_marginRight="@dimen/spacing_normal"
android:layout_marginTop="@dimen/spacing_normal"
android:background="@drawable/edittext_full"
android:drawableLeft="@drawable/ic_search_black"
app:drawableLeftCompat="@drawable/ic_search_black"
android:drawablePadding="10dip"
android:ems="10"
android:hint="@string/hintBarcode"
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/res/values/attrs.xml
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>

<resources>
<declare-styleable name="CustomEditTextView">
<attr name="drawableLeftCompat" format="reference"/>
<attr name="drawableRightCompat" format="reference"/>
<attr name="drawableTopCompat" format="reference"/>
<attr name="drawableBottomCompat" format="reference"/>
</declare-styleable>
</resources>

0 comments on commit 8109cb4

Please sign in to comment.