I am new to Android.I have created a dynamic textview which is displayed on emulator,
but i am not adding this textview using addView then how it displayed?
Here is my code:
package com.DynamicTextField;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class DynamicTextFieldActivity extends Activity {
/** Called when the activity is first created. */
private TextView tv ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = new TextView(this);
tv.setText("Dynamic Text View Test\n");
tv.setTextSize(18);
setContentView(tv);
}
}
What is the best way of adding textview?
Can anyone help!
Thanks.
Simple.
addViewcan be called to add aViewto aViewGroup(aViewcapable of containing otherViews). In your example, you are inside of anActivityand you are callingsetContentViewwhich sets the content to anything that extendsView.The normal way to use views in an activity is to either define your viewgroup in xml or programatically. If you do it programatically it would look something like:
Normally you would define the View in it’s own class. But if you’d like you can do this all in onCreate of your activity, and use “this” in place of context.