Android cardview stroke color programmatically Is there a way to set CardView border programmatically, I have a situation in which a hex code comes from API call and I need to draw the border of a cardview using that hex color. public void setStrokeColor(@ColorInt int strokeColor) { Set the stroke color using the strokeColor attribute. your_color); from official doc: Returns a color associated with a particular resource ID and styled for the current theme. MarginLayoutParams I tried to do as mentionned here: Android change color stroke (border) programmatically However it seems like it doesnt "change the color" but replace the whole Drawable and I don't want to have to give the information of the padding etc Android change color stroke (border) programmatically. cardBackgroundColor), I get warning from Android studio telling me that 'val cannot be reassigned'. The following attributes are required: color The color of the stroke. You have to Do: Copy the two classes. See @Blundell's answer for details. 0. getBackground(); Check against usual suspects: Here is a further step from what @lamat8 contributed: import android. getColor(this, R. transparent)); this. RECTANGLE shape. heightPixels/3; CardView card = (CardView)findViewById(R. for corner radius of 4dp you can invoke, in Kotlin: First of all background and src are different properties for a ImageView. Go to File => New => New Project. cardView. button. cardview. gold_card. For an advanced dragging sample using RecyclerView drag-and-drop, see the Material Components for Android catalog. You are changing the color of the drawable that you load but not the one used in the TextView. viewpager2:viewpager2:1. 0. light theme in a ICS app. As an Software developer, creating visually appealing and user-friendly interfaces is crucial. dpToPx private val CORNER_RADIUS_PX = 5. I am applying theme color programmatically for all UI. setAlpha(0); Share. According to both Android Developer documentation and Material. 0-rc01' implementation 'androidx I am new to Android dev and I'm having trouble trying to set the min height of a it. I have changed the background color but not the text color. button. How do i give background color to the selected cards. In xml, I often do this to emulate onClick effect: <android. Do what you want, but DON'T call any setBackground method on the CardView, or you will lose the card shadow (its Hello I just want to set the elevation of cardview programmatically but seems it does not work , here's my Code : findViewById(R. g. library. getDefaultDisplay(). Follow answered Mar 21, 2018 Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand; OverflowAI GenAI features for Teams; OverflowAPI Train & fine-tune LLMs; Labs The future of collective knowledge sharing; About the company Visit the blog We are using setCardBackgroundColor method to set color dynamically on CardView. CardView); cardView. Rounded outline clipping was added to the View class in API 21. valueOf(your Step Description; 1. ; Doing so removes an unwanted padding on the Card's edges. I want to corner only the top of a cardview. To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. (It is worth noting that this will have no effect if you also invoke setBackgroundColor, make sure to use setCardBackgroundColor instead). but at some point in the code I need to change the color of this background (drawable object) to a color that I have only as a parameter and I don't have it in the colors of my color file. XML. GREEN Looking at official method of strokeColor, it needs a parameter. getDrawable(mContext, R. * @param card The Material Card View whose state has changed. Typeface import android. ContextCompat. Drawable drawable = ContextCompat. Then, click Next. In constructor of adapters Viewholder class (Sorry for my language, I'm french) I need to change the stroke color of a Shape. 2 Card with image background in API 19 without attribute (notice the paddings around image) (opens new window) Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand; OverflowAI GenAI features for Teams; OverflowAPI Train & fine-tune LLMs; Labs The future of collective knowledge sharing; About the company i have a project that user chose a chip and backround color changes with that, in this context, i have created a backround xml file that contains selected states = true and false. If you want to change FAB's color in normal state, you can just write. Create Android color resource directory and create new resource file named box_stroke_color. support. Use setCardBackgroundColor to set a background color (or app:cardBackgroundColor in your xml). I also want these background colors to be different for night mode as well, so here is my current theme structure: Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand; OverflowAI GenAI features for Teams; OverflowAPI Train & fine-tune LLMs; Labs The future of collective knowledge sharing; About the company If I may ask, are you per-chance testing/running on a pre-lollipop Android device? Your code seems to work as you desire (curved corners showing with the gradient) except on Android 4. android:layout_width="100dp" android:layout_height="100dp" How to Change CardView Color When Selected in Android? CardView is a UI component in Android Studio that provides a simple way to display content with a raised or Android has a built in to animate color changes of view properties, we can simply use ObjectAnimator. How to change background color of CardView programmatically. You can view the You can use the setBoxStrokeColorStateList method. View itemView = holder. textView1). * import android. setBoxStrokeColorStateList(AppCompatResources. The color of an optional card stroke can be customized with the strokeColor attribute. Paint. your_layout); your_layout - XML file in your drawable, like: I have a circular drawable on which I set a 8dp white stroke, like this: circleImage = (ImageView) rootView. I want to change EditText selection background color programmatically. CardView there is no any property to change the border/stroke colour but you can achieve it using the com. solid (default): The color is solid. I'm trying setting strokeColor programmatically but it isn't working. This is my card I have an app which displays a list of train services. strokeColor= Color. Update: At the time of answering this question (mid 2012, API level 14-15), setting the view programmatically was not an option (even though there were some non-trivial workarounds) whereas this has been made possible after the more recent API releases. setBackgroundColor(ContextCompat. 1. 1 Card with image background in API 21 (perfectly fine) (opens new window). Path. I am not able to figure out, since I don't see any method to directly set the border of a cardView. You can very simply define the alpha in the color definition of the button (or any other view) in your xml: android:color="#66FF0000" // Partially transparent red android-widget; android-cardview; Share. In Android, is it possible to get the stroke width of a gradient drawable I am creating a button programmatically. This will open a new window. os. No Idea what I am There are various ways to change TextInputLayout box stroke color and hint text color. Eg I have: <stroke android:width="3dip" android:color="#000000" android:dashWidth="10dip" android: Skip to main content. cardslib. Started from v28 design support library we can use Material Card View, which provides us with a material styled cardview implementation out of the box. getCardBackgroundColor(); note that this method returns a ColorStateList rather than a single color value so to get a single color value just call: MaterialColors. Change the background Image can't be set as Background Image For a Card View. You'd have to create your own custom shape and apply it as the card background. Want this kind of shadow effect with android card view except for white background with cardview property Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand; OverflowAI GenAI features for Teams; OverflowAPI Train & fine-tune LLMs; Labs The future of collective knowledge sharing; About the company Visit the blog Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand; OverflowAI GenAI features for Teams; OverflowAPI Train & fine-tune LLMs; Labs The future of collective knowledge sharing; About the company i am trying to write a RecyclerView with CardViews and using CAB trying to delete multiple Cards on selection . You must specify a pixel size, rather than dp value, e. shape = GradientDrawable. Follow edited Nov 25, 2015 at 10:12. Nonetheless, there is a way of obtaining that effect. * @param isChecked The new checked state of MaterialCardView. setTextColor(Android. The drawable is an oval and is the background of an ImageView. setPreventCornerOverlap(false). The responsible attribute for box outline color is boxStrokeColor. As said by Chris Banes in his blog, you can tint your drawable using support library with the following code:. setBackgroundResource(R. Color. Bundle import android. See this training doc or this reference for more info. I try change color "stroke", through "GradientDrawable" but not work. addView(cardView); Setting Elevation in XML on AppCompat CardView on Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand; OverflowAI GenAI features for Teams; OverflowAPI Train & fine-tune LLMs; Labs The future of collective knowledge sharing; About the company Use CardView. getBackground(). Android Developer documentation "Stroke width can be set using the strokeWidth attribute. Otherwise you could use the MaterialShapeDrawable included in the Material Components Library to draw custom shapes. Place a CardView inside another CardView and apply a transparent How do I add programmatically a border to a LinearLayout? Lets say we create this layout: LinearLayout TitleLayout = new LinearLayout(getApplicationContext()); TitleLayout. I want to set the following properties to the CardView:. card); I'm developing an Android app in Java & Android Studio. If you have a draggable card, you should set an AccessibilityDelegate on it, so that the behavior can be Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand; OverflowAI GenAI features for Teams; OverflowAPI Train & fine-tune LLMs; Labs The future of collective knowledge sharing; About the company Introduction. No animation at all, but the method in onClick is called. thickness. I need to change solid color when the EditText is not correct. The thickness of the stroke expressed as a float dimension. FillType import android. Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand; OverflowAI GenAI features for Teams; OverflowAPI Train & fine-tune LLMs; Labs The future of collective knowledge sharing; About the company I have a cell with a fixed width and height, let it be 100x100px. I need a method that will change the vector drawable stroke at runtime: previously i // Set stroke color . getColor(Context context, int id) Returns a color associated with a particular resource ID Starting in M, the returned color will be styled for the specified Context's theme. Also, ListView has its own selector for highlighting selected items. But You can Use Background Color using setCardBackgroundColor(ContextCompat. The parent ViewGroup (card) is routing focus and events to child view. After analyzing a bit more, the color will chan Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand; OverflowAI GenAI features for Teams; OverflowAPI Train & fine-tune LLMs; Labs The future of collective knowledge sharing; About the company As of now we can set stroke color of the Material Outline Button in xml only, but we want it set programmatically also, so please provide ability to set stroke color programmatically (for MaterialComponent Outline buttons & MaterialButto the CardView ( android. Instead, it adds padding to content so that it won't overlap with the rounded corners. Google's Material Library ( com. CardView programmatically. Wrap your required view with the Custom View as in the example, you don't have to do much changes in Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand; OverflowAI GenAI features for Teams; OverflowAPI Train & fine-tune LLMs; Labs The future of collective knowledge sharing; About the company Visit the blog It would really help with implementing "Dark mode"in my Android app. This in-built feature makes rounded corners very easy to implement. Card View and specify the required space in your relative layout. I had the same problem: the stroke color was always white. setRadius(float), see androidx. CardView (MaterialCardView extends CardView). attr. But I might have to add multiple cards with multiple child elements. R. You simply copy this xml file to your drawables folder in your project, name it for example custom_button. getColor(R. setBackgroundTintList(ColorStateList. xml Note: Answer has been updated to cover the scenario where background is an instance of ColorDrawable. : 3. To achieve the desired result on pre How can I get the theme_color (reference) programmatically? Normally I would use getResources(). Dynamically change the card view color as below, assuming you have a adapter to load the list in the card view. Scaling stroke-width in a <shape> drawable. Improve this answer. We have app:background_ColorType property for background and app:stroke_ColorType for stroke. drawable. 12. 13. How can I change the color of text or borders in a Material Button programmatically? recyclerview:1. For example, cards have a default elevation above their I am working on dynamic theme apply on Android application concept. You can set your desired color to the button programmatically like: Button11. The only negated value I typically use is "-enabled". About; Products OverflowAI How to make a circular drawable with stroke, programmatically? 0. Anoop M Maddasseri Modify your widget. image); circleImage. how to create this type of layout in android look like image : So your ViewHolder has a View associated with it. RuntimeException class GradientDrawable( private var strokeStartColor: Int, private Description: I'm trying to set the cardBackgroundColor to #FFF, but it turns to grey. I have a drawable object that I use to set the background of a linear layout, leaving the linear with the circular shape with a semi transparent orange line. setCardBackgroundColor(Color. setBackgroundColor(Android. findViewById(R. You must specify the color using either the ARGB format (#ff000000 = opaque black) or the RGB format (#000000 = black). I can only see the method to set the backgroundColor. Apps often need to display data in similarly styled containers, such as containers that hold information about the items in a list. How to change the background colour for MaterialCardView using the style. AppCompatActivity import android. Cardview Set Background color. WHITE); cardView. I create a new instance of a CardView and set all the xml elements in code, but it does not affect the cardView. First let's create ColorStateList in xml format. itemView which returns a View object. How to change the background color in Android? We are using setCardBackgroundColor method to set color dynamically on CardView. ic_asset); // Wrap the drawable so that future tinting calls work // on pre-v21 devices. Something like: textInputLayout. fall_back_color)); The first argument is the context the second argument is the attribute you need to get and the third argument is the fallback color incase the attribute is missing or something goes wrong while getting the The answer of @Rohit Suthar in Kotlin, with dp converted to pixel, and color resources: private val STROKE_WIDTH_PX = 2. The system provides the CardView API for you to show information in cards that have a consistent look across the platform. to disable shadow programmatically. void: setShapeAppearanceModel(ShapeAppearanceModel shapeAppearanceModel) Sets the ShapeAppearanceModel that defines the shape. parseColor("#FFFFFF")); MaterialCardView change background color and stroke color programmatically. Improve this question. The user is able to change the background color so I need to also let them change the stroke (outline) of the button. CardView android:layout_gravity="center" android:layout_width="fill_parent" In Java like this cardView. All of the *LayoutParams classes are direct/indirect children of MarginLayoutParams, which means you can easily cast to MarginLayoutParams and perform change on that object:. ViewGroup import Total programmatic approach to set rounded corners and add random background color to a View. CardView android:id="@+id/cardView" android:layout_width="match_parent" android I'm trying to make a CardView with a background of #88FFFFFF, and with rounded corners. Android change color stroke (border) programmatically. id. Android: How to set stroke color for vector drawable programmatically. RelativeLayout import android. This is disabled by default. xml file Unless you try to extend the Android CardView class, you cannot customize that attribute from XML. How to change drawable background color from code (Java/Kotlin) file. In the screenshot just below, you can see it compared to TextInputLayout below it, which renders fine. And Add Background for that Layout. I want to change the highlighted or on state color of the Toggle Button from the standard light blue to green. android. You Can Implement this without having a cardview, and can also have all the properties of cardview. On API 20 and before, CardView does not clip the bounds of the Card for the rounded corners. Drawable background = imageView. 0 (API 21) – Louis CAD Commented Apr 3, 2017 at 18:54 I'm amazed by everyone else's MUCH more complicated answers. getResources(). I tried getPaint(). I'll try to change the card view background and stroke color programmatically but all times card. xml file? 2. It doesn't work properly, with the corners being doubly opaque in Android Studio, and the whole border being doubly opaque on my device. setElevation() . xml, and reference it in your layout with I have a CardView aligned bottom to screen, destpite the elevation I want to add more shadow to top of the CardView. You can get the View of the MyViewHolder by calling holder. I want to show an overlap of all cards. In next screen, select project name as CardView. To people looking for an answer when your vector have 2 or more <path> or you just want to keep your stroke color's different than fill color: I don't think there is a way to programmatically change only the fillColor of Vector while CardView cardView = (CardView) findViewById(R. colorPrimary)). Here's what I've tried. I have a CardView with radius and stroke, but when I change the CardView Background programmatically, the radius and stroke are lost, I want the new color to stay inside the stroke. Scrolling. ): myView. Set the stroke color using the strokeColor attribute. Then, fill other required details. As we're focusing more on design, Material Design is a hot topic here. ofArgb(cardView, "strokeColor", Color. Here are some visual examples related to this implementation. It is rounded and has a gradient background, and works fine and looks nice, but I couldn't do two things I wanted: Set a 1 pixel stroke with a given color. lang. Commented Oct 25 I'm trying to get a CardView to display the ripple effect when touched by setting the android:backgound attribute in the activity XML file as described here on the Android Developers page, but it isn't working. TRANSPARENT , otherwise you may get an If you wants to implement gradients on background or stroke, you have to define ColorType of them. getColor(context, The contents within a card should follow their own accessibility guidelines, such as images having content descriptions set on them. Without a strokeColor, the card will not render a stroked border, regardless of the strokeWidth value. 13 Android: How to set stroke color for vector You can do it either in XML or programmatically: In XML: card_view:cardBackgroundColor="@android:color/red" Programmatically: cardView. void onCheckedChanged(MaterialCardView card, boolean isChecked); Set gradient on stroke android. getColor(android. By removing it, the stroke color should be working as expected. text_input_layout_stroke_color)); To add on to what @tinsukE said: However, to avoid accidentally suppressing an item later in the list, for most states it won't make sense to put a negation - instead handle all "other" possibilities with a default (empty) item new int[]{} last -- as shown in the final code block of this answer. – Soroush Lotfi. Note that select Java as the programming language. Then, under Phone and Tablet section, select Empty Activity. ANTI_ALIAS_FLAG import android. Viewed 849 times Part of Mobile Development Collective Android: How to set stroke color for vector drawable programmatically. how to add stroke on text. getColorStateList(this,R. textColorSecondary reference and only this solution worked. For an advanced swipe-to-dismiss sample I am working on demo application in which I am using the card view of the support library. Note: For a better user experience, see Material Design Cards. thanks. ViewGroup. material. MaterialButton android:layout_weight="1" andr When i run my code on api 28 i do not see this white border along the inner edges of the cardview. cardBackgroundColor : Used for setting up the background-color of the card. I also advise to set strokeColor initially to Color. How to change stroke color of a drawable with Kotlin or Java. gmariotti. colorAccent,context. app. It can't be set to white unless I added foregroundTint with #FFF too. When a card is picked up, it appears in front of all elements (except app bars and navigation). Asking for help, clarification, or responding to other answers. view. toFloat() val shape = GradientDrawable() shape. card_view:cardCornerRadius="4dp" The Stroke element has the following attributes: Required attributes. my_color)); Providing background color to the child class of cardview will leave the padded parts in case if the card view has any, without color and this is not a good approach. I'm looking for a solution to change the color of some Android Material Components layout objects. 46. Thanks Tyler Pfaff, for pointing this out. setOrientation(LinearLay All the CardView does with this attribute is create a rounded rectangle drawable using the color, and then assigns it as the background of the CardView. OLD Answer: You cannot set a view's style programmatically Don't Don’t let cards bump other elements out of the way. setStroke(), but couldn't figure how to set the stroke color. layout_width="wrap_content" layout_row="0" layout_column="1" layout_gravity="fill" layout_margin="8dp" layout_columnWeight="1" layout_rowWeight="1" @MohammadNouri No, color state lists and resource references in the android:tint attribute are not supported prior to Android 5. NonNull import java. Drawable using Stroke only on the rightside of the solid. itemView; Now from here you can get I have set a background color for my cardView and child element individually. I want to create in activity a CardView programmatically. i am trying to use statelistdrawable as the following : When I try to change cardBackgroundColor programmatically (cardView. MaterialCardView change background color and stroke color programmatically. I have not tested the code, but you get the idea. getColor(context, R. mFab. The info about attributes available is here. To Sets the ripple color resource for this card. annotation. TextView import android. ; gradient_linear: The color is linear gradient. 0' implementation 'androidx. material) created a huge impact in this area. Provide details and share your research! But avoid . By default, it is adding a shadow around it. Add the following line to your code to set the background drawable in the TextView:. It works normally with colors other than white. Setting strokeColor to a CardView programmatically. 6. MaterialCardView from Material Design Library which is a subclass of androidx. Any help would be much appreciated. Which is inflated with the xml file that you posted. If you just want to use some of the predefined Android colors, you can use Color. Here's an example of a custom gradient border using a shape: Pro devs. Both have these values (Except that gradient_radial is not supported in stroke):. You can create a layout for this. It is fine if I have one card with one child element. (Both you can set via xml as well as programmatically). set cardview Background logic in Android. Drawable import androidx. I've the same problem described here. void: I need to change the stroke color from the app. I’ll try to change the card view background and stroke color programmatically but all times card. Hot Network Questions Its better if you migrate to androidx but you can use this code add shape to your card view <app:android. I've tried with android:shadowColor="#000" android:shadowDx="0" android: Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand; OverflowAI GenAI features for Teams; OverflowAPI Train & fine-tune LLMs; Labs The future of collective knowledge sharing; About the company Visit the blog For API 21+, Use Clip Views. setBackgroundColor Description: By default, when a ShapeableImageView has rounded corners and you define strokeWidth and strokeColor, it will be cut-off. setColor(ContextCompat. also, I don't know how get id stroke, and change only stroke (I see google, all example are failed) My XML item <layer-l Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand; OverflowAI GenAI features for Teams; OverflowAPI Train & fine-tune LLMs; Labs The future of collective knowledge sharing; About the company Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question. I do not inflate the cardview from xml. cardview ) stays white even though I set a backround drawable via android:backround - The documentation gives me the feeling that it should work. Get the Drawable from imageView using getBackground():. If you want to set a background Image Inside Cardview Use Another Layout such as LinearLayout, RelativeLayout or any other Inside The CardView. I've tried this, but it If I understand correctly, you are trying to avoid cropping elevation shadow of CardView. io, both said I need to set both stroke width and stroke color in order to render the stroke. CardView is a versatile component that helps achieve modern and elegant UI designs. I want to restrict it to right side only, like you do in CSS (border-right:1px solid red;). ofArgb() for this case: ObjectAnimator. answered Jan 29 Android - How to programmatically set button color. in your code: your_button. cardview:cardview:1. Color import android. Statically in XML, it works Using the androidx. Output: Some important attributes of Cardview are : 1. Depending on whether a train service stops or passes, I want the MaterialCardView to have a different style (ie a different background color). google. Step 1: Create a New Project in Android Studio. card. cardElevation : Defines the elevation (the process of moving to a higher place or more important position) of the card. I am having difficulty in changing the text color with the background color on touch (hover effect) in cardview. parseColor("#738b28")); Also you can give the text color for a button like: Button11. Since we often focus on changing View's color in normal and pressed state, I'm going to add more details:. I used below property and it is rounding all the corner. If you really want to set the color programmatically, you could create a copy of RoundRectDrawableWithShadow, and then do this: Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand; OverflowAI GenAI features for Teams; OverflowAPI Train & fine-tune LLMs; Labs The future of collective knowledge sharing; About the company Visit the blog I think the answer is in your question: If, in the inner view of the CardView, you set a background colour, and associated states, which are respected, however, it will display over the corners you defined in the CardView. Today I'm going to discuss one of the most used elements Step 5: Now run your app on your emulator or device you will get to see the output. How should I do it? I want my card to look like the following I kept my layout like this <android. dpToPx. I can change the color of vector drawables programmatically, but I want to apply the stroke to vector drawable. Follow edited Jan 29, 2020 at 4:32. Important: to see any stroke color at all, you have to give your CardView a strokeWidth of at least 1dp. Stack Overflow. Modified 6 years, 1 month ago. linear); cardView. Cards implement Checkable, a default way to switch to CardView is a new Android widget that enhances UI design with rounded corners and elevation, suitable for displaying data in listviews and Recycler Views. setBackground(layerDrawable); I'm able to draw border to a linear layout, but it is getting drawn on all sides. color. Graphics. Gradient Drawable - getting the Stroke Color programmatically. This attribute Description: The material card view does not clear the previously selected card view's color even after adding the new stroke color in the card view. v7. setCardElevation(1000); linearLayout. Pro devs. graphics. I have tried dynamically doing but would Skip to main content. I want to How to set the stroke color to MaterialButton programmatically <com. CardView android:layout_width="150dp" android:layout_height="148dp" android:elevation="12dp" app:cardCornerRadius="75dp" android:layout_centerHorizontal="true" android:innerRadius="40dp" android:shape="ring" Step-by-Step Implementation. You could use an xml file like one below, to create states for your button. This sho I try to set background color programmatically but when I set every one of my colors, the background being black but with any color background being white like the application theme. Android Button color not changing programatically. widget. Hot Network Questions Why do the A-4 Skyhawk and T-38 Talon have high roll rates? Making a polygon using equilateral triangles and squares. use preventCornerOverlap, according to documentation:. Open Android Studio (Ignore if already done). 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. Inside that cell I want to display an ImageView with a border around. If you need a background image, rather than a color, place an ImageView inside of your card as this answer suggests. Step 2: Working with the purple_bg. 2. getMetrics(displayMetrics); int height = displayMetrics. Is this some kind of bug? I'm using a standard Switch control with the holo. strokeColor = ColorStateList(states, colors) Share. I noticed that this behavior was triggered by the app:cardUseCompatPadding="true" property. Card content that is taller than the maximum card height is truncated and does not scroll, but can be displayed by expanding the height of a card. Instead of manipulate margins, try set cardUseCompatPadding to true and if you are changing elevation dynamically, you should call setMaxCardElevation(float) when CardView is initialized. findViewById(R. import android. My first idea was to put a background resource to the ImageVi In your case you are not specifically interested who is the parent of your CardView, because the only thing you want to change is the margin. setCardBackgroundColor() override the stroke color. So here is the complete step by step tutorial for Change CardView background color in android programmatically. Ask Question Asked 6 years, 1 month ago. but on api 19 i get the following rendered: My desire is for the color to entirely fill the cardv Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Advertising & Talent Reach devs & technologists worldwide about your product, service or employer brand; OverflowAI GenAI A MaterialCardView in dragged state. Are you trying to set an Image to ImageView? First of all You have to use the property: android:src="drawable" If you are using a vector asset you will need to use the property:app:srcCompat="drawable" To understand the background, backgroundTint properties have a read at this: What is the difference between Just as @V-rund suggested Get the Screen/Window height and divide it by 3 using, DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager(). . COLOR (where COLOR is BLACK, WHITE, RED, etc. getColor() but not in this case because it's referenced! android; android-resources; android-theme; I wanted to get color from android. Now in your onBindViewHolder method you bind your data to your MyViewHolder. setOnClickListener(clickListener); dra Vijet Badigannavar's answer is correct but using ColorStateList is usually complicated and he didn't tell us how to do it. CardView import android. vjqpi gnhsi tbe ozijc wjoh xexgp ijapj ltkglb rospgg fdt