Question: Please provide me with the full working code using Kotlin. I've added the provided code included in the assignment. Instructions: Enhance the application from your
Please provide me with the full working code using Kotlin. I've added the provided code included in the assignment.
Instructions:
Enhance the application from your previous assignment to support multiple pages. We are going to treat each instance of PageViewerFragment like a Tab in a standard browser implementation, but since we are not actually using tabs we'll say page instead.
Redesign your BrowserActivitys default layout to add a new container: browsercontrol illustrated below and replace the FragmentContainerView called pagedisplay with a ViewPager
Create a new fragment class, BrowserControlFragment, with the following features:
This fragment should display ImageButton:
The button should create a new browser page a new instance of PageViewerFragmentand display it in the ViewPagerdescribed below
Your ViewPager will use a FragmentStateAdapter that will allow the user to use a swipe
gesture to move between instances of PageViewerFragments.
When the user creates a new page from the BrowserControlFragment, it should be added to the end of the ViewPager and automatically displayed ie the user should see the blank new page and would swipe right to see the previous page
BrowserActivity should behave as follows:
Attach the necessary fragments
On Startup, whether in Portrait or Landscape mode, the activity should display a
single instance of BrowserControlFragment in the browsercontrol
By default, the activity should start with one empty instance of PageViewerFragment
When the user clicks on the NewPage ImageButton in BrowserControlFragment, a new instance of PageViewerFragment should be created and displayed in the ViewPager and that page should by active automatically that is the ViewPager should select the new page immediately
The activity should display as its title the page title of the currently displayed page.
Here is the provided code:
import androidx.appcompat.app.AppCompatActivity
import android.osBundle
class BrowserActivity : AppCompatActivity
override fun onCreatesavedInstanceState: Bundle?
super.onCreatesavedInstanceState
setContentViewRlayout.activitymain
Ensure browser page isn't added twice
if supportFragmentManagerfindFragmentByIdRidpagevieweris PageViewerFragment
supportFragmentManager
beginTransaction
addRidpageviewer, PageViewerFragment
commit
package edu.temple.superbrowser
import android.annotation.SuppressLint
import android.osBundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.EditText
import android.widget.ImageButton
class PageViewerFragment : Fragment
private lateinit var webView: WebView
private lateinit var goButton: ImageButton
private lateinit var bkButton: ImageButton
private lateinit var fwdButton: ImageButton
private lateinit var urlEditText: EditText
override fun onCreateView
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
: View?
return inflater.inflateRlayout.fragmentpageviewer, container, falseapply
webView findViewByIdRidwebView
goButton findViewByIdRidgoButton
bkButton findViewByIdRidbkButton
fwdButton findViewByIdRidfwdButton
urlEditText findViewByIdRidurlEditText
@SuppressLintSetJavaScriptEnabled
override fun onViewCreatedview: View, savedInstanceState: Bundle?
super.onViewCreatedview savedInstanceState
Enable Javascript for proper functioning websites
webView.settings.javaScriptEnabled true;
Provision WebViewClient implementation that retrieves current page URL
webView.webViewClient object: WebViewClient
override fun onPageFinishedview: WebView?, url: String?
super.onPageFinishedview url
url?.run
urlEditText.setTextthis
Restore previous web state if present
savedInstanceState?.run
webView.restoreStatethis
goButton.setOnClickListener
urlEditText.setTextcleanUrlurlEditTexttext.toString
webView.loadUrlurlEditTexttext.toString
bkButton.setOnClickListener
webView.goBack
fwdButton.setOnClickListener
webView.goForward
Helper function to format malformed URLs
private fun cleanUrlurl: String : String
return if urlstartsWithhttp
url
else
https:$url"
override fun onSaveInstanceStateoutState: Bundle
super.onSaveInstanceStateoutState
Store current web state
webView.saveStateoutState
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
