Android: Launch Activity from Clickable Text

Android: Launch activity from clickable text

Try this,

final Context context = ... // whereever your context is
CharSequence sequence = Html.fromSource(context.getString(R.string.clickable_string));
SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
UnderlineSpan[] underlines = strBuilder.getSpans(UnderlineSpan.class);
for(UnderlineSpan span : underlines) {
int start = strBuilder.getSpanStart(span);
int end = strBuilder.getSpanEnd(span);
int flags = strBuilder.getSpanFlags(span);
ClickableSpan myActivityLauncher = new ClickableSpan() {
public void onClick(View view) {
context.startActivity(getIntentForActivityToStart());
}
};

strBuilder.setSpan(myActivityLauncher, start, end, flags);
}

TextView textView = ...
textView.setText(strBuilder);
textView.setMovementMethod(LinkMovementMethod.getInstance());

Basically you have to attach a Span object to the range of characters you want to be clickable. Since you are using HTML anyways, you can use the underline spans placed by the Html.fromSource() as markers for your own spans.

Alternatively you could also define a Tag within the string that only you know of.
i.e. <activity>
And supply your own tag handler to the Html.fromSource() method. This way your TagHandler instance could do something like, surround the tagged text with a specific color, underline, bold and make it clickable. However I would only recommend the TagHandler approach if you find yourself writing this type of code a lot.

how to link a new activity in android studio using a clickable text

Check out Intent. You use these to start new activities or services within your application.

You're correct in that you have to assign an OnClickListener interface to your text, after you made it clickable. In the interface's onClick() method you would need to do something like this.

For example:

@Override
public void onClick(View v) {
// Create the intent which will start your new activity.
Intent newActivityIntent = new Intent(MainActivity.this, NewActivity.class);

// Pass any info you need in the next activity in your
// intent object.
newActivityIntent.putExtra("aString", "some_string_value");
newActivityIntent.putExtra("anInteger", some_integer_value);

// Start the new activity.
startActivity(newActivityIntent);
}

In the next activity, you can retrieve the intent used to start it, so that you'll have access to the data you passed from the first activity, like so:

@Override
public void onCreate(Bundle savedInstanceState) {
// Get the intent that started this activity.
Intent startingIntent = getIntent();

// Retrieve the values.
String aString = startingIntent.getStringExtra("aString");
Integer anInteger = startingIntent.getIntExtra("anInteger", 0); // 2nd param is the default value, should "anInteger" not exist in the bundle.

// Use the values to your hearts content.
}

Hope that helps.

open an activity by clicking on text hyperlink in android

Refer this:

Android: Launch activity from clickable text

How to open a new activity on click of textview in android?

Even though this question has been asked so many times, I decided to answer anyway. This will work:

tvForgotPwd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent(Login.this, PatientSearch.class);
startActivity(myIntent);
}
});

Clickable text link in Alert Dialog to open Activity

Ok, I did it with the help of several other posts here and youtube videos.
I use a Custom Layout showing text combined with clickable spans which I then show in the Alert Dialog:

public class TestDialog extends AppCompatDialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String text = "This is a test message.";

SpannableString ss = new SpannableString(text);

ClickableSpan cs = new ClickableSpan() {
@Override
public void onClick(@NonNull View view) {
Intent testIntent = new Intent(getContext(), Test.class);
getContext().startActivity(testIntent);
}
};

ss.setSpan(cs, 10, 14, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.test_view, null);

TextView message = (TextView) view.findViewById(R.id.textView);
message.setMovementMethod(LinkMovementMethod.getInstance());
message.setText(ss);

builder.setView(view)
.setTitle("Test Title")
.setPositiveButton("Ok!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// do something
}
})
.setNegativeButton("No!", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// do something
}
});

return builder.create();
}
}

I hope I could also help you.

How do I start same Activity with different text when click two different buttons?

You have to set the desired text when calling your second activity, as an extra in the calling intent.

Intent starterIntent = new Intent(FirstActivity.class.this,SecondActivity.class)
starterIntent.putExtra("text_key", "<your text>");

"text_key" is the look up key that will let you retrieve the text in the next activity. Each button should set a different value for <"your text"> of course.

Then on the second activity you retrieve the text like this:

String text = getIntent().getStringExtra("text_key");

Look that Im using the exact key that you used on the first activity (text_key) otherwise you wont find the text.

Finally set the text.



Related Topics



Leave a reply



Submit