setBackGroundColor resets the shape of view - android

I want to have circular TextViews and I'm doing this by defining a shape drawable that works fine.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="#color/White"/>
</shape>
I want different colors of this TextView, so I do this in my code:
setBackgroundColor(getResources().getColor(R.color.Red));
But it resets the TextView shape to rectangle!
So, the question summarizes to What's the correct way to dynamically change only the color of a view?

It becomes a rectangle because you are changing the background of the view which of course is rectangle shaped, you should change the solid color but i don't think this possible to do programmatically
the only way is to do like this:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="#color/Red"/>
</shape>
and:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="#color/green"/>
</shape>
then:
setBackgroundResource(R.color.green);

You can change background of shape of a view by programmatically by doing like this:
GradientDrawable drawable = (GradientDrawable)textView.getBackground();
drawable.setColor(getResources().getColor(android.R.color.darker_gray));

Use
setBackground(drawable);
Instead of
setBackgroundColor(getResources().getColor(R.color.Red));

Related

Change android textview border color without changing background

I am setting background of a textview dynamically, based on some conditions as
textview.setBackgroundResource(R.drawable.attempted_question_border);
OR
textview.setBackgroundResource(R.drawable.skipped_question_background);
Xml for background is
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid
android:color="#color/answered_que_bg" >
</solid>
<stroke
android:width="1dp"
android:color="#color/answered_que_bg" >
</stroke>
<corners
android:radius="2dp">
</corners>
</shape>
It sets background color 'answered_que_bg' and border color 'answered_que_bg' OR background color 'skipped_question_background' and border color 'skipped_question_background'. So far so good. Now I need to change only border color of this textview keeping background color same as it had. I tried with changing background with below xml.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="1dp"
android:color="#color/bookmark_color" >
</stroke>
<corners
android:radius="2dp">
</corners>
</shape>
It changes border color as desired but background color is also lost.
Take your drawable xml with layer list like that
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/shape">
<shape
android:shape="rectangle" >
<solid
android:color="#color/colorPrimary" >
</solid>
<stroke
android:width="1dp"
android:color="#color/colorPrimary" >
</stroke>
<corners
android:radius="2dp">
</corners>
</shape>
</item>
</layer-list>
code pro-grammatically change stroke also the background
LayerDrawable shape = (LayerDrawable) ContextCompat.getDrawable(demo.this,R.drawable.drawtext);
GradientDrawable gradientDrawable = (GradientDrawable) shape.findDrawableByLayerId(R.id.shape);
// if you want to change the color of background of textview dynamically
//gradientDrawable.setColor(ContextCompat.getColor(demo.this,R.color.colorAccent));
// This is mangage the storke width and it's color by this shape.setStroke(strokeWidth,color);
gradientDrawable.setStroke(2,ContextCompat.getColor(demo.this,R.color.colorAccent));
text2.setBackground(shape);
With your code without change color of stroke
With My code change color of stroke and also you can change the background progrmatically in comment code
I have done that by putting the TextView into a LinearLayout, where the TextView would be centered with certain margins on either side, then I'd change the background color of the LinearLayout, without changing the background color of the textview, and then it'd appear as if it's border color has changed.

Creating android shape that keeps proportions

How do I create an XML ressource shape that looks like a perfect circle no matter which object it is applied to? When I try to add it as a background to a button, it becomes a oval in stead of a circle.
try this:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="ring" android:innerRadiusRatio="5"
android:visible="true" android:thickness="4dp"
>
<stroke android:color="#654321"
android:width="2dp"/>
<solid android:color="#123456"/>
</shape>

How to create a custom TextView Background Android?

I want to create a background for my TextView to look exactly like this one(Yellow Box).
After I create the image how do I go about setting the background to the TV?
Thanks!
for that you create simple image . then Convert in to nine patch here
after set background to that drawable
Example of Nine patch Image
Or you could create a shape drawable.
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<solid android:color="#color/Yellow" />
<stroke android:width="#dimen/" android:color="#color/"/>
<corners android:radius="#dimen/"/>
<gradient
android:startColor="#color/"
android:centerColor="#color/"
android:endColor="#color/"
android:centerX="0.5"
android:centerY="0.5"
android:gradientRadius="100"
android:type="linear" />
</shape>
It can also be done using shape attribute.
I created a similar one for myself using the below.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle" >
<solid android:color="#FFFFFF" /> //This should be the yellow color you want
<corners android:radius="5dp" /> //for rounded corners
<stroke
android:width="2dp"
android:color="#color/color_dark_blue" /> //for border if you wish any
</shape>
Create it as an xml file inside drawable (say myTextViewBg.xml).
Then add it as a background to your TextView as,
yourTextView:background="#drawable/myTextViewBg.xml"
You can set background of your textview
android:background="#drawable/myResouce"
or using java
mTextView.setBackgroundResource(R.drawable.myResouce);
But in your case create a relative layout then put an imageView and Textview inside it and adjust it according to your view.
In imageView use image which you in your question, expect text to change. And in textView use changeable text. Which you may change pragmatically as well.

How to alter LayerDrawable within ImageView?

I'm adding a couple of ImageView to a LinearLayout programmatically, those ImageView have its src set to R.drawable.rectangle.
I'm trying to create a rectangle with solid color that has a border only to the left.
R.drawable.rectangle looks like this:
<?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="#000000" />
<stroke android:width="1dp" android:color="#999" />
</shape>
</item>
<item
android:top="0dp" android:left="1dp" android:bottom="0dp" android:right="0dp">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<solid android:color="#283BCB"/>
<size android:width="100dp" android:height="8dp" />
</shape>
</item>
</layer-list>
In my activity, I'm inflating the layout that contains the LinearLayout and I'm adding ImageViews to it programatically, that is all working. The problem is that I don't manage to manipulate the rectangle width. If I do:
imageView.getLayoutParams().width = 10;
Rectangles need to be different sizes and colors. However if I do it this way, rectangles get deformed weirdly, this doesn't happen If I don't use a layer-list, but only a shape (but that way I cannot add a left border).
So I'm guessing I need to get LayerDrawable, then get GradientDrawable and change it. However I'm not having any luck achieving this.
LayerDrawable layer = (LayerDrawable)imageView.getDrawable();
GradientDrawable rectangle = (GradientDrawable)layer.getDrawable(1);
// rectangle.setBounds
// rectangle.setColor NOT working
Any hints? thanks

android remove default border for a view with rounded corners

I have a layout with 2-3 childs. Set linear layout backgroud to a following drawable using android:background property.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#373949"/>
<stroke android:width="3dip" android:color="#FFF"/>
<corners android:radius="30dip" />
<padding android:left="10dip" android:top="10dip" android:right="10dip" android:bottom="10dip" />
</shape>
But when set radius to 30dip, rounded corners getting displayed, but back to
layout default gray colored border with rectangular shape is displayed.
Is there any way to get rid of that ?
Thanks in advance
Try this code
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFFFF"/>
<corners android:radius="15px"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
once you change the background of the view, this drawable will no longer be active to draw UI, hence the default layout of view will be applicable, so if you want gray layout to also be rounded, make another drawable, and set that drawable instead of gray color.
That rounded corner border that you see is of the parent of layout that you have changed background.If your cusytomized background belongs to your activity than the grey color you see belongs to system. You can use Hierarchy Viewer. To see it in detail.
you can also refer this for your reference.

Categories

Resources