user10543278
user10543278

Reputation:

Xamarin - Set a background color and a border for a textview

I have a very simple question I think...

I have a textview and I want the TextView to have a border, so I did this:

<TextView
    android:text="Status"
    android:layout_width="wrap_content"
    android:layout_weight="16"
    android:layout_height="150px"
    android:id="@+id/txtStatus"
    android:textColor="#000000"
    android:textSize="50px"
    android:paddingTop="20px"
    android:paddingLeft="3dip"
    android:background="@drawable/list_divider"/>

and the list_divider.xml contains this:

<?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="#FF000000" />
</shape>

This code works, but in a c# class I set the background color using this command: txtStatus.SetBackgroundColor(Color.ParseColor("#FF746B"));

But when I use that command, it overwrites the border. How can I have both the border and the background color. I can't assign them in the list_divider.xml, because the color depends on the value of the TextView.

So just to be clear, how can I have both the border and the background color on the textview?

Upvotes: 1

Views: 2736

Answers (1)

Dmytro Ivanov
Dmytro Ivanov

Reputation: 1310

You could try to set your border like a foreground.

<TextView
    ...
    android:foreground="@drawable/list_divider"/>

It is possible to use background and foreground together.

<TextView
    ...
    android:foreground="@drawable/border"
    android:background="@color/colorPrimary"/>

Also you could programatically change background color like in your case.

Upvotes: 1

Related Questions