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);
}
}
}
396LW NO topic_id
AD
Další témata ....(Topics)
Insert into your default start up activity tag inten-filter tag with action MAIN and category LAUNCHER
AndroidManifest.xml
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="//schemas.android.com/apk/res/android"
package="com.example.blabol"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Example from SDK C:\Program Files\Android\android-sdk-windows\samples\android-10\ApiDemos\src\com\example\android\apis\text\Link.java
Source: //developer.android.com/resources/browser.html?tag=sample
License: //www.apache.org/licenses/LICENSE-2.0
1.) Automatically linkifies using android:autoLink="all"
2.) Link text by setMovementMethod
3.) Link as html code using Html.fromHtml()
4.) Link string by SpannableString
Source: //developer.android.com/resources/browser.html?tag=sample
License: //www.apache.org/licenses/LICENSE-2.0
1.) Automatically linkifies using android:autoLink="all"
// res/values/strings.xml
<string name="link_text_auto"><b>text1:</b> This is some text. In
this text are some things that are actionable. For instance,
you can click on //www.google.com and it will launch the
web browser. You can click on google.com too. And, if you
click on (415) 555-1212 it should dial the phone.
</string>
// main.xml
<!-- text1 automatically linkifies things like URLs and phone numbers. -->
<TextView xmlns:android="//schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:autoLink="all"
android:text="@string/link_text_auto"
/>
2.) Link text by setMovementMethod
// MainActivity.java onCreate
/*Be warned that if you want a TextView with a key listener or movement method not to be focusable, or if you want a TextView without a key listener or movement method to be focusable, you must call setFocusable(boolean) again after calling this to get the focusability back the way you want it. */
TextView t2 = (TextView) findViewById(R.id.text2);
t2.setMovementMethod(LinkMovementMethod.getInstance());
// main.xml
<!-- text2 uses a string resource containing explicit <a> tags to
specify links. -->
<TextView xmlns:android="//schemas.android.com/apk/res/android"
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/link_text_manual"
/>
//strings.xml
<string name="link_text_manual"><b>text2:</b> This is some other
text, with a <a href="//www.google.com">link</a> specified
via an <a> tag. Use a "tel:" URL
to <a href="tel:4155551212">dial a phone number</a>.
</string>
3.) Link as html code using Html.fromHtml()
// MainActivity.java onCreate
TextView t3 = (TextView) findViewById(R.id.text3);
t3.setText(
Html.fromHtml(
"<b>text3:</b> Text with a " +
"<a href="//www.google.com">link</a> " +
"created in the Java source code using HTML."));
t3.setMovementMethod(LinkMovementMethod.getInstance());
4.) Link string by SpannableString
SpannableString ss = new SpannableString(
"text4: Click here to dial the phone.");
ss.setSpan(new StyleSpan(Typeface.BOLD), 0, 6,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(new URLSpan("tel:4155551212"), 13, 17,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView t4 = (TextView) findViewById(R.id.text4);
t4.setText(ss);
t4.setMovementMethod(LinkMovementMethod.getInstance());
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();
canvas.drawColor(Color.YELLOW);
paint.setFilterBitmap(true);
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.flower_blue);
int targetWidth = bitmapOrg.getWidth() * 2;
int targetHeight = bitmapOrg.getHeight() * 2;
Bitmap bmp = Bitmap.createBitmap(targetWidth, targetHeight,Bitmap.Config.ARGB_8888);
RectF rectf = new RectF(0, 0, targetWidth, targetHeight);
Canvas c = new Canvas(bmp);
Path path = new Path();
path.addRect(rectf, Path.Direction.CW);
c.clipPath(path);
c.drawBitmap( bitmapOrg, new Rect(0, 0, bitmapOrg.getWidth(), bitmapOrg.getHeight()),
new Rect(0, 0, targetWidth, targetHeight), paint);
Matrix matrix = new Matrix();
matrix.postScale(1f, 1f);
Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, targetWidth, targetHeight, matrix, true);
int h = bitmapOrg.getHeight();
canvas.drawBitmap(bitmapOrg, 10,10, paint);
canvas.drawBitmap(resizedBitmap, 10,10 + h + 10, paint);
}
}
}
Update TextView from TimerTask, Handler, schedule, run, cancel TimerTask, Android example
public class TimerActivity extends Activity {
TimerTask mTimerTask;
final Handler handler = new Handler();
Timer t = new Timer();
TextView hTextView;
TableRow hTableRow;
Button hButton, hButtonStop;
private int nCounter = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hTextView = (TextView)findViewById(R.id.idTextView);
hButton = (Button)findViewById(R.id.idButton);
hButton.setOnClickListener(mButtonStartListener);
hButtonStop = (Button)findViewById(R.id.idButtonStop);
hButtonStop.setOnClickListener(mButtonStopListener);
} // end onCreate
View.OnClickListener mButtonStartListener = new OnClickListener() {
public void onClick(View v) {
doTimerTask();
}
};
View.OnClickListener mButtonStopListener = new OnClickListener() {
public void onClick(View v) {
stopTask();
}
};
public void doTimerTask(){
mTimerTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
nCounter++;
// update TextView
hTextView.setText("Timer: " + nCounter);
Log.d("TIMER", "TimerTask run");
}
});
}};
// public void schedule (TimerTask task, long delay, long period)
t.schedule(mTimerTask, 500, 3000); //
}
public void stopTask(){
if(mTimerTask!=null){
hTextView.setText("Timer canceled: " + nCounter);
Log.d("TIMER", "timer canceled");
mTimerTask.cancel();
}
}
}
File -> Settings -> Keymap you can do select Eclipse keymap from drop down menu
Ctrl+Shift+A (Help | Find Action on the main menu) and start typing the name of the action. Choose the desired action from the suggestion list.
Ctrl+Click (on Windows) or Cmd+Click (on MacOS) a tab in the editor to navigate to any part of the file path. Select the necessary element in the drop-down, and the corresponding file path opens in an external browser (e.g., in the Explorer, if your OS is Windows).
Ctrl+F find
Refactor | Rename: Shift+F6 ( +F6)
Find and replace: CTRL + R
Go to variable declaration: CTRL + B
Ctrl+Shift+I (View | Quick Definition)
Ctrl+Alt+F7 (Edit | Find | Show Usages in the main menu)
Ctrl+Shift+F find in project
Ctrl+Shift+F7 (Edit | Find | Highlight Usages in File) to quickly highlight usages of some variable in the current file.
Use F3 and Shift+F3 keys to navigate through highlighted usages.
Press Escape to remove highlighting.
Ctrl+W (extend selection) in the editor selects the word at the caret and then selects expanding areas of the source code. For example, it may select a method name, then the expression that calls this method, then the whole statement, then the containing block, etc. You can also select the word at the caret and the expanding areas of the source code by double-clicking the target areas in the editor.
To search through the classes, files, tool windows, actions, settings, and symbols of your project by double pressing the Shift key
Ctrl+P (View | Parameter Info) e.g. localURL = new URL(localUrlString); select localUrlString and press Ctrl + P
Show html documentation tooltips: CTRL + Q
Comment uncomment single line: CTRL + /
Comment uncomment selected text: CTRL + SHIFT + /
Code completion (Ctrl+Space)
Alt key and mouse pointer dragging for column selection
If nothing is selected in the editor, and you press Ctrl+C, then the whole line at caret is copied to the clipboard.
Ctrl + Delete delete word from caret to end.
Ctrl + Backspace delete word from caret to start.
Duplicate line: CTRL + D
To compare two directories,or jar files, select one or both of them in the Project view and press Ctrl+D.
Editor Delete Line: CTRL + Y
Move line UP, DOWN. Select a code fragment and press Ctrl+Shift+Up or Ctrl+Shift+Down.
To open any class or file in the editor at the desired line, press Ctrl+Shift+N (Navigate | File), start typing the name, and choose the one from the suggestion list. Then type the colon (:) and a line number
Ctrl+Shift+Enter to complete a current statement such as if, do-while, try-catch, return (or a method call)
Alt+Enter to invoke a quick fix or intention action, press the right arrow key to reveal the list of additional options.
To quickly review your recent changes to the project: Alt+Shift+C
Code Completion, you can accept the currently highlighted selection in the popup list with the period character (.), comma (,), semicolon (;), space and other characters.
Quickly move between methods in the editor: Alt+Up and Alt+Down
Ctrl+Shift+J shortcut joins two lines into one and removes unnecessary space to match your code style
Ctrl+Shift+V to choose and insert recent clipboard contents into the text
Inheritance hierarchy for a selected class: Ctrl+H
Right-clicking on a breakpoint marker you can quickly enable/disable the breakpoint or adjust its properties.
Evaluate the value of any expression while debugging the program, select its text in the editor (you may press a Ctrl+W a few times to efficiently perform this operation) and press Alt+F8.
Quickly evaluate the value of any expression while debugging the program, hold Alt and click this expression to see its value and calculate it, call a method, etc.
You can use the Extract Variable refactoring even on incomplete statements. Just select the necessary block in the editor and press Ctrl+Alt+V.
To open any particular method or field in editor: Ctrl+Alt+Shift+N
Add unimplemented methods: CTRL + I
Override methods: CTRL + O
Format code: CTRL + ALT + L
Show project: ALT + 1
Show logcat: ALT + 6
Hide project - logcat: SHIFT + ESC
Build: CTRL + F9
Build and Run: CTRL + F10
Alt+Shift+F10 access the Run/Debug dropdown on the main toolbar
Collapse all: CTRL + SHIFT + NumPad +
Expand all: CTRL + SHIFT + NumPad -
Ctrl+Shift+A (Help | Find Action on the main menu) and start typing the name of the action. Choose the desired action from the suggestion list.
Ctrl+Click (on Windows) or Cmd+Click (on MacOS) a tab in the editor to navigate to any part of the file path. Select the necessary element in the drop-down, and the corresponding file path opens in an external browser (e.g., in the Explorer, if your OS is Windows).
Find - Rename
Ctrl+F find
Refactor | Rename: Shift+F6 ( +F6)
Find and replace: CTRL + R
Go to variable declaration: CTRL + B
Ctrl+Shift+I (View | Quick Definition)
Ctrl+Alt+F7 (Edit | Find | Show Usages in the main menu)
Ctrl+Shift+F find in project
Ctrl+Shift+F7 (Edit | Find | Highlight Usages in File) to quickly highlight usages of some variable in the current file.
Use F3 and Shift+F3 keys to navigate through highlighted usages.
Press Escape to remove highlighting.
Ctrl+W (extend selection) in the editor selects the word at the caret and then selects expanding areas of the source code. For example, it may select a method name, then the expression that calls this method, then the whole statement, then the containing block, etc. You can also select the word at the caret and the expanding areas of the source code by double-clicking the target areas in the editor.
To search through the classes, files, tool windows, actions, settings, and symbols of your project by double pressing the Shift key
Ctrl+P (View | Parameter Info) e.g. localURL = new URL(localUrlString); select localUrlString and press Ctrl + P
Show html documentation tooltips: CTRL + Q
Edit
Comment uncomment single line: CTRL + /
Comment uncomment selected text: CTRL + SHIFT + /
Code completion (Ctrl+Space)
Alt key and mouse pointer dragging for column selection
If nothing is selected in the editor, and you press Ctrl+C, then the whole line at caret is copied to the clipboard.
Ctrl + Delete delete word from caret to end.
Ctrl + Backspace delete word from caret to start.
Duplicate line: CTRL + D
To compare two directories,or jar files, select one or both of them in the Project view and press Ctrl+D.
Editor Delete Line: CTRL + Y
Move line UP, DOWN. Select a code fragment and press Ctrl+Shift+Up or Ctrl+Shift+Down.
To open any class or file in the editor at the desired line, press Ctrl+Shift+N (Navigate | File), start typing the name, and choose the one from the suggestion list. Then type the colon (:) and a line number
Ctrl+Shift+Enter to complete a current statement such as if, do-while, try-catch, return (or a method call)
Alt+Enter to invoke a quick fix or intention action, press the right arrow key to reveal the list of additional options.
To quickly review your recent changes to the project: Alt+Shift+C
Code Completion, you can accept the currently highlighted selection in the popup list with the period character (.), comma (,), semicolon (;), space and other characters.
Quickly move between methods in the editor: Alt+Up and Alt+Down
Ctrl+Shift+J shortcut joins two lines into one and removes unnecessary space to match your code style
Ctrl+Shift+V to choose and insert recent clipboard contents into the text
Inheritance hierarchy for a selected class: Ctrl+H
Right-clicking on a breakpoint marker you can quickly enable/disable the breakpoint or adjust its properties.
Evaluate the value of any expression while debugging the program, select its text in the editor (you may press a Ctrl+W a few times to efficiently perform this operation) and press Alt+F8.
Quickly evaluate the value of any expression while debugging the program, hold Alt and click this expression to see its value and calculate it, call a method, etc.
You can use the Extract Variable refactoring even on incomplete statements. Just select the necessary block in the editor and press Ctrl+Alt+V.
To open any particular method or field in editor: Ctrl+Alt+Shift+N
Add unimplemented methods: CTRL + I
Override methods: CTRL + O
Format code: CTRL + ALT + L
Show project: ALT + 1
Show logcat: ALT + 6
Hide project - logcat: SHIFT + ESC
Build: CTRL + F9
Build and Run: CTRL + F10
Alt+Shift+F10 access the Run/Debug dropdown on the main toolbar
Collapse all: CTRL + SHIFT + NumPad +
Expand all: CTRL + SHIFT + NumPad -
Editace: 2013-12-09 13:10:42
Počet článků v kategorii: 396
Url:draw-text-drawtext-android-basic-example