Issue
I'm learning how to use Android Studio and the Kotlin language. I created a toolbar in which there are two buttons (search and share). I created an activity where the "search" button works and another activity where the "share" button works. Now, I would like to create an activity where both buttons work. What should I do?
//toolbar
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/main_menu"
android:theme="@style/Theme.Training">
<item
android:id="@+id/search"
android:icon="@drawable/ic_baseline_search_24"
android:title="@string/search"
app:showAsAction="ifRoom|withText"
app:actionViewClass="android.widget.SearchView"/>
<item
android:id="@+id/share"
app:showAsAction="ifRoom"
android:title="@string/share"
android:icon="@drawable/ic_baseline_share_24" />
</menu>
//search
package com.example.training
import android.R.layout.simple_list_item_1
import android.os.Bundle
import android.view.Menu
import android.view.View
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.SearchView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.MenuItemCompat
class SearchActivity : AppCompatActivity() {
private var listView: ListView? = null
var list: ArrayList<String>? = null
var adapter: ArrayAdapter<String>? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_search)
listView = findViewById<View>(R.id.listafrutta) as ListView
list = ArrayList()
list!!.add("Mango")
list!!.add("Avocado")
list!!.add("Papaya")
list!!.add("Kiwi")
list!!.add("Maracuja")
list!!.add("Ananas")
list!!.add("Lici")
list!!.add("Banana")
adapter = ArrayAdapter(this, simple_list_item_1, list!!)
listView!!.adapter = adapter }
override fun onCreateOptionsMenu(menu: Menu): Boolean {
val inflater = menuInflater
inflater.inflate(R.menu.main_menu, menu)
val searchViewItem = menu.findItem(R.id.search)
val searchView = MenuItemCompat.getActionView(searchViewItem) as SearchView
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String): Boolean {
searchView.clearFocus()
if(list?.contains(query)!!){
adapter?.filter?.filter(query)
} else {
Toast.makeText(applicationContext, "No Match found",Toast.LENGTH_LONG).show()
}
return false
}
override fun onQueryTextChange(newText: String): Boolean {
adapter!!.filter.filter(newText)
return false
}
})
return super.onCreateOptionsMenu(menu)
}
}
//share
package com.example.training
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
class ShareActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_share)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.main_menu, menu)
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { // uso switch case perché è possibile avere più icone
R.id.share -> {
val sharingIntent = Intent(Intent.ACTION_SEND)
sharingIntent.type = "text/plain" // tipo di content da condividere
val shareBody = "Body message" // Body del content
val shareSubject = "Subject Here" // puoi condividere qualsiasi cosa
sharingIntent.putExtra(Intent.EXTRA_TEXT, shareBody) // qui passo il body
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, shareSubject) // e poi passo il subject
startActivity(Intent.createChooser(sharingIntent, "Share using"))
}
}
return super.onOptionsItemSelected(item)
}
}
Solution
In order to have both the searchView and the share button on the toolbar your activity must look like the this:
class MainActivity : AppCompatActivity() {
private var listView: ListView? = null
var list: ArrayList<String>? = null
var adapter: ArrayAdapter<String>? = null
lateinit var searchView: SearchView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
listView = findViewById<View>(R.id.listafrutta) as ListView
list = ArrayList()
list!!.add("Mango")
list!!.add("Avocado")
list!!.add("Papaya")
list!!.add("Kiwi")
list!!.add("Maracuja")
list!!.add("Ananas")
list!!.add("Lici")
list!!.add("Banana")
adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, list!!)
listView!!.adapter = adapter
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu, menu)
val searchViewItem = menu.findItem(R.id.search)
searchView = MenuItemCompat.getActionView(searchViewItem) as SearchView
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String): Boolean {
searchView.clearFocus()
if (list?.contains(query)!!) {
adapter?.filter?.filter(query)
} else {
Toast.makeText(applicationContext, "No Match found", Toast.LENGTH_LONG)
.show()
}
return true
}
override fun onQueryTextChange(newText: String): Boolean {
adapter?.filter
adapter!!.filter.filter(newText)
return false
}
})
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.share -> {
val sharingIntent = Intent(Intent.ACTION_SEND)
sharingIntent.type = "text/plain" // tipo di content da condividere
val shareBody = "Body message" // Body del content
val shareSubject = "Subject Here" // puoi condividere qualsiasi cosa
sharingIntent.putExtra(Intent.EXTRA_TEXT, shareBody) // qui passo il body
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, shareSubject) // e poi passo il subject
startActivity(Intent.createChooser(sharingIntent, "Share using"))
true
}
else -> {
super.onOptionsItemSelected(item)
}
}
}
}
So basically you are taking both implemenations of yours and combine them in one. Notice that we have created a global variable that is called searchView. We created a global variable in order to have access to the created view in the onOptionsItemSelected
.
And this is how my menu looks like :
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/main_menu">
<item
android:id="@+id/search"
android:icon="@android:drawable/ic_search_category_default"
android:title="search"
app:showAsAction="always"
app:actionViewClass="android.widget.SearchView"/>
<item
android:id="@+id/share"
app:showAsAction="always"
android:title="share"
android:icon="@android:drawable/ic_menu_share" />
</menu>
I used the app:showAsAction="always"
so that they both are displayed always. Its not considered a good practice but I think its necessary.
Let me know if you need more help
Answered By - Pako1
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.