How Do you open a web browser using links in a listview via android studio?

1.9k Views Asked by At

I am trying to create a listview that list three items which are links to websites. When, I select an item from the listview, it should open a web browser and take me to the website that was linked in the code. I have put the three links into an array and I'm trying to do a click event to trigger the browser.

Here's my Java code:

package edu.dtcc.bwharto9.intentslab;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
private String[] monthsArray = { "StackOverflow", "Developer.Android", "Javatechig", };


private ListView ListView;
private ArrayAdapter arrayAdapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ListView = (ListView) findViewById(R.id.months_list);

    // this-The current activity context.
    // Second param is the resource Id for list layout row item
    // Third param is input array
    arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, monthsArray);
    ListView.setAdapter(arrayAdapter);

    ListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {
        public void onItemSelected(AdapterView parentView, View childView,
                                   int position, long id) {
            Intent i = new Intent(Intent.ACTION_VIEW,
                    Uri.parse("http://stackoverflow.com/"));
            startActivity(i);

            Intent j = new Intent(Intent.ACTION_VIEW,
                    Uri.parse("http://developer.android.com/"));
            startActivity(j);

            Intent k = new Intent(Intent.ACTION_VIEW,
                    Uri.parse("http://Javatechig.com/"));
            startActivity(k);
        }
        public void onNothingSelected(AdapterView parentView) {
        }
    });
}

}

And here's my XML if it's needed:

<LinearLayout 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:orientation="vertical">

<ListView
android:id="@+id/months_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:choiceMode="singleChoice">
</ListView>
</LinearLayout>

Thanks in advance to everyone who helped!

1

There are 1 best solutions below

0
On

On your onItemClickListener you should do something like this

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);

You should use setOnItemClickListener instead setOnItemSelectedListener