Skip to content Skip to sidebar Skip to footer

Clickable Words In A Textview

I want to show some sort of 'formatted' text in my activity. Some words within this TextView (for example bold formatted) should be clickable. When the user clicks on them a toast

Solution 1:

I found probably the better and more flexible solution for me.

I can use WebView, HTML and JavaScript-Functions within it which call methods in my android application.

publicclassMainScreenextendsActivity 
{

    /** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        WebViewwebview=newWebView(this);
        setContentView(webview);
        Stringdata="Test <u onClick=\"showAndroidToast('test')\">it</u>";
        Stringjs_data="<script type=\"text/javascript\">function showAndroidToast(toast){Android.showToast(toast);}</script>";
        webview.loadData(data + js_data, "text/html", "utf-8");

        WebSettingswebSettings= webview.getSettings();
        webSettings.setJavaScriptEnabled(true);

        webview.addJavascriptInterface(newJavaScriptInterface(this), "Android");        
    }    

    publicclassJavaScriptInterface
    {
        Context mContext;

        /** Instantiate the interface and set the context */
        JavaScriptInterface(Context c)
        {
            mContext = c;
        }

        /** Show a toast from the web page */publicvoidshowToast(String toast)
        {
            Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
        }
    }
}

Post a Comment for "Clickable Words In A Textview"