Open a site using a button in the application

1

I'm doing an application similar to a calendar and I need to put a button to open a registered site, I already did a long click on the table and it worked, but on a button I can not use the same syntax

@Override
    public void onCreateContextMenu(final ContextMenu menu, View v, final ContextMenu.ContextMenuInfo menuInfo) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        final Aluno aluno = (Aluno) listaAlunos.getItemAtPosition(info.position);

    MenuItem itemSite = menu.add("Visitar Site");
    Intent intentSite = new Intent(Intent.ACTION_VIEW);

    String site ="http://www.uol.com.br";
            aluno.getSite();
    if(!site.startsWith("http://")) {
        site = "http://" + site;
    }

    intentSite.setData(Uri.parse(site));
    itemSite.setIntent(intentSite);
    
asked by anonymous 18.10.2017 / 15:01

1 answer

0

First, I recommend inflating context menus, rather than creating manually. But do it this way below:

  

Menu XML (menu_site.xml)

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/visitar_site"
        android:title="Visitar Site">
    </item>
</menu>
  

onCreateContextMenu

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{
    getMenuInflater().inflate(R.menu.menu_site, menu);
    super.onCreateContextMenu(menu, v, menuInfo);
}
  

onContextItemSelected (here you will put the code to open the site

@Override
public boolean onContextItemSelected(MenuItem item)
{
        switch (item.getItemId())
        {
            case R.id.pick_camera: {
                Intent intentSite = new Intent(Intent.ACTION_VIEW);

                String site ="http://www.uol.com.br";
                        aluno.getSite();
                if(!site.startsWith("http://")) {
                    site = "http://" + site;
                }

                intentSite.setData(Uri.parse(site));
                startActivity(intentSite);
            } break;
        }
    return super.onContextItemSelected(item);
}
    
18.10.2017 / 15:35