1

I get the color from the database, and it imposed in the TextView. Now I want to display the TextView in the shape of a circle, but I do not get the circle.

XML file in drawable cerchio_cat.xml

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


<corners
    android:bottomLeftRadius="30dp"
    android:bottomRightRadius="30dp"
    android:topLeftRadius="30dp"
    android:topRightRadius="30dp" />

<size
    android:height="60dp"
    android:width="60dp" />
</shape>

TextView

<TextView
        android:id="@+id/color_view"           
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:gravity="center_horizontal|center_vertical"
         android:background="@drawable/cerchio_cat"
        android:textSize="28sp" />

tvCerchio = (TextView) row.findViewById(R.id.color_view);
tvCerchio.setBackgroundColor(d.colore);
Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
user3608814
  • 561
  • 1
  • 8
  • 23

3 Answers3

0

You need to specify android:shape attribute, default value is "rectangle". Refer http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape

FreewheelNat
  • 2,907
  • 1
  • 20
  • 12
0

try this , it worked for me ..

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

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

<stroke
    android:width="2dp"
    android:color="#fff" />

</shape>

and for TextView

  android:background="@drawable/file.xml"
Rajesh Mikkilineni
  • 854
  • 10
  • 22
  • if I set the color dynamically, the TextView does not take the desired shape. – user3608814 Oct 14 '14 at 17:16
  • u can create all the colors .xml and u can assign based on the DataBase Value.. or check this , try to remove Solid from xml and try tv.setBackgroundColor(Color.RED); hope it works – Rajesh Mikkilineni Oct 14 '14 at 17:17
0

You can't set a background drawable in XML and then set the background color in code, the color will replace the drawable.

Since you want to assign the color dynamically from the database, it is better to create the background drawable in code:

ShapeDrawable background = new ShapeDrawable();
background.setShape(new OvalShape()); // or RoundRectShape()
background.getPaint().setColor(d.colore);

tvCerchio = (TextView) row.findViewById(R.id.color_view);
tvCerchio.setBackgroundDrawable(background);
Jaap van Hengstum
  • 4,494
  • 4
  • 30
  • 34
  • works, but `setBackgroundDrawable `is deprecated. How can I fix? Thanks so much – user3608814 Oct 14 '14 at 17:27
  • Use ```setBackground()``` but mind that this method only works from API level 16. See this post on how to check for API level: http://stackoverflow.com/questions/11947603/setbackground-vs-setbackgrounddrawable-android – Jaap van Hengstum Oct 14 '14 at 17:33