Dean Blakely
Dean Blakely

Reputation: 3585

Creating custom TextView programmatically

I'm writing a chat app. I'm creating textviews, in code, for each chat. This involved the "addChat" method and the chatshape.xml pasted below. It all works perfectly except that the corners are not rounded on the textbox. The tv.setBackgroundResource(R.drawable.chatshape) is having no effect.

private void addChat(String chat, String when,  Boolean mine) 
{
    int leftMargin;
    TextView tv = new TextView(this);
    tv.setLayoutParams(new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.MATCH_PARENT)); 

    tv.setTextSize(2,20);
    tv.setPadding(10, 4, 10, 4);
    tv.setBackgroundResource(R.drawable.chatshape);
    tv.setText(chat);
    if (mine) {
        leftMargin = 10;
        tv.setBackgroundResource(R.color.white);
        tv.setTextColor(Color.BLUE);
    }
    else {
        leftMargin = 25;
        tv.setBackgroundResource(R.color.gray);
        tv.setTextColor(Color.WHITE);
    }
    this.messageScroll.addView(tv);
    final ViewGroup.MarginLayoutParams lpt = (ViewGroup.MarginLayoutParams) tv.getLayoutParams();

    lpt.setMargins(leftMargin,4,10,4);

}

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

  <corners
         android:bottomRightRadius="20px" 
         android:bottomLeftRadius="20px" 
         android:topLeftRadius="20px" 
         android:topRightRadius="20px"/>
</shape>

Upvotes: 0

Views: 381

Answers (1)

VegeOSplash
VegeOSplash

Reputation: 204

<corners android:radius="8dp" />

<solid android:color="#FFFFFF" />

<stroke
    android:width="1dp"
    android:color="#848482" />

  <padding
    android:bottom="3dp"
    android:left="3dp"
    android:right="3dp"
    android:top="3dp" />  

it works ...!

Upvotes: 1

Related Questions