Linkify text link url in TextView text Android example
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());
396LW NO topic_id
AD
Další témata ....(Topics)
Html.fromHtml(), setText(), textView.setText(Html.fromHtml(String htmlFormat)), Android example.
TextView mIdTextOk = (TextView)findViewById(R.id.idTextOk);
int _nOk = 57;
// number 57 will bold style
mIdTextOk.setText(Html.fromHtml( "OK: "+"<b>"+_nOk+"</b>"));
android:gravity="left|center_vertical" or android:gravity="right|center_vertical"
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:paddingLeft="20dp"
android:text="My Button"
/>
PNG, GIF, BMP, JPG
// //www.apache.org/licenses/LICENSE-2.0
// 1.) Bitmap from stream
InputStream is = context.getResources().openRawResource(R.drawable.my_image);
mBitmap = BitmapFactory.decodeStream(is);
// 2.)
Bitmap ball = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
int dstWidth =120;
int dstHeight = 120;
mBitmap = Bitmap.createScaledBitmap(ball, dstWidth, dstHeight, true);
// 3.)
Bitmap mBitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
Bitmap mBitmap;
Canvas mCanvas;
Paint mBitmapPaint;
mBitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
// 4.)
Bitmap mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
Canvas mCanvas.setBitmap(mBitmap);
mCanvas.drawColor(0xFFFFFFFF);
The Android WebView changing the height according to text size.
If you need to change the height to a minimum try this source:
If you need to change the height to a minimum try this source:
//public void loadDataWithBaseURL (String baseUrl, String data, String mimeType, String encoding, String historyUrl)
WebView _webView; // findById(.....
String entryContent = "";
_webView.loadDataWithBaseURL(
"blabol",// javascript i styly
entryContent, null,
"utf-8", null
);
Map TreeMap sorted by value Java Android example.
MainClass.java
MainClass.java
import java.util.Comparator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
public class MainClass {
public static void main(String[] arg) {
// english;germany dictionary
String[] arrayOfString = { "one;eine", "two;zwei", "three;drei",
"four;vier" };
Map<String, String> map = new TreeMap<String, String>();
for (String s : arrayOfString) {
String[] array = s.split(";");
String sKey = "", sValue = "";
if (array.length > 1) {
sKey = array[0];
sValue = array[1];
map.put(sKey, sValue);
}
}
for (Entry<String, String> entry: mapSortedByValues(map)) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
}
static <K, V extends Comparable<? super V>> SortedSet<Map.Entry<K, V>> mapSortedByValues(
Map<K, V> map) {
SortedSet<Map.Entry<K, V>> sortedSetOfEntries = new TreeSet<Map.Entry<K, V>>(
new Comparator<Map.Entry<K, V>>() {
@Override
public int compare(Map.Entry<K, V> entry_1, Map.Entry<K, V> entry_2) {
int res = entry_1.getValue().compareTo(entry_2.getValue());
return res != 0 ? res : 1;
// return entry_1.getValue().compareTo(entry_2.getValue());
}
});
sortedSetOfEntries.addAll(map.entrySet());
return sortedSetOfEntries;
}
}
/*
three = drei
one = eine
four = vier
two = zwei
*/
Editace: 2011-11-25 12:32:18
Počet článků v kategorii: 396
Url:linkify-text-link-url-in-textview-text-android-example