简体   繁体   中英

Html.fromHtml not working?

I am trying to add a hyper link into my dialog:

   TextView text = (TextView) layout.findViewById(R.id.text);
        text.setMovementMethod(LinkMovementMethod.getInstance());
        text.setText(Html.fromHtml("text here <i><a href=https://website.com> Terms and Conditions</a> </i> "));

            AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.DialogStyle);
            builder.setView(layout)
                    .setTitle("Dialog")

            .show();
        }

The view is just the following:

<TextView android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:textColor="#FFF"
    />

However, when I do this I only get a blank dialog with a link to website.com, and don't get any of the text here that should appear before the link.

How can I fix this? I should be getting both.

You need to modify your Dialog code properly.

This code will goes inside your Activity on Create .

@SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mContext = this;


        Dialog dialogWeb = new Dialog(mContext);
        dialogWeb.setCancelable(true);
        dialogWeb.setCanceledOnTouchOutside(true);
        dialogWeb.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialogWeb.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        dialogWeb.setContentView(R.layout.test10);

        TextView text = (TextView) dialogWeb.findViewById(R.id.text);
        text.setMovementMethod(LinkMovementMethod.getInstance());
        text.setText(Html.fromHtml("text here <i><a href=https://www.google.com> Terms and Conditions</a> </i> "));

        dialogWeb.show();


    }

Here is XML including your Text View .

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#FFFFFF">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Testtinggg"
        android:textAllCaps="false"
        android:textColor="@android:color/black"
        android:textSize="22sp" />

</RelativeLayout>

Here it will show you the Hyper Link inside Dialog .

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM