How to define a round ImageView on Android? - java

All the view classes on Android are define in rectangle shape.When you touches it detects in the rectangle zone,when you set image it fills the rectangle.How to make a class which is round by default?
Features I am looking for:
Support all the ImageView functions
Square Image input with setImageBitmap or onDraw are converted to round.
Dectect circular touch zone instead of sqaures
Small in size and overhead.
3rd Party is fine but prefer just use interal library.

Create a shape inside your drawable package.
// res/drawable/circle.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="1.9"
android:useLevel="false" >
<solid android:color="#android:color/transparent" />
<stroke
android:width="10dp"
android:color="#android:color/white" />
</shape>
Then create a layer list inside the drawable
// res/drawable/img.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/circle"/>
<item android:drawable="#drawable/ic_launcher"/>
</layer-list>
Then create the image view with layer list
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/img"/>

Related

How to make a fade black background in Android Studio

I would like to put a fade black covering my image, just like this one, with a text, a fade background and then my image. I just want to know how to implement this fade. Thank you!
Just create a drawable file, I'll name it gradient_background (you can set any name)
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="90"
android:startColor="#000000"
android:centerColor="#00000000"
android:endColor="#00000000"/>
</shape>
then inside your target xml, create a View like this one, look that inside it, we're setting the background as your drawable file. In this case is gradient_background
<View
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#drawable/gradient_background"
app:layout_constraintBottom_toBottomOf="post_image"
app:layout_constraintEnd_toEndOf="#+id/post_image"
app:layout_constraintStart_toStartOf="post_image"
app:layout_constraintTop_toTopOf="#+id/post_image" />
So it should look like this:
:)
First, Make bottom_transparent_gradient.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient android:angle="270"
android:endColor="#64000000"
android:startColor="#android:color/transparent" />
</shape>
</item>
</selector>
Then, add that drawable as your view background in which you want to show the fade background effect.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bottom_transparent_gradient"/>
And that's it! You can see the result you wanted. :)

Is possible to set item params programmatically drawable xml file?

I have drawable file that includes layer-list and items inside. I'm trying to set position of one item by variable (or method) from Java class.
I've tried to find it by id and use setLeft(int), but it doesn't work.
findViewById(R.id.arr).setLeft(tone.position());
tone.positions() gives me int values
<View
android:id="#+id/vid"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginStart="90dp"
android:layout_marginEnd="90dp"
android:background="#drawable/line"/>
This is in LinearLayout in MainActivity xml file
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#00ffffff"/>
</shape>
</item>
<item android:gravity="center">
<shape android:shape="rectangle">
<size android:height="1dp"/>
<solid android:color="#000000"/>
</shape>
</item>
<item
android:id="#+id/arr"
android:left="10dp"
android:gravity="center_horizontal"
android:bottom="5dp"
android:top="-5dp"
android:drawable="#drawable/arrow" />
</layer-list>
There is my drawable file. I want to change "left" attribute in item with "arr" id.
Is it possible?
findViewById() can be used to retrieve Views in your layout xml files. More specifically, the layout that you set using setContentView(). Your sample, will probably return null, since there is no #+id/arr in your layout. Hence, you can only use findViewById() to retrieve the view with android:id="#+id/vid"
You might be able to achieve what you want, by first getting the View. And getting the background from that view afterwards.
Something like this (untested):
val view = findViewById(R.id.vid)
val background = view.background as? LayerDrawable
background?.setLayerInsetLeft(R.id.arr, tone.positions())
or in Java (not sure about the syntax, but you'll get the idea):
View view = findViewById(R.id.vid);
LayerDrawable background = (LayerDrawable)(view.getBackground());
background.setLayerInsetLeft(R.id.arr, tone.positions());
For more details what you can do with a LayerDrawable, look at the official documentation

How to set thickness of circular indeterminate progress bar android?

I have two nested circular indeterminate progress bars but I cannot set their thickness to an equal size. I have tried to customize it using shapes. However, that removes the default behaviour of the circular indeterminate progress.
I customize like this and it made thickness equal but not getting default behaviour.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/progress">
<rotate android:toDegrees="360">
<shape
android:shape="ring"
android:thickness="4dp">
<solid android:color="#color/white" />
</shape>
</rotate>
</item>
</layer-list>
<ProgressBar
android:id="#+id/download_progress4"
android:layout_width="85dp"
android:layout_height="85dp"
android:layout_gravity="center"
android:indeterminate="true"
android:indeterminateDrawable="#style/ProgressThem" />
I want like this with default behaviour of circular indeterminate progress bar
Try this
USE
android:innerRadiusRatio="3"
android:thicknessRatio="20.0
SAMPLE CODE
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/progress">
<rotate android:toDegrees="360">
<shape
android:shape="ring"
android:thickness="4dp"
android:innerRadiusRatio="3"
android:thicknessRatio="20.0">
<solid android:color="#color/white" />
</shape>
</rotate>
</item>
</layer-list>
If you want the modern animation rather than just rotation, you can copy and alter https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/drawable/progress_indeterminate_anim_medium_material.xml using it as the indeterminateDrawable.
<ProgressBar
android:indeterminateDrawable="#drawable/new_progress_indeterminate_anim_medium_material"
To change the width in vector_drawable_progress_bar_medium.xml android:strokeWidth="1" for example.
It's a bit painful to do this as you also need to copy over the animation, interpolator and animator xml files to your project as they are private in the Android SDK.
There is actually an even easier way to do that with the material components:
<com.google.android.material.progressindicator.CircularProgressIndicator
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
app:trackThickness="3dp" />
More details in material docs -> https://material.io/components/progress-indicators/android#circular-progress-indicators

Create drawable programmatically

How can I create this exact drawable in java code only?
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size android:width="1dp" />
<solid android:color="#ff0000" />
<padding
android:bottom="-15px"
android:top="-5px" />
</shape>
I need the padding values to be variable so I can't do it as an xml.
If you wanna draw something custom, you should open a Canvas and make your drawing there.
Read this:
https://developer.android.com/training/custom-views/custom-drawing.html
And it seems a simple shape. So you can add a layout view (View,TextView, ImageView, etc.) set background with this drawable and adjust padding through this view.

How can I shape my button on android rectangle to circle?

In generally android buttons are rectangular and on the mxl file what should I do if I want to make ring shaped or any other shaped button? or is there any tool that assure these to android developers which is open source (for now I'm seeking a ring-shaped button ).
create a new xml file inside your drawables, and put this code:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:innerRadius="15dp"
android:thickness="10dp"
android:useLevel="false">
<solid android:color="#fff" />
<size
android:height="50dp"
android:width="50dp" />
</shape>
now, use this as your image in an imageButton
You can also take a look at this:
android-state-list-generator

Categories