Headertab

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Friday 3 March 2017

Change icon color in android

Hello friends,

I am going to teach you today, how to change colour of image programmatically. So if you want to use a image(icon) with multiple colours then you don't have to take multiple images, now you can change colour by code.


Method 1:

activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#e6eae8"
    android:orientation="vertical">

    <ImageView
        android:layout_weight="1"
        android:id="@+id/imgView2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:src="@drawable/dummy_prodcut"
        />
    <ImageView
        android:layout_weight="1"
        android:id="@+id/imgView1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:src="@mipmap/ic_launcher"
        />

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private ImageView imgIcon1,imgIcon2;
 
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imgIcon1 = (ImageView) findViewById(R.id.imgView1);
        imgIcon2 = (ImageView) findViewById(R.id.imgView2);

        imgIcon1.setColorFilter(getResources().getColor(R.color.darkPurple,null));
        imgIcon2.setColorFilter(getResources().getColor(R.color.colorPrimaryDark,null));

}
}


Method 2:

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private ImageView imgIcon1,imgIcon2;
 
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imgIcon1 = (ImageView) findViewById(R.id.imgView1);
        imgIcon2 = (ImageView) findViewById(R.id.imgView2);

     String strColorCode = "#969600";
        if (strColorCode != null && strColorCode.length() > 0 && strColorCode.length()==7) {
            mColorCode = Color.parseColor(strColorCode);

            //Get the image to be changed from the drawable, drawable-xhdpi, drawable-hdpi,etc folder.
             Drawable sourceDrawable = getResources().getDrawable(R.drawable.ic_big_cart,null);

            //Convert drawable in to bitmap
            Bitmap sourceBitmap = convertDrawableToBitmap(sourceDrawable);
            //Pass the bitmap and color code to change the icon color dynamically.
            mFinalBitmap = changeColor(sourceBitmap, mColorCode);
            imgIcon1.setImageBitmap(mFinalBitmap);

        }else {

            Toast.makeText(MainActivity.this, "Please enter valid color code",Toast.LENGTH_SHORT).show();
        }

}
}

public static Bitmap changeColor(Bitmap bitmap, int color) {
        Bitmap resultBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth() - 1, bitmap.getHeight() - 1);
        Paint p = new Paint();
        ColorFilter filter = new LightingColorFilter(color, 1);
        p.setColorFilter(filter);

        Canvas canvas = new Canvas(resultBitmap);
        canvas.drawBitmap(resultBitmap, 0, 0, p);
        return resultBitmap;
    }


    public static Drawable covertBitmapToDrawable(Context context, Bitmap bitmap) {
        Drawable d = new BitmapDrawable(context.getResources(), bitmap);
        return d;
    }

        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        return bitmap;
    }


Thank you

Live Sample


 





   

No comments:

Post a Comment