Create TextView dynamically Android sample

Example source code for Android Developers

// clickable TextView
public TextView createTextView(String sText, Context con){
 TextView b = null;
 try {
  b = new TextView (con);
  b.setTextSize(15.0f);
  b.setTextColor(Color.rgb( 0, 0, 200));
  b.setOnClickListener(this);
  b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
  LayoutParams.WRAP_CONTENT));
  b.setText(sText);
  //tr.addView(b, 60,30);
  } catch (Exception e) {
  e.printStackTrace();
 return b;
 }
return b;
}
/*****************/
public void onClick(View view) {
 try {
  String s = ((TextView) view).getText().toString();
 }
 catch (Exception e1) {
   e1.printStackTrace();
 }
}
/***********/
 
// if you want restore in TextView after chagne of orientation
// you have to put  code to Manifest.xml android:configChanges
 
activity android:name=".main"
android:label="@string/app_name"
android:configChanges="keyboardHidden|orientation" //this line important !!!!!!!
This entry was posted in Android Examples Code and tagged , , , , . Bookmark the permalink.


Comments are closed.

10,093 views