TAG | abrir
Nov/09
3
Abrir URL web al hacer click en un botón
162 Comments | Posted by Mikel in aplicacion, codigo, primeros pasos, tutoriales
Vamos a aprender a abrir una URL a través de un botón.
Para ello vamos a crear un nuevo proyecto llamado lanzar web y vamos a ir a main.xml donde diseñaremos el botón.
Antes del vamos a escribir:
<Button android:id="@+id/abrir" android:text="@string/texto_abrir" android:layout_width="fill_parent" android:layout_height="wrap_content" />
Como ves, al boton le hemos asignado la id abrir y el string texto_abrir.
Vamos a strings.xml y vamos a añadir la variable texto_abrir
<string name="texto_abrir">Abrir URL</string>
Ya hemos diseñado el botón, pero ahora hay que programarlo.
Vamos fichero java y debajo del SetContentView añadimos lo siguiente:
Button abrir = (Button) findViewById(R.id.abrir);
abrir.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.goear.com")));
}
});
y lanzamos nuestra aplicación.
Un saludo
ir al Tema del FORO “Crear botón para abrir URL web”
