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);
Related
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);
If I have a drawable shape:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="2dp" android:color="#FFFFFFFF" />
<gradient android:startColor="#DD000000" android:endColor="#DD2ECCFA"
android:angle="225"/>
<corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp" android:topRightRadius="7dp"/>
</shape>
How would I go about using this as the background for a textview? I tried something like below, but it didn't work.
TextView jObjTv = new TextView(getActivity());
jObjTv.setBackgroundDrawable(findViewById(R.drawable.sample_box));
The code you posted doesn't work because a drawable isn't a view and isn't a part of the layout.
You need to call textView.setBackgroundResource(R.drawable.sample_box) if you want to use the resource. If you want to use the Drawable, you need to create the Drawable using context.getResources().getDrawable(R.drawable.sample_box).
Assuming I have an ImageView defined as:
<ImageView
android:id="#+id/my_image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/my_image_view_background"
/>
With my_image_view_background being:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<gradient
android:angle="45"
android:startColor="#555555"
android:endColor="#111111"
android:type="radial"
android:centerX="0.1"
android:centerY="0.1"
android:gradientRadius="70"
/>
<stroke
android:width="5dp"
android:color="#ff3300"
/>
</shape>
Is it possible to access the my_image_view_background drawable and change the colour of its stroke in code? Also, is it possible to do that based on the 'state' (pressed/selected/default)?
yes its possible
using GradientDrawable you can do it thru code.
link:
GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, new int[] {0xFFadced4, 0xFF71c2eb });
gd.setCornerRadius(0f);
gd.setStroke(1, 0xFF7c279b); //here you can define stroke width
gd.setCornerRadius(3f);
refer this site:
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>