Reputation: 2157
I would like to show my web content in dialog. It can be ordinary Dialog()
or DialogFragment
. I have created layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
and then firstly created Fragment
which extends DialogFragment()
:
class ShowJob : DialogFragment(), View.OnClickListener {
var listPosition: Int? = 0
var offset: Int? = 0
lateinit var link: String
val TAG = ShowJob::class.java.simpleName!!
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val bundle = arguments
offset = bundle!!.getInt("jobOffset")
link = bundle.getString("link")
listPosition = bundle.getInt("listPosition", 0)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.show_job_scr,container,false)
val webView = view.findViewById<WebView>(R.id.webView)
webView.loadUrl(link)
webView.webViewClient = HelloWebViewClient()
WebView.setWebContentsDebuggingEnabled(false)
return view
}
private inner class HelloWebViewClient : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(intent)
return true
}
}
override fun onClick(p0: View?) {
}
}
it doesn't show anything and redirect me to in-built browser like Chrome. Then I read that sometimes changing type of root layout type can help and changed my RelativeLayout
to FrameLayout
- it didn't help. Then I thought that maybe problem is connected with all layout and placed textView
with some text and it was shown in my fragment. Second variant - I showed Dialog()
from my parent fragment from which I tried to show DialogFragment
:
val webDialog = Dialog(context)
webDialog.setContentView(R.layout.show_job_scr)
webDialog.setCancelable(true)
val webView = webDialog.findViewById<WebView>(R.id.webView)
!webView.isScrollbarFadingEnabled
!webView.isHorizontalScrollBarEnabled
webView.settings.javaScriptEnabled
webView.clearCache(true)
webView.loadUrl(url)
webDialog.show()
and it also redirected me to in-built browser. Where is my problem and how I can solve it? I saw this example and here developer managed to show content in dialog. I saw a lot of examples and everywhere people managed to show web content in dialogs but I have some problems with redirecting.
Upvotes: 0
Views: 145
Reputation: 2157
I have solved my problem with a solution which was published at this question:
webView.webViewClient = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
view.loadUrl(url)
return false
}
}
I removed inner class and added these lines in onCreateView
.
Upvotes: 0
Reputation: 51
// this is the reason webView.webViewClient = HelloWebViewClient()
private inner class HelloWebViewClient : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean
{
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(intent)
return true
}
}
Upvotes: 1