Android cardview set background drawable programmatically

Apps often need to display data in similarly styled containers.

Set the background color of a view programmatically

These containers are often used in lists to hold each item's information. The system provides the CardView API as an easy way for you show information inside cards that have a consistent look across the platform.

These cards have a default elevation above their containing view group, so the system draws shadows below them. Cards provide an easy way to contain a group of views while providing a consistent style for the container.

The CardView widget is part of the v7 Support Libraries. To use it in your project, add the following dependency to your app module's build. In order to use the CardView you need to add it to your layout file.

Use it as a view group to contain other views. In this example, the CardView contains a single TextView to display some information to the user. The cards are drawn to the screen with a default elevation, which causes the system to draw a shadow underneath them. This will draw a more pronounced shadow with a larger elevation, and a lower elevation will result in a lighter shadow. CardView uses real elevation and dynamic shadows on Android 5.

Android ImageView Example by XML and Programmatically

Use these properties to customize the appearance of the CardView widget:. Content and code samples on this page are subject to the licenses described in the Content License. App Basics. Build your first app. App resources. Resource types. App manifest file.

App permissions. Device compatibility. Multiple APK support. Adding wearable features to notifications. Creating wearable apps. Creating custom UIs. Sending and syncing data.

Adminlte sidebar toggle

Creating data providers. Creating watch faces. Android TV. Build TV Apps.When you need to display static images in your app, you can use the Drawable class and its subclasses to draw shapes and images. A Drawable is a general abstraction for something that can be drawn. The various subclasses help with specific image scenarios, and you can extend them to define your own drawable objects that behave in unique ways.

There are two ways to define and instantiate a Drawable besides using the class constructors:. Note: You might instead prefer using a vector drawable, which defines an image with a set of points, lines, and curves, along with associated color information. This allows vector drawables to be scaled for different sizes without a loss of quality.

For more information, see Vector drawables overview. You can add graphics to your app by referencing an image file from your project resources. App icons, logos, and other graphics, such as those used in games, are well suited for this technique. Once in your project, you can reference the image resource from your code or your XML layout.

Either way, it's referred to using a resource ID, which is the file name without the file type extension. For example, a true-color PNG that doesn't require more than colors may be converted to an 8-bit PNG with a color palette.

This results in an image of equal quality but which requires less memory. As a result, the image binaries placed in this directory can change at build time. The following code snippet demonstrates how to build an ImageView that uses an image created from a drawable resource and adds it to the layout:.

How to Implement Horizontal Recyclerview in Android Studio - HorizontalRecyclerview - Android Coding

In other cases, you may want to handle your image resource as a Drawable object, as shown in the following example:. Note: Each unique resource in your project can maintain only one state, no matter how many different objects you instantiate for it. For example, if you instantiate two Drawable objects from the same image resource and change a property such as the alpha for one object, then it also affects the other.

When dealing with multiple instances of an image resource, instead of directly transforming the Drawable object you should perform a tween animation. For more information about using project resources, see Resources and assets. Note: When using image resources as the source of your drawables, be sure the images are the appropriate size for various pixel densities. If the images are not correct they will be scaled up to fit, which can cause artifacting in your drawables.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service.

The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. I have a card and want to add content to it. How should I add images and also Text to the card? Here is my xml code:. The card1. So can anybody help me add an image? And Add Background for that Layout.

android cardview set background drawable programmatically

This is one of the easy way to set BackgroundImage for CardView. Add RelativeLayout after Cardview declarations such that you can able to move elements within the cards. Learn more. How to set a background image for a card in CardView? Ask Question. Asked 2 years, 5 months ago. Active 9 months ago.

Iphone 10 s

Viewed 22k times. ColorInt; import android. Nullable; import android.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

If I use the iconBg. Since you are just using the shape to create a layout with rounded corners and a border, the first option is to wrap your LinearLayout inside a CardView and then apply to the card the corner radius, the stroke and the background color. Learn more.

android cardview set background drawable programmatically

