Saturday, February 15, 2014

android.text.TextUtils and android.text.Html

This example demonstrate how HTML encode/decode using android.text.TextUtils and android.text.Html.
HTML encode/decode
HTML encode/decode

package com.example.androidtextutils;

import android.os.Bundle;
import android.app.Activity;
import android.text.Html;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

EditText textSrc;
TextView textDest1, textDest2, textDest3, textDest4, textDest5;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textSrc = (EditText)findViewById(R.id.textsrc);
textDest1 = (TextView)findViewById(R.id.textdest1);
textDest2 = (TextView)findViewById(R.id.textdest2);
textDest3 = (TextView)findViewById(R.id.textdest3);
textDest4 = (TextView)findViewById(R.id.textdest4);
textDest5 = (TextView)findViewById(R.id.textdest5);

Button buttonConvert = (Button)findViewById(R.id.convert);
buttonConvert.setOnClickListener(buttonConvertOnClickListener);
}

View.OnClickListener buttonConvertOnClickListener =
new View.OnClickListener(){

@Override
public void onClick(View arg0) {
String src = (String) textSrc.getText().toString();
String t_htmlEncode = TextUtils.htmlEncode(src);
textDest1.setText(t_htmlEncode);
Spanned t_htmlEncode_fromHtml = Html.fromHtml(t_htmlEncode);
textDest2.setText(t_htmlEncode_fromHtml);

Spanned t_fromHtml = Html.fromHtml(src);
textDest3.setText(t_fromHtml);
String t_fromHtml_toHtml = Html.toHtml(t_fromHtml);
textDest4.setText(t_fromHtml_toHtml);

String t_escapeHtml = Html.escapeHtml(src); //API 16
textDest5.setText(t_escapeHtml);
}};


}

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="android-coding.blogspot.com" />

<EditText
android:id="@+id/textsrc"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/convert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Html-encode the string" />
<TextView
android:text="--- TextUtils.htmlEncode(src) ---"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textdest1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="--- Html.fromHtml(TextUtils.htmlEncode(src)) ---"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textdest2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="--- Html.fromHtml(src) ---"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textdest3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="--- Html.toHtml(Html.fromHtml(src)) ---"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textdest4"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<TextView
android:text="--- Html.escapeHtml(src) ---"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textdest5"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

No comments:

Post a Comment