How to Get Clickable Hyperlinks in Alertdialog from a String Resource

How can I get clickable hyperlinks in AlertDialog from a string resource?

If you are only showing some text and URL[s] in your dialog perhaps the solution is simpler

public static class MyOtherAlertDialog {

public static AlertDialog create(Context context) {
final TextView message = new TextView(context);
// i.e.: R.string.dialog_message =>
// "Test this dialog following the link to dtmilano.blogspot.com"
final SpannableString s =
new SpannableString(context.getText(R.string.dialog_message));
Linkify.addLinks(s, Linkify.WEB_URLS);
message.setText(s);
message.setMovementMethod(LinkMovementMethod.getInstance());

return new AlertDialog.Builder(context)
.setTitle(R.string.dialog_title)
.setCancelable(true)
.setIcon(android.R.drawable.ic_dialog_info)
.setPositiveButton(R.string.dialog_action_dismiss, null)
.setView(message)
.create();
}
}

As shown here
http://picasaweb.google.com/lh/photo/up29wTQeK_zuz-LLvre9wQ?feat=directlink

Alert dialog with clickable links

How can I get clickable hyperlinks in AlertDialog that will navigate to a new Activity?

Use AlertDialog.setView() to display a custom layout containing your hyperlink TextView in the message area.

E.g. Make a hyperlink_layout.xml containing a TextView with id = hyperlink_text

It only has to contain what is shown in the message area of the AlertDialog, you don't need buttons/title etc.

Create the AlertDialog something like this (from an Activity):

LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.hyperlink_layout, null);

// get the text view from the view:
TextView hyperlinkText = (TextView)dialogView.findViewById(R.id.hyperlink_text);

// format/set it up how you like...

// handle the click on the hyperlink:
hyperlinkText.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// handle the click, launch the new Activity etc
}
});

// create the alert dialog
new AlertDialog.Builder(this)
.setView(dialogView)
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// handle OK
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// handle Cancel
}
})
.show();

How I can make links clickable in an AlertDialog without using a TextView?

I'm not sure you can do without a TextView, I would use a Custom AlertDialog, however you can create a "temporary" TextView within the code to perform what you need, here I leave the url where you have several examples

How can I get clickable hyperlinks in AlertDialog from a string resource?

How to have text and clickable URL link on Alertdialog?

You're missing quotes around your URL. Try:

Html.fromHtml("<a href='https://play.google.com/store/apps/details?id=com.xxxxxxxxx'>Click Here</a>")

Note the single quotes around the URL.

Hyperlink in string resource in AlertDialog

try custom alert dialog below way

Sample Image

LayoutInflater li = LayoutInflater.from(MainActivity.this);
View promptsView = li.inflate(R.layout.prompts, null);

final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setView(promptsView);
final TextView userInput = (TextView) promptsView
.findViewById(R.id.textView);
userInput.setMovementMethod(LinkMovementMethod.getInstance());
userInput.setText(Html.fromHtml(getResources().getString(R.string.about_body)));
builder.setTitle(getResources().getString(R.string.app_name));
// builder.setMessage(Html.fromHtml(getResources().getString(R.string.about_body)))
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
}).show();

prompts.xml

<?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:padding="10dip">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

make sure your string is correct and links are working

add link to alertdialog text android

As I said in my comment you should use custom dialog box.

so create a new xml file for layout of your custom dialog box.

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

<Button
android:id="@+id/button1"
android:text="firstButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<Button
android:id="@+id/dialogButtonOK"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text=" Ok "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
/>

<Button
android:id="@+id/button2"
android:text="SecondButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>

The above will be your layout for custom dialog box.

now inside your method where your using AlertDialog.Builder use below code.

public void myMethod(){
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custome);
dialog.setTitle("Title...");

// set the custom dialog components - text, image and button
Button button1 = (Button) dialog.findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getBaseContext(),MyActivity2.class);
startActivity(intent);
}
});

Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});

dialog.show();
}
}

So this will show your custom dialog box with layout specified in above xml and when you click on button1 it will open MyActivity2 in new view.

for more detail you should check here

Android alert dialog hyper link text

You've still got 1 more button (neutral) to play with if you wanted...

Otherwise, I would suggest reading this question.



This is how I have accomplished it though:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.app_name)
.setIcon(R.drawable.dialog_icon)
.setMessage(R.string.welcome_text)
.setCancelable(true)
.setNegativeButton(R.string.okay, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});

AlertDialog welcomeAlert = builder.create();
welcomeAlert.show();
// Make the textview clickable. Must be called after show()
((TextView)welcomeAlert.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());

I have used regular <a href=".."></a> formatting in my strings.xml

The only thing I don't like about my current solution is that it makes all of the text clickable and not just the links.

How i make String + URL in AlertDialog with String is not clickable and URL is clickable

I am not sure whether it will work or not but you can try this

text.setText(Html.fromHtml("<bMy Text is going here....</b>" +
"<a href=\"http://www.example.com\">Terms and Conditions.</a> "));
text.setMovementMethod(LinkMovementMethod.getInstance());

Android hyperlinks on TextView in custom AlertDialog not clickable

You defined the TextView without android:autoLink="all" , android:clickable="true" & android:linksClickable="false"

For info.xml TextView as below

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/infoDetailText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp" />


Related Topics



Leave a reply



Submit