Skip to content

Instantly share code, notes, and snippets.

@ZaeemSattar
Created November 1, 2016 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZaeemSattar/be6cc9edf019c72786c5625038a597b3 to your computer and use it in GitHub Desktop.
Save ZaeemSattar/be6cc9edf019c72786c5625038a597b3 to your computer and use it in GitHub Desktop.
public class Insta extends Fragment {
WebView webView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.activity_my_insta, container,
false);
webView=(WebView)root.findViewById(R.id.webView);
WebSettings webSettings=webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("https://instagram.com");
webView.setWebViewClient(new MyWebViewClient());
getActivity().setTitle("Instagram");
return root;
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
}
}
@Nouman16
Copy link

Nouman16 commented Nov 5, 2016

What Does MyWebViewClient acctualy doing?

@ZaeemSattar
Copy link
Author

when you click anywhere in website inside webview then you will be asked my android OS to choose how you want to open this link . MyWebViewClient overrides this behaviour and don't ask this prompt message again and again and you will be in webview until you close this application

@Nouman16
Copy link

Nouman16 commented Nov 5, 2016

is there a method names as loadResources in which we can show progresss dialog untill the website open in webview ??

@Nouman16
Copy link

Nouman16 commented Nov 5, 2016

my code is working but i need to know that we have write a line of code in onCreateView method that is webView.loadUrl("https://instagram.com");
and in method shouldOverrideUrlLoading we have almost same line of code view.loadUrl(url);

@ZaeemSattar
Copy link
Author

No . if you want to show progress dialog you should use

@OverRide
public void onPageFinished(WebView view, String url) {
progressbar.setVisibility(View.GONE);
super.onPageFinished(view, url);
}

shouldOverrideUrlLoading method gives you feasibility to change nevigation behaviour of webview and gives you nect nevigation URL

@Nouman16
Copy link

Nouman16 commented Nov 5, 2016

by this it will hide...when to initialize it ?? is not there any method to set some UI like we have onPreExecute() in AsyncTask
90

@ZaeemSattar
Copy link
Author

You can initialize it in onCreate in Acivity and onCreateView in fragment

Below is code snippet

public class DcsAdmissonPortalFragment extends BaseFragment {
LinearLayout progressbar;

public DcsAdmissonPortalFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_dcsadmissionportal, container, false);
    progressbar=(LinearLayout) rootView.findViewById(R.id.progressbar_layout);
    progressbar.setVisibility(View.VISIBLE);
    WebView myWebView = (WebView) rootView.findViewById(R.id.webview);
    myWebView.loadUrl("http://csadmissions.gcu.edu.pk/");
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.setWebViewClient(new MyWebViewClient());



    // Inflate the layout for this fragment
    return rootView;
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
}

@Override
public void onDetach() {
    super.onDetach();
}

private class MyWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        progressbar.setVisibility(View.GONE);
        super.onPageFinished(view, url);
    }
}

}

@Nouman16
Copy link

Nouman16 commented Nov 5, 2016

OnAttach method functionality in this code ?? in fragment i used this method to cast my interface variable into the calling activity variable..
just like
public void onAttach(Activity activity) {
myInterfaceVariable = (InterFace) activity;

}

then we can send values to activity from fragments by this
myInterfaceVariable.methodName(myValues);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment