How to change card_view:cardCornerRadius programmatically - android

In my project, I am using RecyclerView in my listing using CardView. In my listing I have to set the corner radius of CardView dynamically based on device.
Is there any way to set cardview corner radius value dynamically?
Thanks.

Use CardView.setRadius(float), see androidx.cardview.
(It is worth noting that this will have no effect if you also invoke setBackgroundColor, make sure to use setCardBackgroundColor instead).
You must specify a pixel size, rather than dp value, e.g. for corner radius of 4dp you can invoke, in Kotlin:
radius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4f, context.resources.displayMetrics)

Related

Approximating "height" of layout when contains TextView

I want to use this simple layout for items of list:
Note the right gray area (which I draw red rectangle on its border in below) must be a square:
So I have to approximate the height of layout. It must be simple, but the problem is that the text size of TextViews is set by using sp unit and height of layout must be set by dp unit and I did not found a way for converting dp and sp in .xml file. Also I do not want to create a CustomView or change properties of layout at runtime. All things must be done in .xml file. How I can approximate height of layout?
See the TextSize is different from TextView's width and height.
So Provide fixed width-height to all views in "dp". Fixed Text-Size in "sp" that fits perfectly inside TextView.
P.S. -> test in a few devices/emulators to verify.

MPAndroidChart Remove space when right axis is disabled

I use this code to hide right axis:
linechart.getAxisRight().setEnabled(false);
but it has a space like margin of chart (Line chart width is match_parent without margin)
Is there any way to remove this space and make the chart fill to the right edge of screen?
Actually, this space is better for keep. But if you really want to remove, I found this method that can help:
chart.setViewPortOffsets(leftOffset,topOffset,rightOffset,bottomOffset);
You can try this method by custom the view port to remove the blank space.
Firstly you disable the right axis by using
// Java
linechart.getAxisRight().setEnabled(false);
// Kotlin
linechart.axisRight.isEnabled = false
After disabling the right axis there will still be some blank space remaining. All the surrounding padding of the chart can be removed using:
// Java
linechart.setMinOffset(0f);
// Kotlin
linechart.minOffset = 0f
Please also make sure that you don't have any extra offset set by using one of these earlier:
// Kotlin
lineChart.extraLeftOffset
lineChart.extraTopOffset
lineChart.extraRightOffset
lineChart.extraBottomOffset

CardView cardUseCompatPadding

I am developing an Android app for both Lollipop and Previous versions.
I am using CardView (This cardView does not have any child, it simply placed behind my View) to create shadow.
But the problem arise when it runs on the pre Lollipop devices.
so I set cardUseCompatPadding to true. I am wondering if I could get the value of this compat padding?
Is there anywhere I could find the reference to the value?
The compat padding added to the CardView depends on the elevation and the radius of the corners you have set. You can find the actual calculation in the RoundRectDrawableWithShadow class in the support library.
You can calculate it at runtime using something like:
float elevation = cardView.getMaxCardElevation();
float radius = cardView.getRadius();
double cos45 = Math.cos(Math.toRadians(45));
int horizontalPadding = (int) (elevation + (1 - cos45) * radius);
int verticalPadding = (int) (elevation * 1.5 + (1 - cos45) * radius);
Updates the backward compatible maximum elevation of the CardView.
Calling this method has no effect if device OS version is Lollipop or
newer and getUseCompatPadding() is false.
Use this code
android:elevation="3dp"or app:elevation="3dp" to your cardview
CardView uses elevation property on Lollipop for shadows and falls
back to a custom emulated shadow implementation on older platforms.
Due to expensive nature of rounded corner clipping, on platforms
before Lollipop, CardView does not clip its children that intersect
with rounded corners. Instead, it adds padding to avoid such
intersection (See setPreventCornerOverlap(boolean) to change this
behavior).
Before Lollipop, CardView adds padding to its content and draws
shadows to that area. This padding amount is equal to maxCardElevation
+ (1 - cos45) * cornerRadius on the sides and maxCardElevation * 1.5 + (1 - cos45) * cornerRadius on top and bottom.
Since padding is used to offset content for shadows, you cannot set
padding on CardView. Instead, you can use content padding attributes
in XML or setContentPadding(int, int, int, int) in code to set the
padding between the edges of the CardView and children of CardView.
Note that, if you specify exact dimensions for the CardView, because
of the shadows, its content area will be different between platforms
before Lollipop and after Lollipop. By using api version specific
resource values, you can avoid these changes. Alternatively, If you
want CardView to add inner padding on platforms Lollipop and after as
well, you can call setUseCompatPadding(boolean) and pass true.
To change CardView's elevation in a backward compatible way, use
setCardElevation(float). CardView will use elevation API on Lollipop
and before Lollipop, it will change the shadow size. To avoid moving
the View while shadow size is changing, shadow size is clamped by
getMaxCardElevation(). If you want to change elevation dynamically,
you should call setMaxCardElevation(float) when CardView is
initialized.

achartengine; fitting all x axis labels without clipping, and without margins on graph

I'm using a fill to color the area below my chart, and I want the graph to take up the entire view, so I'm setting zero margin on the right and left in my renderer.
mRenderer.setMargins(new int[]{20, 0, 20, 0});
Graph renders great; uses all horizontal space and doesn't look wonky like it does without fill extending to right or left margins when they're set to > 0 values.
I'm also using custom text labels, aligned center, and I'm getting the outer two labels clipped; setting padding on x labels amplifies the problem by pushing labels off view.
Is there a way I can fit all the x labels in without the truncation, and without having to use margins? I can fit them all using margins, but then it just looks funky using the fill below.
Ideally, I guess there'd be some way to set alignment on each text label individually; probably asking for a lot. Or, a means to specify whether it should fit x labels when using custom text labels, such that it would align right on the first, and align left on the last.
Thanks

Drawing custom view using Android

I am drawing a custom view in my application which basically takes arguments(XML) as the text to display and then keeps on rotating it infinitely.
While I was making this control I had a few doubts which I want to ask:
I have made 2 of my stylable attributes which I have declared in the attrs.xml file. These attributes are to set the width and the width of the circle used in my control. These values I would use in the ondraw method and the onmeasure method. I ran my program by declaring just these but there was an error which asked me to put android:width and android:height attributes. My question is why would I need those if I am already using my custom attributes to define the height and the width of my view
I am coding on a mac. Now when I was making my attrs.xml file, to get the autocomplete list which we usually get by ctrl+space was not showing up. Why is that. For e.g., i wanted to know what values can I give the format attribute of my custom attribute that like I am demostrating in the following:
<attr name ="somename" format="value I wanted to find out through autocomplete"> . Why is the autocomplete not popping up? It does pop up when I am making a.java file.
In my ondraw() method I wanted to draw a circle. Now for this I would require the co-ordinates of the center of the circle. So what I do is put 0, 0. But this is not correct. I want the circle to be at the center of the parent view it is in. So if my view is in a hierarchy I want it to respect that. I want to give the co-ordinates relative to the parent view. What is happeining right now is that it is being drawn at the top left corner of my screen. This is what I don't want. What do I do to achieve this?
why would I need those if I am already using my custom attributes to define the height and the width of my view?
This is because Android needs to know how and where to put your view in the layout. You can implement your view to make use of your custom height/width requirements by overriding View.onMeasure() to return your own size parameters. You can then just set android:layout_width="wrap_content" android:layout_height="wrap_content" in your layout file.
Why is the autocomplete not popping up? It does pop up when I am making a.java file.
Autocomplete and autodoc implementation for Android xml files is shaky at best, so it's not really surprising that it doesn't work. I'm not aware of a fix and AFAIK it's an IDE bug.
In my ondraw() method I wanted to draw
a circle. Now for this I would require
the co-ordinates of the center of the
circle. So what I do is put 0, 0. But
this is not correct. I want the circle
to be at the center of the parent view
it is in. So if my view is in a
hierarchy I want it to respect that. I
want to give the co-ordinates relative
to the parent view. What is happeining
right now is that it is being drawn at
the top left corner of my screen. This
is what I don't want. What do I do to
achieve this?
If you implemented onMeasure() correctly, the coordinates relative to parent should be taken care of already. To get the center of your view use the following code:
void onDraw(Canvas canvas)
{
int centerX = this.getWidth() / 2;
int centerY = this.getHeight()) / 2;
//..draw code here
}
Edit
Here's an example with source code that should help: http://mindtherobot.com/blog/272/android-custom-ui-making-a-vintage-thermometer/

Categories

Resources