1

I know there are lots of other answers on stackoverflow on the same thing but I can't seem to get it to work.

What I'm trying to do is find the ID of a view from an inflated layout. I want WV1 to load google.com when the button is clicked, you can see I'm using onClick from XML to do this.

public void ButtonClicked(View view)
        { 

        View inflatedView = getLayoutInflater().inflate(R.layout.tab_content, null);
        WV1 = (WebView) inflatedView.findViewById(R.id.tab1WV);
        WV1.setWebViewClient(new InsideWebViewClient());

          if (WV1.isShown()) {
                WV1.requestFocus();
                    }
                else{
                }

          if (WV1.isFocused()) {
                WV1.loadUrl("http://www.google.com"); 
                }
        }

This is in the MainActivity, the webview (WV1) is in the other, inflated class.

Problem is, nothing happens at all...

I've been stuck on this for quite some time now, I appreciate all help given to me.. If there's any other information you require then just ask, thanks in advance!

--Edit-- In the MainActivity, theres tabhost and a button that creates new tabs. When new tabs are created the MainActivity inflates the second class file containing the webview, I can't get the mainactivity to find the webview from the inflated class. I dont know if this helps any more or not...

Tssomas
  • 362
  • 1
  • 4
  • 16

3 Answers3

0

You're not attaching it to anything. You need to either supply the parent when you inflate it, or call addView on the ViewGroup that should contain it.

public void ButtonClicked(View view){ 

    View inflatedView = getLayoutInflater().inflate(R.layout.tab_content, (ViewGroup) findViewById(android.R.id.content));
    WV1 = (WebView) inflatedView.findViewById(R.id.tab1WV);
    WV1.setWebViewClient(new InsideWebViewClient());

    if (WV1.isShown()) {
        WV1.requestFocus();
    }

     if (WV1.isFocused()) {
         WV1.loadUrl("http://www.google.com"); 
    }
}

Or:

((ViewGroup)findViewById(android.R.id.content)).addChild(WV1);

Both of these will add your view at the end. You may need to add some layout attributes to get it to look like you want.

JohanShogun
  • 2,956
  • 21
  • 30
  • hey johan, thanks for the time! I'm a little confused by what you mean, could you possibly include an example? :) – Tssomas Sep 10 '15 at 19:41
  • 1
    Done :) add your current layout and desired result if you need more help. – JohanShogun Sep 10 '15 at 20:05
  • I've added an edit to my question.. I'm just not sure about what you're saying, thank you for the example though, I appreciate all your time! :) – Tssomas Sep 10 '15 at 20:23
  • Calling addChild will just add your view without any information about how you want it to look (width, height etc). The option of supplying the parent during inflation will use your attributes supplied in the xml file – JohanShogun Sep 10 '15 at 20:31
0

Check out this link:

Android - Add textview to layout when button is pressed

Where one of the answers does this: mLayout.addView(createNewTextView(mEditText.getText().toString()));

where mLayout is a linear layout in the activity.

You'd have to add a view to one of your current layouts or start up an activity that opens up with a web view in it.

Community
  • 1
  • 1
sb6847
  • 92
  • 4
0

Alternative way to create webview :

Webview WV1 = new WebView(view.getContext);
WV1.setWebViewClient(new InsideWebViewClient());

  if (WV1.isShown()) {
        WV1.requestFocus();
            }
        else{
        }

  if (WV1.isFocused()) {
        WV1.loadUrl("http://www.google.com"); 
        }
}

Now add this to a view group

viewgroup.addchild(WV1);
rahul.ramanujam
  • 5,608
  • 7
  • 34
  • 56