Click here to Skip to main content
15,743,427 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.example.momskart.R;
import com.example.momskart.databinding.FragmentGalleryBinding;

public class GalleryFragment extends Fragment {

    private GalleryViewModel galleryViewModel;
    private FragmentGalleryBinding binding;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        galleryViewModel =
                new ViewModelProvider(this).get(GalleryViewModel.class);

        binding = FragmentGalleryBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        final WebView specialWebView = binding.idWebViewSpecail;
        final ProgressBar loadingPB =binding.idPBLoading;
        specialWebView.setWebViewClient(new WebViewClient(){
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);

            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

        specialWebView.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View view, int i, KeyEvent keyEvent) {
                if (keyEvent.getAction()==KeyEvent.ACTION_DOWN){
                    switch (keyCode){
                        case KeyEvent.KEYCODE_BACK:


                return false;
        return root;

    public void onDestroyView() {
        binding = null;

What I have tried:

i tried to inilize this keyCode to int
Updated 25-Apr-22 1:58am

1 solution

keyCode is not defined in your file, and is thus not a part of your class.

Did you mean to call the getKeyCode method of the KeyEvent?
KeyEvent (Java Platform SE 7 )[^]
Share this answer
Bhupendra Makode 25-Apr-22 8:16am    
How to define keyCode

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900