I have a Textview design in activity_main.xml which I want to use for displaying one textview below another. I got this code from stackoverlow itself and made a little change to use the existing Textview design. But the app closes when opened displaying nothing. I'm very new to Android Development.
XML code for TextView:
<TextView
android:id="@+id/textviews"
android:layout_width="match_parent"
android:layout_height="104dp"
android:textSize="30sp" />
Here is the code I got from Stackoverflow:
String[] textArray = {"One", "Two", "Three", "Four"};
LinearLayout linearLayout = new LinearLayout(this);
setContentView(linearLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL);
for( int i = 0; i < textArray.length; i++ )
{
TextView textView = new TextView(this);
textView.setText(textArray[i]);
linearLayout.addView(textView);
}
This code runs perfectly with no issues. But when I tried calling the existing textview design by using findViewById, the code builds but the app never opens. Here it is:
String[] textArray = {"One", "Two", "Three", "Four"};
LinearLayout linearLayout = new LinearLayout(this);
setContentView(linearLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL);
for( int i = 0; i < textArray.length; i++ )
{
TextView textView =(TextView) findViewById(R.id.textviews);
textView.setText(textArray[i]);
linearLayout.addView(textView);
}
How do I use the existing textview design in this code?