-3

I have the following code in my Main-Activity:

LinearesLayout = (LinearLayout) findViewById(R.id.linearlayout);
TextView textView = new TextView(this);
textView.setBackgroundResource(R.color.colorPrimary);
LinearesLayout.addView(textView);

How can I set the corners of the TextView to round?

takendarkk
  • 3,347
  • 8
  • 25
  • 37
  • 1
    You should try to find a way to do it yourself, first, and show us what you tried if it didn't work and you need help understanding why. Don't ask people to do work for you – Tal Mantelmakher Aug 17 '20 at 13:43
  • @TalMantelmakher I've already tried to find a command for it on the Internet. But how can I show you something if I do not know which command to use? – Nils Schmidt Aug 17 '20 at 14:06
  • It should look somethink like this: textView.setradius="5dp". But i cant finde somethink like this... – Nils Schmidt Aug 17 '20 at 14:07
  • For example: https://stackoverflow.com/questions/62317455/how-to-apply-shapeappreanace-in-edittext-and-textview-using-material-design-in-a/62318842#62318842 – Gabriele Mariotti Aug 17 '20 at 16:15

1 Answers1

0

Create an XML file in res\drawable folder - rounded_corners.xml like this:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <solid android:color="#ff0000"/>
  <corners android:radius="4dp" />
  <padding
    android:top="5dp"
    android:left="5dp"
    android:bottom="5dp"
    android:right="5dp" />
</shape> 

Replace this line:

textView.setBackgroundResource(R.color.colorPrimary);

to this:

textView.setBackgroundResource(R.drawable.rounded_corners);

Tal Mantelmakher
  • 994
  • 1
  • 7
  • 23