I have written a simple program to get familiar with the UI design in android. I have dynamically added texts inside a linear layout. When i debug the code i’m not able to see any error in the logcat, the code runs perfectly well. But i’m not able to see any text in my device. I’m using Galaxy S2.I have tried using the emulator also. But nothing showed up. I don’t have an idea why it is not showing any text in my device/emulator. Please help.
Here is my activity
public class ViewActivity extends Activity {
/** Called when the activity is first created. */
LinearLayout bar;
String myarray[]={"Foot Ball","Basket Ball","Cricket"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bar= (LinearLayout) findViewById(R.id.tag);// Map the Layout
bar.setVisibility(View.VISIBLE);
TextView bs_text[]= new TextView[myarray.length]; // Create TextView according to value in length variable;
for(int z=0;z< myarray.length ;z++)
{
try
{
bs_text[z] = (TextView) new TextView(this);
bs_text[z].setText( myarray [z]);
//bs_text[z].setTextSize(1);
bs_text[z].setBackgroundColor(android.graphics.Color.TRANSPARENT);
bs_text[z].setTextColor(android.graphics.Color.BLACK);
int height_in_pixels = bs_text[z].getLineCount() * bs_text[z].getLineHeight(); //approx height text
bs_text[z].setHeight(height_in_pixels);
//bs_text[z].setHeight(android.graphics.)
bar.addView(bs_text[z]);
}
catch(ArrayIndexOutOfBoundsException e)
{
Log.d("ArrayIndexOutOfBoundsException",e.toString());
}
}
}
}
here is my main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tag"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</LinearLayout>
set layoutparams to the textview as: