ryhan112
ryhan112

Reputation: 51

Access TextView in Fragment from Activity

I am trying to access the TextView in the fragment class from the main activity. I can't get fragment instance from the main activity. I wanna be able to display the url in the TextView when my WebView is loaded. When i run up app i can see in the logcat the url title is retrieved, but i cant find a way to display it in the fragment TextView

MainActivity

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {

    private WebViewFragment mWebViewFragment;
    public static TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.textViewId);
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction =  fragmentManager.beginTransaction();

        mWebViewFragment = new WebViewFragment();
        fragmentTransaction.replace(R.id.mainFragment, mWebViewFragment);
        fragmentTransaction.commit();
    }

    @Override
    public void onBackPressed() {

        if(mWebViewFragment != null && mWebViewFragment.canGoBack()) {
            mWebViewFragment.goBack();
        } else {
            super.onBackPressed();
        }
    }
}

WebFragment Activity

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebViewFragment extends Fragment {

    private WebView mWebView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);

        mWebView = (WebView) view.findViewById(R.id.webView);

        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.loadUrl("https://www.google.co.uk/");
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
               String title = mWebView.getTitle();
                Log.v(getClass().getName(), "Title=" + title);
            }
        });

        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                Log.v(getClass().getName(), "Received Title" + title);
            }
        });
        return view;
    }

    public boolean canGoBack() {
        return mWebView.canGoBack();
    }

    public void goBack() {
        mWebView.goBack();
    }

    public void setTitle(String title) {
        Activity activity = getActivity();
    }

}

Upvotes: 0

Views: 3873

Answers (1)

guipivoto
guipivoto

Reputation: 18677

I'm assuming that you want to access Activity in setTitle() method.

In your fragment:

public class WebViewFragment extends Fragment {

    private WebView mWebView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);

        mWebView = (WebView) view.findViewById(R.id.webView);

        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.loadUrl("https://www.google.co.uk/");
        mWebView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView view, String url) {
               String title = mWebView.getTitle();
                Log.v(getClass().getName(), "Title=" + title);
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                setTitle(view, title);
                Log.v(getClass().getName(), "Received Title" + title);
            }
        });

    }
}

public void setTitle(String title) {
    Activity activity = getActivity();
    if(activity instanceof MainActivity) {
        ((MainActivity) activity).setTitle(title);
    }
}

And in you MainActivity

public class MainActivity extends FragmentActivity {

    // Remove STATIC from the view to avoid memory leaks
    public TextView textView;

    public void setTitle(String title) {
        if(textView != null) {
            textView.setText(title);
        }
    }
}

Upvotes: 1

Related Questions