im new to Android development. I think i got kinda the basics, but after 8 workhours of not getting my DropDown above my WebView, i tought i gotta ask.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val languages = resources.getStringArray(R.array.array1)
val webview = WebView(this)
setContentView(webview)
webview.settings.javaScriptEnabled = true
webview.loadUrl("http://localhost/test/neu/index.php")
// access the spinner
val spinner = findViewById<Spinner>(R.id.spinner1)
if (spinner != null) {
val adapter = ArrayAdapter(this,
android.R.layout.simple_spinner_item, languages)
spinner.adapter = adapter
spinner.onItemSelectedListener = object :
AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>,
view: View, position: Int, id: Long) {
Toast.makeText(this@MainActivity,
getString(R.string.selected_item) " "
"" languages[position], Toast.LENGTH_SHORT).show()
}
override fun onNothingSelected(parent: AdapterView<*>) {
// write code to perform some action
}
}
}
}
It does what it's supposed to do, the only thing is it places the Spinner below the WebView. If i comment out line 5-8 inside the onCreate(), it shows me a white screen with only the Spinner.
Looking for Answers to this question just brings me SO threads on how to make a Progress bar, which i dont get, but w/e. Thanks in advance
My Manifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ttermscannerandroid51">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.TTermScannerAndroid51">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
at last my activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:id="@ id/WebView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.377"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<Spinner
android:id="@ id/spinner1"
android:layout_width="100dp"
android:layout_height="100dp"
android:spinnerMode="dropdown" />
</WebView>
</androidx.constraintlayout.widget.ConstraintLayout>
CodePudding user response:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Spinner
android:id="@ id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:spinnerMode="dropdown"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<WebView
android:id="@ id/WebView"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/spinner1" />
</androidx.constraintlayout.widget.ConstraintLayout>
CodePudding user response:
Don't nest your Spinner
inside the Webview
, just give it constraints so it sits where you want.
I'm not sure if you mean you want it vertically above the WebView, or overlaid - if it's above, constrain the top of the spinner to the top of the parent, and the top of the webview to the bottom of the spinner:
<Spinner
android:id="@ id/spinner1"
...
app:layout_constraintTop_toTopOf="parent"/>
<WebView
android:id="@ id/WebView"
...
app:layout_constraintTop_toBottomOf="@id/spinner1"/>
If you want it overlaid, constrain the top of the spinner to the top of the webview, etc:
<WebView
android:id="@ id/WebView"
...
app:layout_constraintTop_toTopOf="parent"/>
<Spinner
android:id="@ id/spinner1"
...
app:layout_constraintTop_toTopOf="@id/WebView"/>
Now the spinner is relative to the WebView, and you can use margins, constraints etc to position it relative to the WebView's edges. When overlaying, order matters, but you can give it an elevation
value to explicitly move it higher, so it displays over the WebView