Programatically change background of Drawable, maintaining the corner radius Ask Question. Asked 4 days ago. Active yesterday. Viewed 49 times. Gabriele Mariotti k 51 51 gold badges silver badges bronze badges. Abd Maj Abd Maj 69 9 9 bronze badges. You can't add something to the resource file programmatically. You can wrap your ImageView inside a CardView and give a radius and color to that.

Active Oldest Votes. Gabriele Mariotti Gabriele Mariotti k 51 51 gold badges silver badges bronze badges. Dilshod Dilshod 1 1 silver badge 7 7 bronze badges. Appreciate the response but there should be an easier way than this or else I will need to create multiple files in order to give each image a different color background.

First, You set background, which is uses shape resources with radius corners. Then, you are replacing the background to new new layer color.

It will lost rounded corner. You have another choice, use CardView. What is R. AbdMaj updated the answer. Sign up or log in Sign up using Google. Sign up using Facebook.

Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Programming tutorials can be a real drag. Socializing with co-workers while social distancing.Maintains and draws a drawable inside rounded rectangular bounds.

Subscribe to RSS

The drawable set by the setDrawable Drawable method will be drawn within the rounded bounds specified by setBounds Rect and setRadius int when the draw Canvas method is called. By default, RoundedDrawable will apply padding to the drawable inside to fit the drawable into the rounded rectangle.

If clipping is enabled by the setClipEnabled boolean method, it will clip the drawable to a rounded rectangle instead of resizing it.

The setRadius int method is used to specify the amount of border radius applied to the corners of inner drawable, regardless of whether or not the clipping is enabled, border radius will be applied to prevent overflowing of the drawable from specified rounded rectangular area. To inflate from XML, use getDrawable int method.

Sets whether the drawable inside should be clipped or resized to fit the rounded bounds.

Ark atlas wiki

If the drawable is animated, don't set clipping to true as clipping on animated drawables is not supported. Content and code samples on this page are subject to the licenses described in the Content License. The second Android 11 Developer Preview is now available, test it out and share your feedback.

Android Developers. Mode arg0 boolean setVisible boolean arg0, boolean arg1 void unscheduleSelf Runnable arg0. RoundedDrawable added in version Interfaces CircularProgressLayout. LayoutCallback WearableRecyclerView.

Drawables overview

Theme theme. From class android. Drawable void. Theme arg0. Options arg4. Theme arg2. Theme arg3. Callback arg0. Mode arg1. Mode arg0. From class java. Object Object. Drawable that will be rendered.Thanks for this.

But what if I have image loaders such as picasso and UIL in an adapter and I have to make image round at its corner?

android cardview set background drawable programmatically

I also need a way to use this with PIcasso within an adapter. Any idea on how to achieve it? Still zooms the image to the actual size. This method sets the image as a background, and it looks like there is currently no way to centerCrop a background. Is this method supposed to also work to get rounded corners for the enclosed image? Hi, this is just what I need, but I get out, because not wear a drawable, but is an image database, a string:.

Skip to content. Instantly share code, notes, and snippets. Code Revisions 2 Stars 64 Forks Embed What would you like to do?

Embed Embed this gist in your website. Share Copy sharable link for this gist. Learn more about clone URLs. Download ZIP. This comment has been minimized. Sign in to view. Copy link Quote reply.

Watch e

Thank you : That works fine for me. Thanks again. Hi, this is just what I need, but I get out, because not wear a drawable, but is an image database, a string: sport.

Thank you. Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. CLAMP .This attribute works fine. I have got the same problem on Xamarin.

Android - VS The Solution that worked for me:. CardView element add this propriety:. You can also add cardElevation and cardElevation. If you want to edit the cardview programmatically you just need to use this code: For C. And now you can change background color programmatically without lost border, corner radius and elevation. I got this simple solution working, not sure if it's the best solution, but it worked for me. It is very simple in kotlin.

Use ColorStateList to change card view colour. At the same time there isn't a method to change the color dynamically. I've just tried solutions like: mCardView. I finally got the corners to stay. This is cXamarin. SetCardBackgroundColor Color. ParseColor " 4dd0e1". Red ; And now you can change background color programmatically without lost border, corner radius and elevation.

I was having a similar issue with formatting CardViews in a recylerView. WHITE. You can use this in java. Continue Reading.