tags:

views:

75

answers:

2

Hi, I have an XMl file like below which I will use to set background for Textview:

row.xml

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

android:shape="rectangle">
The above Xml I will set as background for TextView in main.xml as below:

main.xml

<TextView
android:id="@+id/rowtext3"
android:text="Availablity"
android:layout_height="25px"
android:layout_width="60px"
android:textSize="10px"
android:textStyle="bold"
android:textColor="@color/black"
android:gravity="center"
android:background="@drawable/row"
/>


But I want this to do from code rather than Xml.I have done everything that I have done in Xml like font,width,Height,font dynamically through code , but not able to set Background that I mentioned in Xml file . How can we set content of Xml file as background to textview similar to how we set background as XML in main.xml.

In the code I have done like this:
t1=new TextView(this);
t1.setText(ed1.getText());
t1.setHeight(25);
t1.setWidth(60);
t1.setTextSize(10);
But I didn't find how to set background i.e. how to set XML content as background?
Can any one help me in sorting out this issue?
Thanks In Advance,

A: 

If I understand you correctly, findViewById(int id) from the Activity class is what you're looking for. When you have retrieved the view, you can set the background using setBackgroundResource(int id). The parameter id can be found in your generated R-file, e.g. findViewById(R.drawable.row).

hpe
t1.setBackgroundResource(findViewById(R.drawable.row));
Android_programmer_camera
Hi, I give like you suggested in my activity :t1.setBackgroundResource(findViewById(R.drawable.row)); It was showing error as "The method setBackgroundResource(int ) in the type view is not applicable for the arguments view. " How can I resolve this error in order to setBackground for TextView .
Android_programmer_camera
A: 

I think the method you're looking for is setBackgroundDrawable(Drawable d).

This will set the background using the given Drawable. So it would look something like this:

TextView t1 = (TextView) findViewById(R.id.rowtext3);
t1.setBackgroundDrawable(row);
Tyler
I tried the way u suggested as below: TextView tt=(TextView)findViewById(R.id.ttt);tt.setBackgroundDrawable(R.drawable.row); I am getting error as "The method setBackgroundDrawable(Drawable) in the type View is not applicable for the arguments (int)". How can I resolve this issue? But I am creating textview also dynamically in the activity file,there is no need for me to get reference from XML file.
Android_programmer_camera
That's because `setBackgroundDrawable()` does not accept an int as an argument. Try using the same technique but with the method `setBackgroundResource()` which accepts an int resource (Drawable object) ID.
Tyler
Super, It worked well. Thanks a lot
Android_programmer_camera
Hi,Can u give me sample code for how to create a RelativeLayout dynamically through code with 2 textviews one below other.I want to implement android:layout_below feature through code.
Android_programmer_camera