Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle drawable vector for Pre Lollipop
- Loading branch information
Showing
5 changed files
with
81 additions
and
8 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
app/src/main/java/openfoodfacts/github/scrachx/openfood/utils/CustomEditTextView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |