-4

I am a beginner at Android. I am not able to pass url as a string to other activity for generating WebView. Someone Help me out!

MainActivity:

    public class MainActivity extends AppCompatActivity {
EditText et_url;
Button btn;
public static String url;



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

    btn=(Button)findViewById(R.id.btn);
    et_url=(EditText)findViewById(R.id.et_url);
    url=et_url.getText().toString();
    /*url=url.replace(" ","");*/
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id=v.getId();
            if(id==R.id.btn){
                Intent web=new Intent(getApplicationContext(),WebActivity.class);
                startActivity(web);
            }
        }
    });
}

WebActivity:

    public class WebActivity extends AppCompatActivity {

 /* public String  myurl=MainActivity.getUrl();*/
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web);
    getWindow().
    setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.La
    youtParams.FLAG_FULLSCREEN);
    getSupportActionBar().hide();
    url=url.trim();
    webView=(WebView)findViewById(R.id.web);
    webView.loadUrl("http://"+url+"/");
       }
   }

Its not able to read the url which I inout in the main activity.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

6 Answers6

2

In your main activity:

btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id=v.getId();
            if(id==R.id.btn){
                Intent web=new Intent(getApplicationContext(),WebActivity.class);
web.putExtra("url",url);
                startActivity(web);
            }
        }
    });

And in WebActivity:

Intent intent = getIntent();
url = intent.getStringExtra("url");
Piyush
  • 2,589
  • 6
  • 38
  • 77
0

Before start activity enter something like this.

web.putExtra("url", urlString);

the first parameter in putExtra is the key and second parameter is the value for the data you want to pass.

In your WebActivity you can receive the data by using the following code:

String url = getIntent().getStringExtra("url");
0

You can pass string from one activity to another using intent. Please check below code snippet.

In your MainActivity.class

if(id==R.id.btn){
 Intent web=new Intent(getApplicationContext(),WebActivity.class);
 web.putExtra("URL", url);
 startActivity(web);
}

OnCreate() of WebActivity .class

Intent intent = getIntent();
String url= intent.getExtras().getString("URL");
url=url.trim();
webView=(WebView)findViewById(R.id.web);
webView.loadUrl("http://"+url+"/");
EKN
  • 1,886
  • 1
  • 16
  • 29
0

You didn't actually send or pass the String Url to other activity.. Try this :

Intent intent = new Intent(getBaseContext(), webActivity.class);
intent.putExtra("This is your Url String", myUrl);
startActivity(intent);

And access the URL through other activity:

String s = getIntent().getStringExtra("MyUrl");
Prithvi Raj
  • 1,611
  • 1
  • 14
  • 33
0

Please Try below like this...

  url=et_url.getText().toString();

      btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent =new Intent(MainActivity.this,WebActivity.class);
                    intent.putExtra("passurl",url);
                     startActivity(intent );
                    }
                }
            });


         public class WebActivity extends AppCompatActivity {

         /* public String  myurl=MainActivity.getUrl();*/
        WebView webView;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_web);
            getWindow().
            setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.La
            youtParams.FLAG_FULLSCREEN);
            getSupportActionBar().hide();


            Intent intent = getIntent();
            String url = intent.getStringExtra("passurl");
            webView=(WebView)findViewById(R.id.web);
            webView.loadUrl(url);

               }
           }

I hope this will help you.

Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
0

Pass your url to next activity via intent.

btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        int id=v.getId();
        if(id==R.id.btn){
            Intent intent = new Intent(MainActivity.this, WebActivity.class);
            intent.putExtra("intentKey", url);
            startActivity(web);
        }
    }
});

And in your WebActivity in onCreate:

try {
        Intent intent = getIntent();
        String urlString = intent .getStringExtra("intentKey");
        Log.e("urlString ", urlString + "    +++++");

    } catch (Exception e) {
        e.printStackTrace();            
    }
vss
  • 1,093
  • 1
  • 20
  • 33