0

I'm trying to add a FloatingActionButton to my Android App whick has a WebView as the app content, but I can't figure out how.

Where and how should I go about it?

Thank you all in advance.

public class MainActivity extends AppCompatActivity {

    private AppBarConfiguration appBarConfiguration;
    private ActivityMainBinding binding;

    private WebView mWebview ;

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

        binding = ActivityMainBinding.inflate(getLayoutInflater());

        mWebview  = new WebView(this);

        mWebview.loadUrl("https://www.Camp-Ann.com/");

        setContentView(mWebview);

        FloatingActionButton reloadTab = getReloadFab(this);
    }

    public FloatingActionButton getReloadFab(Context context) {
        FloatingActionButton fab = new FloatingActionButton(context);

        fab.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                mWebview.loadUrl( "javascript:window.location.reload( true )" );
            }
        });
        return fab;
    }
}
Program-Me-Rev
  • 6,184
  • 18
  • 58
  • 142

1 Answers1

0

What you are trying is possible. But, according to the approach you are using, it is wrong.

  1. So, first of all, you are not using binding so, we will use that.
  2. You are not adding the fab to the view, so we will add that.

Now, to solve the problem, we will do both the things mentioned above with our final code looking like this:

public class MainActivity extends AppCompatActivity {

    private AppBarConfiguration appBarConfiguration;
    private ActivityMainBinding binding;

    private WebView mWebview ;

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

        binding = ActivityMainBinding.inflate(getLayoutInflater());

        setContentView(binding.getRoot());

        mWebview  = new WebView(this);
   
        binding.getRoot().addView(mWebView);

        mWebview.loadUrl("https://www.Camp-Ann.com/");

        FloatingActionButton reloadTab = getReloadFab(this);

        binding.getRoot().addView(reloadTab);
        
    }

    public FloatingActionButton getReloadFab(Context context) {
        FloatingActionButton fab = new FloatingActionButton(context);

        fab.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                mWebview.loadUrl( "javascript:window.location.reload( true )" );
            }
        });
        return fab;
    }
}
Sambhav Khandelwal
  • 3,585
  • 2
  • 7
  • 38