Is possible to set item params programmatically drawable xml file? - java

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

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. :)

How to define a round ImageView on Android?

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"/>

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.

Why does the android:src behaves weirdly on padding in drawable?

I recently did some test on android drawable and I found that the android:src behaviors to padding is extremely weird compare the android:background.
Here is my drawable:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#color/colorAccent"/>
<padding
android:top="1dp"
android:right="1dp"
android:bottom="1dp"
android:left="1dp"
/>
</shape>
</item>
<item android:top = 2>
<shape android:shape="oval">
<solid android:color="#color/white"/>
</shape>
</item>
</layer-list>
Inside of the mainActivity layout I have another image view which I used to test. I set the image view to 40dp width and height. Then I tested the android:src vs the android:background attribute by setting them to my drawable above. Here is the result:
Android:src
Android:background
The android:background as you can see is the expected behavior because I move the white circle down 2 and it has padding that obeys the pink oval padding. Now the android:src is straight up unexpected because 1st of all the white oval which is the layer that is above the pink oval is not shown. Second of all, the shapes change to a flatten oval.
Any clue to why this is the case??????
src works with android:scaleType where you can give different options ScaleType
whereas background always takes scaleType of FitXY covering full region for that view.
if you want the same behaviour as background with src,
put android:scaleType="FitXY". there are other options you should check also.

Changing Spinner Highlight AND line color

This XML below results in my Spinner having an orange line across the bottom and an orange rectangle on the right-hand side. I have two questions:
How can I make it so that the orange rectangle on the right-hand side is an orange triangle, like the default one associated with Theme.Dialog?
Now that I changed the look of the Spinner, when I click on it it no longer gets highlighted. I want to make it look the way I want AND highlight to a lighter color of orange when I press on it.
Do I have to accomplish both things in the same XML file? I've already got the drawable below set to the background of my spinner, so I can't declare the background twice right?
Any ideas? Thanks in advance!
My Drawable Custom Spinner Layout:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape >
<solid android:color="#color/base_orange" />
</shape>
</item>
<!-- main color -->
<item android:bottom="1.0dp"
android:left="0.0dp"
android:right="10.0dp">
<shape >
<solid android:color="#color/white" />
</shape>
</item>
<item android:bottom="25.0dp">
<shape >
<solid android:color="#color/base_orange" />
</shape>
</item>
</layer-list>
You could refer to this and get the spinner drawables according to your wish.
Hope that helps

Categories