2

I'm trying to change the border colour of my TextView programmatically, depending on the if condition in my activity. Here's my ViewContact.java code:

if (bob == 0) {
  //change colour depending on value

  LayerDrawable layerDrawable = (LayerDrawable) ContextCompat
      .getDrawable(ViewContact.this,R.drawable.textboxes);
  GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable
      .findDrawableByLayerId(R.id.textbox_shape);
  gradientDrawable.setColor(Color.parseColor("#DA850B")); // change color
}

if (bob == 1) {

  LayerDrawable layerDrawable = (LayerDrawable) ContextCompat
      .getDrawable(ViewContact.this,R.drawable.textboxes);
  GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable
      .findDrawableByLayerId(R.id.textbox_shape);
  gradientDrawable.setColor(Color.parseColor("#0A7FDA")); // change color

}

if (bob == 2) {

etc...

}

I've looked here Changing color in a shape inside a layer-list programmatically and here Android change color stroke (border) programmatically and elsewhere for a solution but can't get it working.

Here is the xml for my TextView in the ViewContact.java activity, activity_view_contact.xml:

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight=".5"
            android:background="@drawable/textboxes"
            />

And here is textboxes.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--space between each text box-->
    <item
        android:top="3dp"
        android:id="@+id/textbox_shape"
        >
        <shape android:shape="rectangle">
            <!--formatting of colour inside the box-->
            <solid android:color="@android:color/transparent" />
            <!--formatting of lines on the box-->
            <stroke
                android:width="1dip"
                 />
            <corners android:radius="3dp"/>
            <!--text formatting in the box-->
            <padding
                android:textSize="30dp"
                android:top="15dp"
                android:bottom="1dp"
                android:left="20dp"
                android:right="20dp"/>
        </shape>
    </item>
</layer-list>
Cœur
  • 37,241
  • 25
  • 195
  • 267
CHarris
  • 2,693
  • 8
  • 45
  • 71
  • 1
    Have u check this https://stackoverflow.com/a/39489442/7666442 – AskNilesh Jan 03 '19 at 11:53
  • @NileshRathod Looked at it. But I `cannot resolve symbol` with this line, the name of my `textView` : `categoryname.setBackground(layerDrawable);` – CHarris Jan 03 '19 at 12:46

3 Answers3

2

You are changing the color of the drawable that you load but not the one used in the TextView. Add the following line to your code to set the background drawable in the TextView:

findViewById(R.id.textView1).setBackground(layerDrawable);

You can also get the background for the TextView directly with

LayerDrawable layerDrawable = (LayerDrawable) findViewById(R.id.textView1).getBackground();

Here is a set of code to change the background and the stroke color:

LayerDrawable layerDrawable = (LayerDrawable) findViewById(R.id.textView1).getBackground();
GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable
    .findDrawableByLayerId(R.id.textbox_shape);
// Change background color
gradientDrawable.setColor(Color.parseColor("#DA850B"));
// Change stroke color. (Assumes 5px stroke width.)
gradientDrawable.setStroke(5, Color.parseColor("#FF0000"));
Cheticamp
  • 61,413
  • 10
  • 78
  • 131
  • @CHarris What isn't working? Are you getting an error or is something happening/not happening? – Cheticamp Jan 03 '19 at 13:55
  • I am getting the message, `Cannot resolve symbol 'layerDrawable'` when I try `findViewById(R.id.textView1).setBackground(layerDrawable);` – CHarris Jan 03 '19 at 14:09
2

TextView - add border in XML or code

Steps for Android Studio 3.5.2 - app running on Android 9 Pie

Create new drawable

  • in project tree

  • open res folder

  • open drawable folder

  • Right click on drawable

  • Choose New > Drawable resource file

  • New resource screen appears

  • enter name

  • textview_border.xml

  • leave all other options alone

  • Press Ok

Creates default drawable

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

</selector>

Replace the root < selector > with this < shape />.. xml but keep exact name space generated by Android Studio - if different from below:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <!--FILL    -->
    <solid android:color="#fafbfb" />

    <!--    BORDER-->
    <stroke android:width="1dp"
            android:color="#FFFFFF" />

</shape>

You can now use this drawable shape as a background

Given any textview

Here we set the background color to Android system color white in XML

android:background="@android:color/white"

<TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:background="@android:color/white"
        android:text="SOME TEXT"
        android:textAlignment="center"
        android:textColor="@color/colorAppColorAppDark0"
        android:textSize="@dimen/app_textSize_mediumToSmall"
       .../>

Now we can use the drawable instead

android:background="@drawable/textview_border"

<TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:background="@drawable/textview_border"
        android:text="SOME TEXT"
        android:textAlignment="center"
        android:textColor="@color/colorAppColorAppDark0"
        android:textSize="@dimen/app_textSize_mediumToSmall"
       ..layout commands etc./>

Or you can add it programatically

TextView textView1 = activity.findViewById(R.id.textView1);

//--------------------------------------------------------------------------------------
//see res/drawable/textview_border.xml
textView1.setBackgroundResource(R.drawable.textview_border);

Set background to drawable in XML - Design Mode

  • In Design mode

  • List item

  • Click on your textView

  • Find background in Declared Attributes or scroll down to All Attributes to add it.
  • In the far right of the row click the small bar to the right of the field
  • background chooser screen appears
  • On left choose type : Drawable instead of Color
  • Find textview_border
  • Press OK

enter image description here

Sets the android:background="@drawable/textview_border"

<TextView
        android:id="@+id/fixture_list_constraintlayout_textView_fixturetypecode"
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:background="@drawable/textview_border"
        android:text="XXX"
        android:textAlignment="center"
        android:textColor="@android:color/white"
        android:textSize="@dimen/app_textSize_mediumToSmall"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
brian.clear
  • 5,277
  • 2
  • 41
  • 62
1

You want to change Stroke color (Border):

GradientDrawable gradientDrawable = (GradientDrawable)btn_submit.getBackground();
gradientDrawable.setStroke(2, Color.GREEN);

it works.. check it once..

LundinCast
  • 9,412
  • 4
  • 36
  • 48
satyan_android
  • 364
  • 4
  • 15
  • I am trying this but doesn't work, there is no change: `gradientDrawable.setStroke(1, Color.parseColor("#DA850B")); ` – CHarris Jan 03 '19 at 13:33
  • try this.. get it from colors.m=xml.. getResources().getColor(R.color.)-- --Color.parseColor("#DA850B")); -- For TextViews or Buttons or EditTexts.. – satyan_android Jan 03 '19 at 16:45