Part-5-MD-101-Web-View-Auto-Complete-and-Fragments
Part-5-MD-101-Web-View-Auto-Complete-and-Fragments
Mobile
Application
Development
btnWebView.setOnClickListener{
val url = webText.text.toString()
webBrowse.settings.loadsImagesAutomatically = true
webBrowse.settings.javaScriptEnabled = true
webBrowse.scrollBarStyle = View.SCROLLBARS_INSIDE_OVERLAY
webBrowse.loadUrl(url)
}
}
Don’t forget the calling statement!
WebView Application
webBrowse.settings.loadsImagesAutomatically = true
This line sets the loadsImagesAutomatically property of the webBrowse WebView to true. When this property
is true, the WebView will load and display images from web pages.
webBrowse.settings.javaScriptEnabled = true
This line sets the javaScriptEnabled property of the webBrowse WebView to true. Enabling JavaScript allows
the WebView to execute JavaScript code on the web page, which is often necessary for interactive and
dynamic web content.
webBrowse.scrollBarStyle = View.SCROLLBARS_INSIDE_OVERLAY
This line sets the scrollbar style of the webBrowse WebView. It specifies that the scrollbars should appear
inside the content, overlaid on top of the web page content. View.SCROLLBARS_INSIDE_OVERLAY is an
integer constant indicating this style.
webBrowse.loadUrl(url)
This line loads a URL (web page) into the webBrowse WebView. The URL to load is provided as the url
variable, which is a string containing the web address.
WebView and Web App in Localhost
To run your web application in XAMPP and display it in a WebView in Android Studio, follow these
steps:
Step 1: Set Up XAMPP
1. Install XAMPP: If you haven't already, download and install XAMPP from
the Apache Friends website.
2. Start Apache: Open the XAMPP Control Panel and start the Apache server. This will allow your
local web server to serve files.
3. Place Your Web Application: Copy your web application files (HTML, CSS, JavaScript, etc.)
into the htdocsdirectory of your XAMPP installation. For example, if you created a folder
named mywebapp, the path might look like C:\xampp\htdocs\mywebapp.
Access Your Web Application: Open a web browser and navigate
to http://localhost/mywebapp to ensure your web application is running properly.
WebView and Web App in Localhost
1. Create or Open Your Android Project: Start a new project or open an existing one in
Android Studio.
3. Set Up WebView in Layout: In your layout XML file (e.g., activity_main.xml), add
a WebView component:
xml
Copy code
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
WebView and Web App in Localhost
import android.os.Bundle
webView = findViewById(R.id.webView)
import android.webkit.WebView
webView.webViewClient = WebViewClient() // To
import android.webkit.WebViewClient
handle navigation
import androidx.appcompat.app.AppCompatActivity
1. Run Your Android App: Connect your Android device or start an emulator and run your
application from Android Studio.
2. Access the Web Application: The WebView should load your web application from
XAMPP using the URL http://192.168.0.2/mywebapp. The IP address 192.168.0.2 is a
special alias that points to localhost when running on an Android emulator.
WebView and Web App in Localhost
Additional Tips
If you want to use a physical device, ensure that the device is on the same network as
your computer, and replace 192.168.0.2 with your computer's local IP address (you can
find it using ipconfig on Windows or ifconfig on macOS/Linux).
If you're accessing the web application via a physical device, make sure your firewall is
configured to allow incoming connections to the Apache server.
Always check for errors in logcat if the WebView does not load your application as
expected.
AutoCompleteTextView
actView.threshold = 1
This line sets the threshold for showing autocomplete suggestions to 1 character.
In other words, the autocomplete suggestions will appear when the user types at least one character
in the AutoCompleteTextView.
actView.setAdapter(adapter)
This sets the adapter you created earlier as the adapter for the AutoCompleteTextView.
The adapter is responsible for providing suggestions as the user types in the AutoCompleteTextView.
DatePicker
Create a method with a listener for the Create a method to show the DatePicker
event button click: private fun showDatePickerDialog() {
private fun showDialogOnButtonClick() { val calendar = Calendar.getInstance()
btnDatePicker.setOnClickListener { val datePickerDialog =
showDatePickerDialog() DatePickerDialog(this, dpickerListener,
} calendar.get(Calendar.YEAR),
} calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH)
)
datePickerDialog.show()
}
DatePicker
Create another variable listener to display the selected date into a Toast:
private val dpickerListener = DatePickerDialog.OnDateSetListener { _, year, monthOfYear,
dayOfMonth ->
yearx = year
monthx = monthOfYear
dayx = dayOfMonth
Toast.makeText(this@MainActivity, "$yearx/$monthx/$dayx", Toast.LENGTH_LONG).show()
}
DatePickerDialog.OnDateSetListener { ... }
This code initializes the property with a lambda expression. The lambda expression serves as the implementation for
the DatePickerDialog.OnDateSetListener interface.
Inside the lambda expression:
{ _, year, monthOfYear, dayOfMonth -> ... } : This defines the lambda function. It takes four parameters:
_ : An underscore, which is a convention to ignore a parameter that you don't intend to use. In this case, it's used to
ignore the first parameter.
year: The selected year.
monthOfYear: The selected month (0-based, where 0 is January).
dayOfMonth: The selected day of the month.
yearx = year, monthx = monthOfYear, and dayx = dayOfMonth: These lines set the values of the yearx, monthx, and
dayx variables based on the selected date.
Toast.makeText(this@MainActivity, "$yearx/$monthx/$dayx", Toast.LENGTH_LONG).show(): This line creates a
toast message to display the selected date in the format "YYYY/MM/DD" (year, month, and day) and shows it to
the user. this@MainActivity refers to the current Activity or Context.
Fragment
if(view == findViewById(R.id.button)){
fragment = FragmentOne()
fragmentTransaction.replace(R.id.fragmentContainerView, fragment)
fragmentTransaction.commit()
}
if(view == findViewById(R.id.button2)){
fragment = FragmentTwo()
fragmentTransaction.replace(R.id.fragmentContainerView, fragment)
fragmentTransaction.commit()
}
}
Fragment Application
Variable Declaration:
1. var fragment: Fragment
- Declares a variable to hold a reference to the fragment that will be displayed.
2. val fragmentManager = supportFragmentManager
- Retrieves the FragmentManager associated with the hosting activity. The
supportFragmentManager is used for activities that extend AppCompatActivity.
3. val fragmentTransaction = fragmentManager.beginTransaction()
- Begins a new fragment transaction. This transaction will be used to replace the current
fragment.
Fragment Application
Fragment Replacement:
1. If the first button is clicked (R.id.button), it creates an instance of FragmentOne,
replaces the current fragment in the specified container
(R.id.fragmentContainerView) with FragmentOne, and commits the transaction.
2. If the second button is clicked (R.id.button2), it creates an instance of
FragmentTwo, replaces the current fragment in the specified container with
FragmentTwo, and commits the transaction.