Launch a web browser or youtube using textview in android

1k views Asked by At

I will have a list of YouTube video titles and I want when the user clicks them it goes to the url.

Example: click here for this VIDEO!

thanks in advance

code::: Java::

@Override                                                                                    
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
}

private void twistUp() {
TextView twistUp = (TextView) findViewById(R.id.twistUp);
twistUp.setOnClickListener(new OnClickListener() {

@Override
Intent videoclk= new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.youtube.com/watch?v=jvO6CqtiRmo"));
startActivity(videoclk);
}
});


<TextView                                                                                                                         
android:id="@+id/twistUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/twisted"
android:autoLink="web"
android:onClick="twistUp"
android:textAppearance="?android:attr/textAppearanceMedium" />
2

There are 2 answers

1
Adarsh Yadav On

Make your text view clickable and inside your onClick method put:

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("put your url here"));
startActivity(browserIntent);
0
FoamyGuy On

The Linkify object is what you are looking for. You could read up a bit on this object and do your linking "manually" from Java code. But it's actually a bit easier if you don't need fine grain control there is an XML attribute to enable the functionality a bit more automatically.

if you add:

android:autolink="web"

to your TextView it should automatically linkify any URLs that are contained within your text.