Share Coding

Tutorials, Problems, Stuffs …

Change background color of Android ImageButton / Custom background

Change background color for a custom background.

1.jpg

drawable/circle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <gradient
        android:angle="270"
        android:endColor="@color/colorAccent"
        android:startColor="@color/colorAccent" />
</shape>

layout/main.xml, use the drawable as background

<ImageView
    android:id="@+id/imgKey"
    android:layout_width="28dp"
    android:layout_height="28dp"
    android:background="@drawable/circle"
    android:padding="4dp"
    android:scaleType="fitCenter"
    android:src="@drawable/main1a_icon_key" />

In MainActivity.java,

ImageView imgKey = (ImageView) actionbarView.findViewById(R.id.imgKey);
int mainColor = Color.parseColor(myApp.property.mainColor);
GradientDrawable bgShape = (GradientDrawable) imgKey.getBackground();
bgShape.setColor(mainColor);
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: