Open URL from EditText

2

I'm trying to make an application similar to a browser but without WebView , just a EditText where you put a URL and pressing a certain button opens said URL in the default browser. In case it was not understood, I leave more or less what I have so far, it is not much at the moment:

XML

<Button
    android:text="IR"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/button"
    android:layout_below="@+id/et1"
    android:layout_toRightOf="@+id/tv1"
    android:layout_toEndOf="@+id/tv1" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:text="Name"
    android:ems="10"
    android:layout_marginTop="17dp"
    android:id="@+id/et1"
    android:layout_below="@+id/tv1"
    android:layout_alignLeft="@+id/button"
    android:layout_alignStart="@+id/button"
    android:layout_marginLeft="31dp"
    android:layout_marginStart="31dp" />

Code:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Button;

public class actividad extends AppCompatActivity {
private EditText et1;
private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_actividad);
    et1 = (EditText) findViewById(R.id.et1);

  }

 public void ejecutar(View view) {
    Intent i = new Intent(this, Actividad.class);
    i.putExtra("direccion", et1.getText().toString());
    startActivity(i);
    
asked by Franco Galuzzi 15.11.2016 в 19:00
source

2 answers

2

To Open URL from the content of a EditText .

You need to get the value of the content within the EditText , in this case a url that you have written, you make a Intent defining an action as ACTION_VIEW .

  //Obtienes la referencia del EditText.
  myEditText = (EditText) findViewById(R.id.edittext);
  //Obtienes el text (url) dentro del EditText y defines ACTION_VIEW .
  Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(myEditText.getText().toString()));
  //abre el Intent.
   startActivity(intent);

It is important to use .toString () to get the value correctly within the EditText.

according to your code, with this you can open the url that you specify inside your EditText:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(et1.getText().toString()));
startActivity(intent);
    
answered by 15.11.2016 / 21:05
source
0

You have to create a Intent

Intent intent= new Intent(Intent.ACTION_VIEW,Uri.parse(et1.getText().toString()));
startActivity(intent);

You should also validate that what is entered in EditText is a valid URL

if(URLUtil.isValidUrl(et1.getText().toString()){
    Intent intent= new Intent(Intent.ACTION_VIEW,Uri.parse(et1.getText().toString()));
    startActivity(intent);
}

To do it from the button

Button buttonUrl = (Button) findViewById(R.id.button);
buttonUrl.setOnClickListener( new OnClickListener() {
    @Override
    public void onClick(View v) {
       if(URLUtil.isValidUrl(et1.getText()){
           Intent intent= new Intent(Intent.ACTION_VIEW,Uri.parse(et1.getText().toString()));
           startActivity(intent);
       }
    }
});
    
answered by 15.11.2016 в 19:25