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>
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 a problem and I really do not know what's going on.
It is the following, I have a drawable.xml as background of a textview that is element of a listview, I set it and everything OK but I have the action that when they touch it expands (changing the number of visible lines), at that moment it is that the background is lost. Its happens only in very large views
Before it was done with background images.9 and it worked well
Here are the elements mentioned and a picture :D
sample
<img src="https://i.stack.imgur.com/0wIaX.jpg" alt="Sample" height="50%" width="50%">
Regards
fondo.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:paddingRight="25dp"
android:paddingLeft="25dp">
<solid android:color="#color/mailUnReadBack" />
<corners
android:bottomLeftRadius="15dp"
android:bottomRightRadius="15dp"
android:topLeftRadius="15dp"
android:topRightRadius="15dp" />
</shape>
Activity.java
...
private void expand(View view) {
((TextView) view).setMaxLines(reduceLines?minVal: Integer.MAX_VALUE);
}
...
myView.java
...
setBackground(getResources().getDrawable(R.drawable.fondo));
...
Try the following
drawable/fondo.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<padding
android:bottom="5dp"
android:top="5dp"
android:right="25dp"
android:left="25dp">
<solid android:color="#color/mailUnReadBack" />
<corners
android:radius="15dp" />
</shape>
Java
Drawable background = ContextCompat.getDrawable(context, R.drawable.fondo);
ViewCompat.setBackground(yourView, background);
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 am new to Android, and I have got this problem : the shape drawable cannot be applied to the TextView.
The java code in my textview as below:
shape = getResources().getDrawable(R.drawable.shape_tag_round_corner);
this.setBackgroundDrawable(shape);
this.setBackgroundColor(color);
this.setText(roleName);
this.setGravity(Gravity.CENTER);
XML shape :
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="5dp"></corners>
<padding
android:left="5dp"
android:top="0dp"
android:right="5dp"
android:bottom="0dp"
/>
</shape>
Finally, the TextView does not get the shape and the color setted in the code, why?
You can have shapes for textviews in android, go to your xml file and look for the intial lines of code
they would look something like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ScreenOnOff" >
Here the relative layout is a type of drawable view, and try to gravity in xml file not through java. Your drawable problem will be solved in xml itself. Hope this was of some help
You can only have one drawable as the background.
First you set the shape (which is transparent), and then replace it with the color.
If you want a colored shape, use that xml :
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff" />
<corners android:radius="5dp"></corners>
<padding
android:left="5dp"
android:top="0dp"
android:right="5dp"
android:bottom="0dp"
/>
</shape>
And then :
shape = getResources().getDrawable(R.drawable.shape_tag_round_corner);
shape.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.DST_ATOP ));
this.setBackgroundDrawable(shape);
What I want to do is have something like the picture below.
The whole rounded corner rectangle needs to be clickable. Then Record: ### and ### need to be some sort of TextView or String. The green check mark needs to be an ImageView.
I am having trouble even knowing where to start with this. I know there is a way to achieve this because the app Unblock Me has something sort of like how I want it. Below is a screenshot of their app.
Any help on ideas of how to achieve this?
It is kind of easy.
First that rounded rectangle can be a linear layout ok.
In its background attribute you pass a layer list which will contain two items having colors black and white with radius of lets say 5dp
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- "background shadow" -->
<item>
<shape android:shape="rectangle" >
<solid android:color="#DDDDDD" />
<corners android:radius="15dp" />
</shape>
</item>
<!-- background color -->
<item
android:bottom="5px"
android:left="5px"
android:right="5px"
android:top="5px">
<shape android:shape="rectangle" >
<solid android:color="#FFFFFF" />
<corners android:radius="8dp" />
</shape>
</item>
</layer-list>
put this in a xml file and save it in a drawable folder and put it in the background attribute of your linear layout.
Now in your layout, with orientation horizontal, put three child elements
two textviews and one imageview with weights 35, 35, 30 and set image to the imageview that green tick mark sign.
Voila!