HTML Text in WebView Android

XML File

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

Activity Class

public class HtmlWebViewActivity extends AppCompatActivity{

    private WebView webView;

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

        webView = (WebView) findViewById(R.id.webView);

        showHtmlWebView();
    }

    private void showHtmlWebView() {
        final String mimeType = "text/html";
        final String encoding = "UTF-8";
        String htmlString = "<html>\n" +
                "<body>\n" +
                "<center>\n" +
                "<img src='http://www.homeworknow.com/hwnow/upload/images/tn_star300.gif' />\n" +
                "<p>Hello World </p>\n" +
                "<font color='#60c000' size='4'><strong>Android Developer</strong></font>\n" +
                "<p>Android powers hundreds of millions of mobile devices in more than 190 countries around the world. It's the largest installed base of any mobile platform and growing fast—every day another million users power up their Android devices for the first time and start looking for apps, games, and other digital content.</p>\n"+
                "<p>Android gives you everything you need to build best-in-class app experiences. It gives you a single application model that lets you deploy your apps broadly to hundreds of millions of users across a wide range of devices—from phones to tablets and beyond.</p>\n"+
                "</center>\n" +
                "</body>\n" +
                "</html>\t\t ";

        webView.loadData(htmlString, mimeType, encoding);
    }
}

View Image

Share this if like this post

Encode and Decode data in android

XML File

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    android:layout_width="match_parent"    
    android:layout_height="match_parent"    
    android:orientation="vertical"    
    android:weightSum="1">

    <RelativeLayout        
        android:layout_width="match_parent"        
        android:layout_height="match_parent"        
        android:layout_weight=".5"        
        android:orientation="vertical">

        <TextView            
          android:layout_width="match_parent"            
          android:layout_height="wrap_content"            
          android:gravity="center"            
          android:text="@string/Encode"            
          android:textColor="@android:color/holo_blue_dark"            
          android:textSize="@dimen/twenty" />

        <EditText            
          android:id="@+id/editEncode"            
          android:layout_width="match_parent"            
          android:layout_height="wrap_content"            
          android:layout_above="@+id/txtEncoded"            
          android:layout_alignParentLeft="true"            
          android:layout_alignParentStart="true"            
          android:gravity="center"            
          android:hint="@string/EnterText" />

        <TextView            
          android:id="@+id/txtEncoded"            
          android:layout_width="wrap_content"            
          android:layout_height="wrap_content"            
          android:layout_centerHorizontal="true"            
          android:layout_centerVertical="true"            
          android:layout_marginTop="@dimen/twenty"            
          android:textIsSelectable="true"            
          android:text="Encode Text" />

        <Button            
          android:id="@+id/btEncode"            
          android:layout_width="wrap_content"            
          android:layout_height="wrap_content"            
          android:layout_alignParentBottom="true"            
          android:layout_centerHorizontal="true"            
          android:text="@string/Encode" />

    </RelativeLayout>

    <RelativeLayout        
       android:layout_width="match_parent"        
       android:layout_height="match_parent"        
       android:layout_weight=".5">

        <LinearLayout         
           android:layout_width="match_parent"     
           android:layout_height="5dp"
           android:background="@android:color/black"></LinearLayout>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/Decode"
            android:textColor="@android:color/holo_blue_dark"
            android:textSize="@dimen/twenty" />

        <EditText
            android:id="@+id/editDecode"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/txtDecoded"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:gravity="center"
            android:hint="@string/EnterText" />

        <TextView
            android:id="@+id/txtDecoded"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginTop="@dimen/twenty"
            android:text="Decode Text"
            android:textIsSelectable="true" />

        <Button
            android:id="@+id/btDecode"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:text="@string/Decode" />
    </RelativeLayout>

</LinearLayout>

Activity Class


public class EncodeDecodeActivity extends AppCompatActivity implements View.OnClickListener {

    private final String TAG = EncodeDecodeActivity.class.getSimpleName();
    @Override    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.encode);

        initializeValues();
    }

    private void initializeValues() {

        Button btEncode = (Button) findViewById(R.id.btEncode);
        Button btDecode = (Button) findViewById(R.id.btDecode);

        btEncode.setOnClickListener(this);
        btDecode.setOnClickListener(this);
    }

    @Override    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btEncode:
                encodeData();
                break;

            case R.id.btDecode:
                decodeData();
                break;
        }
    }

    private void encodeData() {
        EditText editEncode = (EditText) findViewById(R.id.editEncode);
        TextView txtEncode = (TextView) findViewById(R.id.txtEncoded);
        String encode = editEncode.getText().toString().trim();
        if (!encode.isEmpty()){
            try {
                byte[] encodeByte = encode.getBytes("UTF-8");
                String strEncode = Base64.encodeToString(encodeByte, Base64.DEFAULT);
                txtEncode.setText(strEncode);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

        }else {
            Toast.makeText(this, "Enter your text to encode", Toast.LENGTH_SHORT).show();
        }
    }

    private void decodeData() {
        EditText editDecode = (EditText) findViewById(R.id.editDecode);
        TextView txtDecode = (TextView) findViewById(R.id.txtDecoded);

        String decode = editDecode.getText().toString().trim();
        if (!decode.isEmpty()){
            try {

                byte[] decodeByte = Base64.decode(decode, Base64.DEFAULT);
                String strDecode = new String(decodeByte, "UTF-8");
                txtDecode.setText(strDecode);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

        }else {
            Toast.makeText(this, "Enter your text to decode", Toast.LENGTH_SHORT).show();
        }
    }
}

View Image

Share this blog if you like this post:

Privacy Policy

What information do we collect? – We may collect information such as device name, device id, client user-agent for advertising. What ...