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?
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
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.
I have an xml file, res/color/btn_black that allows me to apply a gradient look to buttons.
I can use it in a layout.xml successfully by calling:
<Button
android:background="#color/btn_black"
/>
Elsewhere, I am creating buttons dynamically in Java, and I want to apply the same style. When I try this using:
myButton.setBackgroundColor(getResources().getColor(R.color.btn_black));
I get this error:
android.content.res.Resources$NotFoundException:
File res/color/btn_black.xml from color state list resource ID #0x7f040001
This seems to be the correct method from other questions I've found answered here, but it isn't working for me. What am I doing wrong?
edit: This is the file btn_black.xml for reference
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#343434" />
<stroke
android:width="1dp"
android:color="#171717" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#343434"
android:endColor="#171717"
android:angle="270" />
<stroke
android:width="1dp"
android:color="#171717" />
<corners
android:radius="4dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
Seems like a color in your colors.xml defined in wrong way. Your colors.xml should look like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="btn_black">#000000</color>
</resources>
As I see, you have defined res/color/btn_black.xml, what is wrong. You need to create colors.xml file in /res/values/ directory.
If you have a gradient xml-file, then you need to put it in /res/drawable/ folder and call myButton.setBackground(getResources().getDrawable(R.drawable.btn_black)) method.
i think the best solution is to define a layout for the button with a specific style.
Then you can inflate the button with the layout you've created.
See this question for specific help android set style in code
So in your case you can use your xml file with the defined layout and inflate the button created programmatically
I've defined a custom shape in my res/drawable folder:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke
android:color="#FF404040"
android:width="1dp"
android:dashGap="3dp"
android:dashWidth="3dp"
/>
<size
android:height="1dp"
/>
</shape>
I'm using it as a background for one of my views. The shape is positioned vertically centered inside the view, but I want it to appear at the bottom of the view instead, is there a way to do this?
I am not sure there is a way to do position shape inside a view. However as a workaround I would consider something like this.
<RelativeLayout >
<OldView with shape as background now without any background
android:layout_alignParentTop="true" android:layout_alignParentBottom="true"/>
<View with this shape as background
android:layout_alignParentBottom="true"/>
</RelativeLayout>
This will supposedly give you what you want to achieve.
Try this , this may be useful,
http://thinkandroid.wordpress.com/2010/01/14/how-to-position-views-properly-in-layouts/
Set your custom view as background of layout or view.
or if not useful then provide your layout xml file code here so i can help you more easily.
The following receives the margin layout params of the shape so that you can change the views' margins, positioning it anyway you want:
MarginLayoutParams params = (MarginLayoutParams)shape.getLayoutParams();
// Sets the margins of the shape, making it move to a specific location on the screen
params.setMargins(int left, int top, int right, int bottom);
I had a similar problem, but I wanted to have a background for my views, a rectangle, but only left and bottom sides of the rectangle to be shown, and no top or right ones. In order to achieve this, I have used this xml:
<?xml version="1.0" encoding="utf-8"?>
<item
android:bottom="2dp"
android:left="2dp"
android:right="2dp"
android:top="2dp">
<shape
android:shape="rectangle" >
<stroke
android:width="1px"
android:color="#FFFF00" />
</shape>
</item>
<item
android:bottom="3dp"
android:left="3dp"
android:right="1dp"
android:top="1dp">
<shape
android:shape="rectangle" >
<stroke
android:width="1px"
android:color="#000000" />
</shape>
</item>
Note: There are two rectangles, one drawn on top of the other, the second one drawn on top of the first, but a bit shifted, so that 1dp of the first one to be shown on screen on the left and bottom. Also, you must note that the color of the second one must be chosen to be the one who hides. In my case it was the black color.
The result is (You may observe yellow lines only on the left and bottom):
I know gravity works on other kinds of drawables
android:gravity=["top" | "bottom" | "left" | "right" | "center_vertical" |
"fill_vertical" | "center_horizontal" | "fill_horizontal" |
"center" | "fill" | "clip_vertical" | "clip_horizontal"]
Here is my decision to embed blocks
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<solid android:color="#f00" />
<corners android:radius="50dp" />
<padding android:bottom="20dp" />
</shape>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<solid android:color="#090" />
<corners android:radius="50dp" />
<padding
android:top="450dp"
/>strong text
</shape>
</item>
</layer-list>