override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.action_delete_all -> {
val intent = Intent(this, StatsActivity::class.java)
startActivity(intent)
true
}
else -> super.onOptionsItemSelected(item)
}
}
Related
I have a NestedScrollView contain a RecyclerView, each adapter item have
TextView one
ImageView
TextView two
and I want to custom TextView select action use kt ext
fun TextView.customMenuCallBack() {
var selectAll = false
customSelectionActionModeCallback = object : ActionMode.Callback {
override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
return true
}
override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {
return true
}
override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
item?.let {
return when (it.itemId) {
android.R.id.selectAll -> {
selectAll = true
false
}
android.R.id.copy -> {
if (selectAll) {
//TODO set content
try {
val clipboard: ClipboardManager = context.getSystemService(AppCompatActivity.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText(context.toString(), content)
clipboard.setPrimaryClip(clip)
} catch (e: Exception) {
e.printStackTrace()
}
}
//make custom action work return true
true
}
else -> {
false
}
}
}
return false
}
override fun onDestroyActionMode(mode: ActionMode?) {
}
}
}
I appear some issues:
menu not hide after click menu item
Affects scrolling, when scrolling forward it suddenly back to nearby selected text position
Not workinig in MIUI ROM
I have fixed part of issues
menu not hide after click menu item
Affects scrolling, when scrolling forward it suddenly back to nearby selected text position
fun TextView.customMenuCallBack() {
var selectAll = false
customSelectionActionModeCallback = object : ActionMode.Callback {
override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
return true
}
override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {
return true
}
override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
item?.let {
return when (it.itemId) {
android.R.id.selectAll -> {
selectAll = true
false
}
android.R.id.copy -> {
if (selectAll) {
//TODO set content
try {
val clipboard: ClipboardManager = context.getSystemService(AppCompatActivity.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText(context.toString(), content)
clipboard.setPrimaryClip(clip)
} catch (e: Exception) {
e.printStackTrace()
}
}
//fixed first issue
mode?.finish()
//fixed second issue
clearFocus()
//make custom action work return true
true
}
else -> {
false
}
}
}
return false
}
override fun onDestroyActionMode(mode: ActionMode?) {
}
}
}
update: Custom my NestedScrollView2:NestedScrollView and override
override fun requestChildFocus(child: View?, focused: View?) {
//do nothing prevent focused issued
}
Previously I used onCreateOptionsMenu and onSupportNavigationUp.
Now I use MenuProvide. To check if navigation up was clicked I check id - "android.R.id.home". While it works, I'm not sure this is the right approach?
override fun onCreate(savedInstanceState: Bundle?) {
WindowCompat.setDecorFitsSystemWindows(window, false)
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
setSupportActionBar(binding.toolbar)
val navController = findNavController(R.id.nav_host_fragment_content_main)
appBarConfiguration = AppBarConfiguration(navController.graph)
setupActionBarWithNavController(navController, appBarConfiguration)
addMenuProvider(object : MenuProvider {
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
menuInflater.inflate(R.menu.menu_main, menu)
}
override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
Log.d(TAG, "onMenuItemSelected: {${menuItem.itemId}}")
return when (menuItem.itemId) {
android.R.id.home -> {
navController.navigateUp(appBarConfiguration)
}
else -> {
true
}
}
}
})
}
I am trying to go to another activity, but my setting activity is preventing to go there. How do I fix that?
class MainActivity : AppCompatActivity() {
private var gettext: EditText? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
gettext = findViewById(R.id.text2)
// val intent = intent
val action = intent.action
val type = intent.type
val button = findViewById<Button>(R.id.button_submit)
//this part is what I want to go to another activity, while still have setting activity
button.setOnClickListener {
val intent = Intent(this,com.example.testing1.DecryptVerify::class.java)
startActivity(intent)
}
if (Intent.ACTION_SEND == action && type != null) {
handleSendText(intent)
}
}
private fun handleSendText(intent: Intent) {
val sharedText = intent.getStringExtra(Intent.EXTRA_TEXT)
if (sharedText != null) {
// showing the text in edittext
gettext?.setText(sharedText)
}
}
//I want to stay, while when I run it and click button on the top one
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.action_settings -> {
val intent = Intent(this, com.example.testing1.SettingsActivity::class.java
)
startActivity(intent)
}
}
return super.onOptionsItemSelected(item)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
val menuInflater = menuInflater
menuInflater.inflate(R.menu.main_menu, menu)
return super.onCreateOptionsMenu(menu)
}
}
Do I need to some form if-else?
I'm using a navigation architecture component and from drawer, I want to open share intent?
class MainActivity : AppCompatActivity() {
override fun onNavigationItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.action_share_app -> {
val sendIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, "This is my text to send.")
type = "text/plain"
}
startActivity(sendIntent)
return true
}
}
return true
}
override fun onCreate(savedInstanceState: Bundle?) {
setTheme(R.style.AppTheme_NoActionBar)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
// navigationView.setNavigationItemSelectedListener(this)
setupNavigation()
}
private fun setupNavigation() {
val navController = findNavController(this, R.id.nav_host_fragment)
// Update action bar to reflect navigation
setupActionBarWithNavController(this, navController, drawerLayout)
// Handle nav drawer item clicks
ActivityNavigator(this)
.createDestination().intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, "This is my text to send.")
type = "text/plain"
}
navigationView.setNavigationItemSelectedListener { menuItem ->
when (menuItem.itemId) {
R.id.action_share_app -> {
val sendIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, "This is my text to send.")
type = "text/plain"
}
startActivity(sendIntent)
}
}
menuItem.isChecked = true
drawerLayout.closeDrawers()
false
}
// Tie nav graph to items in nav drawer
setupWithNavController(navigationView, navController)
}
override fun onSupportNavigateUp(): Boolean {
return navigateUp(findNavController(this, R.id.nav_host_fragment), drawerLayout)
}
i'm stuck in my application because i have to do a fragment with the navigation drawer in kotlin.. can anyone help? I have search a lot of things in web but i didn't find anything yet...
Down here is two items that i have do in main activity and i want that is present in the fragment too.
override fun onNavigationItemSelected(item: MenuItem): Boolean {
// Handle navigation view item clicks here.
when (item.itemId) {
R.id.home -> {
}
R.id.subjects -> {
val intent = Intent(this, SubjectsActivity::class.java)
startActivity(intent)
}
}
drawer_layout.closeDrawer(GravityCompat.START)
return true
}
You can use this to replace your frame with HomeFragment()
mainDisplayFrame should be replace by your FrameID
var fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.mainDisplayFrame,HomeFragment()).commit()
mDrawerLayout!!.closeDrawers()
You can download the source link from here(Navigation Drawer In Android Kotlin)
MainActivity.kt:
class MainActivity : AppCompatActivity(), View.OnClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
init()
}
private fun init() {
tv_home.setText("Home Tab")
tv_title.setText("Home")
ll_about.setOnClickListener(this)
ll_home.setOnClickListener(this)
ll_bookmark.setOnClickListener(this)
ll_help.setOnClickListener(this)
rl_menu.setOnClickListener(this)
rl_header.setOnClickListener(this)
tv_link.setLinkTextColor(Color.parseColor("#000000"));
Linkify.addLinks(tv_link, Linkify.ALL)
}
override fun onBackPressed() {
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
}
override fun onClick(p0: View?) {
when (p0?.id) {
R.id.ll_home -> {
drawer_layout.closeDrawer(GravityCompat.START)
tv_home.setText("Home Tab")
tv_title.setText("Home")
}
R.id.ll_about -> {
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse("http://deepshikhapuri.blogspot.in/"))
startActivity(browserIntent)
}
R.id.ll_help -> {
drawer_layout.closeDrawer(GravityCompat.START)
tv_home.setText("Help Tab")
tv_title.setText("Help")
}
R.id.ll_bookmark -> {
drawer_layout.closeDrawer(GravityCompat.START)
tv_home.setText("Bookmark Tab")
tv_title.setText("Bookmark")
}
R.id.rl_header -> {
}
R.id.rl_menu -> {
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
} else {
drawer_layout.openDrawer(GravityCompat.START)
}
}
}
}
Thanks!
Is something like this? #NongthonbamTonthoi
class NavViewFragment : AppCompatActivity(),
NavigationView.OnNavigationItemSelectedListener {
fun AppCompatActivity.addFragment(fragment: Fragment, frameId: Int){
supportFragmentManager.inTransaction { add(frameId, fragment) }
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_nav_view_fragment)
setSupportActionBar(toolbar)
nav_view.setNavigationItemSelectedListener(this)
}
override fun onBackPressed() {
if (drawer_layout.isDrawerOpen(GravityCompat.START)) {
drawer_layout.closeDrawer(GravityCompat.START)
} else {
super.onBackPressed()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.nav_view, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
when (item.itemId) {
R.id.action_settings -> return true
else -> return super.onOptionsItemSelected(item)
}
}
override fun onNavigationItemSelected(item: MenuItem): Boolean {
// Handle navigation view item clicks here.
when (item.itemId) {
R.id.home -> {
}
R.id.subjects -> {
val intent = Intent(this, SubjectsActivity::class.java)
startActivity(intent)
}
}
drawer_layout.closeDrawer(GravityCompat.START)
return true
}
inline fun FragmentManager.inTransaction(func: FragmentTransaction.() -> Unit) {
val fragmentTransaction = beginTransaction()
fragmentTransaction.func()
fragmentTransaction.commit()
}
}