0

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

1 Answer 1

1

<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 ...!

Sign up to request clarification or add additional context in comments.

2 Comments

VegeOSplash: I'm giving you the benefit of the doubt and marking this as the answer but I'm not even going to try it after learning about 9Patch. It looks to be a way better solution for custom shapes and backgrounds.
Sure !. 9 Patch is a good Solution for Rounded Corner because if work in all Device without any distortion.Android itself take care of scale and resize the edges based on user device screen resolution and DPI

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.