Get the ID of a drawable in ImageView

I think if I understand correctly this is what you are doing. ImageView view = (ImageView) findViewById(R.id.someImage); view.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { ImageView imageView = (ImageView) view; assert(R.id.someImage == imageView.getId()); switch(getDrawableId(imageView)) { case R.drawable.foo: imageView.setDrawableResource(R.drawable.bar); break; case R.drawable.bar: default: imageView.setDrawableResource(R.drawable.foo); break; } }); Right? So that function getDrawableId() doesn’t exist. You … Read more

Scale image keeping its aspect ratio in background drawable

It is impossible to achieve manipulating background attribute within xml-files only. There are two options: You cut/scale the bitmap programmatically with Bitmap.createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter) and set it as some View‘s background. You use ImageView instead of background placing it as the first layout’s element and specify android:scaleType attribute for it: … Read more

What is the difference between Bitmap and Drawable in Android?

A Bitmap is a representation of a bitmap image (something like java.awt.Image). A Drawable is an abstraction of “something that can be drawn”. It could be a Bitmap (wrapped up as a BitmapDrawable), but it could also be a solid color, a collection of other Drawable objects, or any number of other structures. Most of … Read more

Different resolution support android

App launcher icon size in pixels for different resolution Mobile Resolution mipmap-mdpi (48X48) mipmap-hdpi (72X72) mipmap-xhdpi (96X96) mipmap-xxhdpi (144X144) mipmap-xxxhdpi (192X192) Tablet Layouts: Use following folders if you wish to have tablet-specific layouts: layout-large-mdpi (1024×600) layout-large-tvdpi (800×1280) layout-large-xhdpi (1200×1920) layout-xlarge-mdpi (1280×800) layout-xlarge-xhdpi (2560×1600) Drawables folders: Mobile res/drawable (default) res/drawable-ldpi/ (240×320 and nearer resolution) res/drawable-mdpi/ (320×480 … Read more

Adding bottom margin to ListView last element

In your ListView, set a paddingBottom and clipToPadding=”false”. <ListView android:layout_width=”match_parent” android:layout_height=”match_parent” android:paddingBottom=”8dp” android:clipToPadding=”false” android:scrollbarStyle=”outsideOverlay”/> This also works for RecyclerView. Only use android:scrollbarStyle=”outsideOverlay” if you want the scroll bar to not overflow into the padded area.

Android: Alternative for context.getDrawable()

The previously accepted method has been deprecated, according to the SDK 22 documentation: Prior to android.os.Build.VERSION_CODES#JELLY_BEAN, this function would not correctly retrieve the final configuration density when the resource ID passed here is an alias to another Drawable resource. This means that if the density configuration of the alias resource is different than the actual … Read more

Create a new color drawable

Since you’re talking about hex you have to start with 0x and don’t forget the opacity. So basically: 0xFFFF6666 ColorDrawable cd = new ColorDrawable(0xFFFF6666); You can also create a new colors.xml file into /res and define the colors like: <?xml version=”1.0″ encoding=”utf-8″?> <resources> <color name=”mycolor”>#FF6666</color> </resources> and simply get the color defined in R.color.mycolor getResources().getColor(R.color.mycolor)

Create a Bitmap/Drawable from file path

Create bitmap from file path: File sd = Environment.getExternalStorageDirectory(); File image = new File(sd+filePath, imageName); BitmapFactory.Options bmOptions = new BitmapFactory.Options(); Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions); bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true); imageView.setImageBitmap(bitmap); If you want to scale the bitmap to the parent’s height and width then use Bitmap.createScaledBitmap function. I think you are giving the wrong file path.

Using android vector Drawables on pre Lollipop crash

LATEST UPDATE – Jun/2019 Support Library has changed a bit since the original answer. Now, even the Android plugin for Gradle is able to automatically generate the PNG at build time. So, below are two new approaches that should work these days. You can find more info here: PNG Generation Gradle can automatically create PNG … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)