I would need move-change position of individual bitmap items in layer-list such as this:
background.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<gradient
android:centerX="0.5"
android:centerY="0.3"
android:endColor="#color/colorBackgroundEnd"
android:gradientRadius="300dp"
android:startColor="#color/colorBackgroundStart"
android:type="radial" />
</shape>
</item>
<item
android:id="#+id/peas"
android:left="35dp"
android:top="110dp"
>
<bitmap
android:gravity="left|start|top"
android:src="#drawable/peas"
/>
</item>
<item
android:id="#+id/orange"
android:right="-35dp"
android:bottom="110dp">
<bitmap
android:gravity="right|end|bottom"
android:src="#drawable/orange" />
</item>
<item
android:id="#+id/salad"
android:bottom="-40dp"
android:left="-40dp">
<bitmap
android:gravity="left|start|bottom"
android:src="#drawable/salad"/>
</item>
I was able to reference items in java code:
layerDrawable = (LayerDrawable) ContextCompat.getDrawable(MainActivity.this,R.drawable.background);
Drawable salad = (Drawable) layerDrawable.findDrawableByLayerId(R.id.salad);
and get-set some layer properties, such as getAlpha and setAlpha
layerDrawable.findDrawableByLayerId(R.id.salad).setAlpha(0);
down.setText( String.valueOf(layerDrawable.findDrawableByLayerId(R.id.salad).getAlpha()));
, but I need code access to <item> left and top property that would enable setting layer bitmap position, like it can be done from XML file. How do I do this?
Related
How can I change the color of the card view elevation?
Or make it softer
It gets dark when I increase the app:cardElevation value
Add these 2 properties to your CardView
android:outlineAmbientShadowColor="#color/yourShadowColor"
android:outlineSpotShadowColor="#color/yourShadowColor"
here you can change color first you can make a drawable file and paste this code in it. then in your layout inside cardview, you can assign as background file.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#android:id/background">
<shape>
<corners android:bottomLeftRadius="8dp"
android:topLeftRadius="5dp"
android:bottomRightRadius="8dp"
android:topRightRadius="5dp"/>
<solid android:color="#ddd"/>
</shape>
</item>
I have 2 pictures, one is a background and the other is a complete picture.
I want to create one image from both of them as if one is above the other and save them on your cell phone as one image
Which JAVA library should I study and research?
I would be happy if you would refer me to the same library I develop in Android Studio.
Yes you can, this is what you have to do:
Folder drawable -> new -> drawable resource file -> choose the name of the file and click ok
and this is what you get
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
</selector>
Inside the tags selector put the tag <layer-list> (remember to close the tag) and inside the layer-list put the tag <item>
your result can be this for example:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<layer-list>
<item>
</item>
<item>
</item>
</layer-list>
</item>
</selector>
Now you can customize your 2 items, if you want to put an image inside your item you can write your code like this:
<item
android:gravity="center_vertical|center_horizontal"
android:drawable="#drawable/YOUR_IMAGE"
android:width="96dp"
android:height="96dp"
tools:ignore="UnusedAttribute" />
if you want to put a shape of an object like a rectangle you can write your code like this:
<item>
<shape
android:shape="rectangle">
<solid
android:color="#000000" />
<size
android:width="1080px"
android:height="1920px" />
<padding
android:left="0dp"
android:top="0dp"
android:right="0dp"
android:bottom="0dp" />
</shape>
</item>
Now you have your image composed with 2 images (or other things) and if you want to use it you can just call android:background="#drawable/YOUR_FILE_NAME" inside your layout_activity.xml for example
with this informations I think you can solve your problem, cya :)
I have defined a custom background to RadioButton in my project so that I can change it in the java code. The background is a Layer-list that includes 2 items: XML drawable and image.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#color/colorAccent"/>
<size android:width="20dp" android:height="20dp"/>
<corners android:radius="3dp"/>
</shape>
</item>
<item
android:id="#+id/correct"
android:drawable="#drawable/correct_image"
android:height="15dp"
android:width="15dp"
android:gravity="center"/>
</layer-list>
The design will break because the height and width are not supported prior to API 23. Is there any way that I can set the size of the image programatically using dimension value?
I don't have any prior coding experience or knowledge, so any explanation provided with possible solution will be very helpful.
Thank you
Currently I have a action bar menu with few items inside.
I am trying to have a menu item with shape which has a drawable
The code i get so far:
ShapeDrawable circle = new ShapeDrawable(new OvalShape());
circle.getPaint().setColor(Color.GREEN);
circle.setIntrinsicHeight(120);
circle.setIntrinsicWidth(120);
circle.setBounds(0, 0, 120, 120);
menu.findItem(R.id.menu_item_1).setIcon(circle);
However currently only looks like a green circle, I want inside to have a drawable icon.
for example
just create icon.xml in drawable folder:
<?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/colorPrimaryDark"/>
<size
android:width="70dp"
android:height="70dp"/>
</shape>
</item>
<item android:drawable="#android:drawable/ic_dialog_map" android:gravity="center">
</item>
</layer-list>
and use it like this:
menu.findItem(R.id.menu_item_1).setIcon(ContextCompat.getDrawable(this, R.drawable.icon));
Is it possible to draw a line like shown in the fig. using android xml ? I need it for setting a background for view pager indicator
If you need hard edges like that, I'd suggest using a 9-patch: http://developer.android.com/tools/help/draw9patch.html
If you're ok with soft edges, you can use a gradient (which I got from using this online tool):
<gradient
android:angle="0"
android:centerX="50%"
android:centerColor="#000000"
android:startColor="#FF0000"
android:endColor="#FF0000"
android:type="linear"
/>
create a new drawable like this
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="-100dp"
android:top="-100dp">
<shape android:shape="rectangle">
<stroke
android:width="100dp"
android:color="#FF0000" />
<solid android:color="#ffffff" />
</shape>
</item>
</layer-list>
and then add this as background.