Text In textView is not showing but the java code work. If i add autoLink="web" tag to textview , the text shows but java code doesn't work. When the textview is pressed , user should be redirected to contact email.
This is the text view xml.
<TextView
android:id="@+id/textViewContact"
android:layout_width="match_parent"
android:layout_height="75dp"
android:layout_marginBottom="250dp"
android:autoLink="email"
android:fontFamily="@font/rubik_bold"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
If i add any autoLink tag , the text in textview shows but the user is not redirected to email. If i remove the tag , user is redirected to email but the textview text is not showing.
This is the java code:
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.MailTo;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.TextView;
public class ContactUs extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_us);
TextView mail =(TextView) findViewById(R.id.textViewContact);
mail.setText(Html.fromHtml("<a href=\"mailto:[email protected]\">Contact Us</a>"));
mail.setMovementMethod(LinkMovementMethod.getInstance());
}
}
Thanks in advance for help!
You need to add below code to your activity as the method you are using is deprecated
For kotlin, use below code snippet
if (Build.VERSION.SDK_INT >= 24) {
mail.text = Html.fromHtml("<a href=\"mailto:[email protected]\">Contact Us</a>",Html.FROM_HTML_MODE_LEGACY)
} else {
mail.text = Html.fromHtml("<a href=\"mailto:[email protected]\">Contact Us</a>")
}
For Java, Use below lines
if (Build.VERSION.SDK_INT >= 24) {
mail.setText(Html.fromHtml("<a href=\"mailto:[email protected]\">Contact Us</a>",Html.FROM_HTML_MODE_LEGACY));
} else {
mail.setText(Html.fromHtml("<a href=\"mailto:[email protected]\">Contact Us</a>"));
}
Also there is no need of autolink property in the TextView tag. You can skip this line in xml and you will be able to see the Contact Us text and clicking on link will navigate you to gmail.