20

I am looking for a way to put a border for either textview or a button programmatically without using the setBackgroundResource method.

The goal I am trying to achieve here is, to change the background color dynamically but with a fixed border. When I use the setBackgroundResource method for the background border, the border doesn't remain after changing the background color programmatically.

Vishanth
  • 1,320
  • 3
  • 14
  • 26
  • I think you'd best use a 9 patch and use the setImageDrawable method. OR use the setBackground/setBackgroundDrawable with an xml drawable. (the difference is in the API level) – Phantômaxx Apr 19 '14 at 19:00
  • a border is a form of background. You can't have two backgrounds at the same time on the same view. One solution would be to embed your button in a framelayout, set the border on the button, (with everything transparent but the border), and set the color on the parent framelayout – njzk2 Apr 19 '14 at 20:20

2 Answers2

60

Simple example how could be this achieved:

activity_main.xml

<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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text="@string/hello_world" />

</RelativeLayout>

MainActivity.java

package com.exmple.test;

import android.app.Activity;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GradientDrawable gd = new GradientDrawable();
        gd.setColor(0xFF00FF00); // Changes this drawbale to use a single color instead of a gradient
        gd.setCornerRadius(5);
        gd.setStroke(1, 0xFF000000);
        TextView tv = (TextView)findViewById(R.id.textView1);
        tv.setBackground(gd);


    }

}
janzoner
  • 1,420
  • 1
  • 11
  • 19
0

I think this will help you.

 <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
      android:shape="rectangle">
      <gradient android:startColor="#FFFFFF" 
        android:endColor="#FFFFFF"
        android:angle="270" />
      <corners android:radius="3dp" />
      <stroke android:width="5px" android:color="#eecc68" />
    </shape>
<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="52dp"
        android:layout_marginTop="39dp"
        android:background="@drawable/button"
        android:text="Button" />
StealthRT
  • 10,108
  • 40
  • 183
  • 342
  • The problem here is android:background is set to a drawable style. Then when i programmatically change the colour, i dont get the other styles. – Vishanth Apr 19 '14 at 19:26