How to use a drawable shape in Android through Java? - java

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

Related

Change Card View Elevation Color

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>

Using resources to set the size of an item in Layer-list

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

Why Drawable background disappears in large Views?

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);

How to draw multi colored line using xml in android

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.

why shape can not be appied to the textview in android

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);

Categories

Resources