Okhelp.cz

Recepty, články, nápady, programování. Dříve dum-zahrada, finance, internet a know-how.okhelp.cz Pro lepší výsledky hledání používejte i diakritiku.

Create TextView dynamically Android sample


AD MOB

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 !!!!!!!


397LW NO topic_id




AD

Další témata ....(Topics)


264

String Array of Array Java Example | string-array-of-array-java-example


Set, get string , array of array of strings, Java example code

	public final static String[][]_arArOfString_1 = {
		new String[] {"bla","bla","hello world!"},
		new String[] {},
		new String[] {},
		new String[] {},
		new String[] {},
		new String[] {}
	};
String sOut = _arArOfString_1[0][2]; // hello world!
301

sqlite database get path Android | sqlite-database-get-path-android



Context context = getApplicationContext();
String DB_PATH = "/data/data/"
                + context.getPackageName()
                + "/databases/";
72

While cycle loop in Java example | while-cycle-loop-in-java-example


While cycle Java basic example with array of strings.

MainClass.java

public class MainClass {
	public static void main(String[] arg) {
		String[] arrayOfString = { "Hello", "people", "hello", "world!" };
                int i = 0;
		while ( i < arrayOfString.length ){
			System.out.println(arrayOfString[i]);
			i++;
		}
	}
}
74

Continue statement Java example | continue-statement-java-example


For and continue statement in Java.

public class MainClass {
	public static void main(String[] arg) {
		String[] arrayOfString = { "Hello", "people", "hello", "world!" };

		for (int i = 0; i < arrayOfString.length; i++){
			if(arrayOfString[i].equals("hello"))
				continue; // skip to for
			System.out.println(arrayOfString[i]);
		}
	}
}
/*
Hello
people
world!
*/
138

Draw text drawText Android basic example | draw-text-drawtext-android-basic-example


drawText(), setColor(), setTextSize(), setTextAlign()

public class ApokusActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new SampleView(this));
	}

	private static class SampleView extends View {

		// CONSTRUCTOR
		public SampleView(Context context) {
			super(context);
			setFocusable(true);

		}
		@Override
		protected void onDraw(Canvas canvas) {
			Paint paint = new Paint();
			
            paint.setColor(Color.YELLOW);
            paint.setTextSize(60);
            //paint.setTextAlign(Paint.Align.CENTER);

            canvas.drawText("Hello world!", 0, 50, paint);
		}

	}
}



Editace: 2011-10-22 07:28:43
Počet článků v kategorii: 397
Url:create-textview-dinamically

AD
11 Share
Share
Tweet