Question

I have created a RelativeLayout in an XML that contains a TextView like this:

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView3"
    android:layout_below="@+id/button1"
    android:layout_marginTop="28dp"
    android:background="@drawable/rounded_edges"
    android:textSize="25sp" />

so that a circle will be drawn under the text. The rounded_edges.xml is defined as follows:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="oval">
    <corners android:radius="10dip"/>
    <stroke android:color="#c00000" android:width="2dip"/>
    <solid android:color="#ffffff"/>
</shape>

Then I needed more textViews depending on a number entered by user. Each textView will show a number with a circle below. I succeeded in creating the textViews programmatically in the Java code as below, except for the circle:

final TextView textView5 = (TextView) findViewById(R.id.textView5);

// size stores the number entered by user
RelativeLayout myLayout =  (RelativeLayout)findViewById(R.id.layout1);

// Creating a new TextView
TextView[] tv = new TextView[size+1];
TextView temptv;

for (i = 1; i <= size; i++) {
    temptv = new TextView(MainActivity.this);
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    if (i > 1) {
        lp.addRule(RelativeLayout.BELOW, R.id.textView5);
        lp.addRule(RelativeLayout.RIGHT_OF, tv[i-1].getId());
    }
    else {
        lp.addRule(RelativeLayout.BELOW, R.id.textView5);
        lp.addRule(RelativeLayout.ALIGN_LEFT, R.id.textView5);
    }

    // width of Text
    temptv.setWidth(50);
    // size of Text
    temptv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25);

    temptv.setLayoutParams(lp);
    myLayout.addView(temptv, lp);

    answer = "<font color='#8000ff'>" + mynum[i] + "</font>";
    answer = answer + "&nbsp;&nbsp;&nbsp;";

    // Update the label with our dynamic answer
    temptv.setText(Html.fromHtml(answer));
    tv[i] = temptv;
    tv[i].setId(i);

}
setContentView(myLayout);

Is there a way to convert the android:background="@drawable/rounded_edges" to Java code so that I can still use the rounded_edges.xml to draw the circles? Or do I have to draw it completely from scratch in Java?

Was it helpful?

Solution 2

you can add this code before tv[i] = temptv;

temptv.setText(Html.fromHtml(answer));
temptv.setBackgroundResource(R.drawable.rounded_edges);

hope it helps

OTHER TIPS

U can set textView Background as txt.setBackgroundResources(); or else try this This Not the Exact what You are looking for but I can say it will help you to find the Solution.

 private int xCord, yCord ;
 private final Paint dotPaint  = new Paint();
   private void init()
 {
  dotPaint.setAntiAlias(true);    
  dotPaint.setStyle(Paint.Style.FILL);
  dotPaint.setColor(Color.parseColor("#FFAC4A70"));// Change color as per Ur...
 }
 private static final int DOT_RADIUS = 16;//change it as per requirement
 protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if(isEventCell)
    {
         xCord = getWidth() / 2;
         yCord = getHeight() - DOT_RADIUS - 8;   
        canvas.drawCircle(xCord , yCord,DOT_RADIUS , dotPaint);
    }



}

and then try to call init(); in method constructor where your textVies is there....

    public class CalendarCellView extends TextView {

private final Paint dotPaint  = new Paint();
private boolean isEventCell ;
private int xCord, yCord ;
private static final int DOT_RADIUS = 16;


 private static final int[] STATE_SELECTABLE = {
  R.attr.state_selectable
 };
 private static final int[] STATE_CURRENT_MONTH = {
  R.attr.state_current_month
 };
private static final int[] STATE_TODAY = {
  R.attr.state_today
 };
 private static final int[] STATE_RANGE_FIRST = {
  R.attr.state_range_first
 };
 private static final int[] STATE_RANGE_MIDDLE = {
  R.attr.state_range_middle
 };
 private static final int[] STATE_RANGE_LAST = {
  R.attr.state_range_last
 };

 private boolean isSelectable = false;
  private boolean isCurrentMonth = false;
private boolean isToday = false;
 private RangeState rangeState = RangeState.NONE;

public CalendarCellView(Context context) {
super(context);
init();
 }

  public CalendarCellView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
 }

 public CalendarCellView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
 }

  public void setSelectable(boolean isSelectable) {
   this.isSelectable = isSelectable;
   refreshDrawableState();
  }

 private void init()
 {
  dotPaint.setAntiAlias(true);    
  dotPaint.setStyle(Paint.Style.FILL);
  dotPaint.setColor(Color.parseColor("#FFAC4A70"));//("#FFAC4A70"));
  }

  public void setCurrentMonth(boolean isCurrentMonth) {
this.isCurrentMonth = isCurrentMonth;
refreshDrawableState();
 }

 public void setToday(boolean isToday) {
this.isToday = isToday;
refreshDrawableState();
 }

 public void setRangeState(MonthCellDescriptor.RangeState rangeState) {
   this.rangeState = rangeState;
   refreshDrawableState();
 }

 public void isEventCell(boolean isEventCell)
 {
  this.isEventCell = isEventCell;
 }

 @Override protected int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 4);

if (isSelectable) {
  mergeDrawableStates(drawableState, STATE_SELECTABLE);
}

if (isCurrentMonth) {
  mergeDrawableStates(drawableState, STATE_CURRENT_MONTH);
  }

  if (isToday) {
   mergeDrawableStates(drawableState, STATE_TODAY);
  }

if (rangeState == MonthCellDescriptor.RangeState.FIRST) {
  mergeDrawableStates(drawableState, STATE_RANGE_FIRST);
  } else if (rangeState == MonthCellDescriptor.RangeState.MIDDLE) {
    mergeDrawableStates(drawableState, STATE_RANGE_MIDDLE);
     } else if (rangeState == RangeState.LAST) {
    mergeDrawableStates(drawableState, STATE_RANGE_LAST);
     }
     return drawableState;
   }

   @Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if(isEventCell)
    {
         xCord = getWidth() / 2;
         yCord = getHeight() - DOT_RADIUS - 8;   
        canvas.drawCircle(xCord , yCord,DOT_RADIUS , dotPaint);
      }



    }



      }

Try this

Drawable dr = getResources().getDrawable(R.drawable.rounded_edges);
temptv.setBackground(dr);

There is method setBackgroundResource. To find out analogue of xml attribute just look at documentation where you can find table of XML Attributes:

android:background > setBackgroundResource(int) > A drawable to use as the background.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top