Is there a way to disable Edittext contextmenuitem "Clipboard"? - android

I want to disable EditText contextmenuitem "Clipboard" to open a popup view. Whenever I long press edittext in my app contextmenuitem appears as "Paste, Clipboard". I want to retain the functionality of "Paste" option but "Clipboard" should not work. I have Made a Custom EditText class where I have callbacks for actions such as cut, paste,copy with respect to their Id. But I am unable to diable "Clipboard" function.
override fun onTextContextMenuItem(id: Int): Boolean {
// Do your thing:
val consumed = super.onTextContextMenuItem(id)
when (id) {
R.id.cut -> onCut()
R.id.copy -> onCopy()
R.id.paste, R.id.pasteAsPlainText -> {
onPaste()
}
android.R.id.keyboardView->{}
R.id.accessibilityActionContextClick->{}
R.id.accessibilityActionHideTooltip->{}
R.id.accessibilityActionImeEnter->{}
}
return consumed
}
Below picture shows the EditText options
and this picture shows when I Click Clipboard option

Related

jetpack compose Image on entire screen

I have a simple screen implemented with Jetpack Compose,
here is a preview of the screen.
There is a button like the sun on the TopAppBar which the user clicks to take a screenshot.
The problem is that I want to show the screenshot of the entire screen but without showing the status bar and the navigation bar.
Is there a solution to do this?
The composables are tied to a View which you can access with LocalView.current and through that View you can retrieve its .context and through that you can get to its Activity and to the Window object.
Once you have a reference to the Window (and to the View, if you have to support API level lower than 30), you can control status bar and navigation bar visibility.
A code example:
#Composable
fun MyFullScreenComposable() {
val view = LocalView.current
SideEffect {
requestFullScreen(view)
}
Box {
// ... other content
Button(
onClick = { requestFullScreen(view) }
) {
Text("Go fullscreen")
}
}
}
fun requestFullScreen(view: View) {
// !! should be safe here since the view is part of an Activity
val window = view.context.getActivity()!!.window
WindowCompat.getInsetsController(window, view).hide(
WindowInsetsCompat.Type.statusBars() or
WindowInsetsCompat.Type.navigationBars()
)
}
fun Context.getActivity(): Activity? = when (this) {
is Activity -> this
// this recursion should be okay since we call getActivity on a view context
// that should have an Activity as its baseContext at some point
is ContextWrapper -> baseContext.getActivity()
else -> null
}
In your case you might have to hide your action/title bar as well (or maybe you are already doing that).
Note that when the user taps on the screen while in full-screen mode, the bars will slide back. That's why I added a button as an example how you can let the user go back into full-screen mode through an action. In your case that could be a FloatingActionButton or some action on a semi-transparent top action bar.

Android/Kotlin - Fragment not attached to Activity after loosing focus on textfield by pressing navigation menu button

i've a problem in my Android app.
I have a fragment that is generated as a list of editable textview. Every textview has a setOnFocusChangeListener that calls an API on server.
FocusChangedListener works correctly on last textview, and after that my textview remain focused as you can see in the figure below.
Problem
Now i have to change menu (fragment) by clicking on the right menu button.
When button is clicked and the new menu is about to be loaded, my textview loses focus and calls the API, but i don't want my app do that. If I click menu button is because i've to change it and i expect that my old fragment disappear or basically don't execute the FocusChangedListener.
Any ideas?
I made a little trick to solve the problem.
I just added this code inside OnCreate of my DrawerMenu and then i set a Global Variable "drawerOpened" to check every time if my Menu is open or not.
// Initialize the action bar drawer toggle instance
val drawerToggle:ActionBarDrawerToggle = object : ActionBarDrawerToggle(
this,
drawer_layout,
toolbar,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close
){
override fun onDrawerClosed(view:View){
super.onDrawerClosed(view)
GlobalVar.drawerOpened = false
}
override fun onDrawerOpened(drawerView: View){
super.onDrawerOpened(drawerView)
GlobalVar.drawerOpened = true
currentFocus?.clearFocus() //clear focus - any view having focus
}
}
Then in my HomeFragment i did this:
// if GlobalVar.drawerOpened is false then setOnFocus
if (field.validatefield || field.nextpage != 0)
{
textView.setOnFocusChangeListener { _, hasFocus ->
if (!hasFocus && !GlobalVar.drawerOpened)
{
if ((field.mandatory && !textView.text.toString().isNullOrEmpty()) || (!field.mandatory)) {
if (field.validatefield) {
validateField(field.fieldcode, textView.text.toString(), field.nextpage)
} else {
_goToNextPageNo = field.nextpage
goToNextPageIfExist()
}
}
}
}
}
This is not a complete solution, but it works fine.
I assume that after a click, the application switches to touch mode and your EditText loses focus.
It happens at the system level.
A focus can have only one view at a time.
https://android-developers.googleblog.com/2008/12/touch-mode.html

EditText field never gets submitted

I got a RecyclerView with multiple EditText-fields. When I try to edit one of the EditText-fields and click enter on the virtual keyboard, the focus shifts down to the next EditText-field, something I don't want to happen. I want to submit the changes I made in the first EditText-field and then close the keyboard. I managed to turn off this focus-shifting by adding the following to my .xml file:
android:focusable="true"
android:focusableInTouchMode="true"
But the problem still persists, now the changes just never get submitted as my listener never gets called. If I remove all items except from one in my RecyclerView everything works like I want. How can I make that happen with more items in myRecyclerView too?
My bind function inside my UserCardItem.kt file;
override fun bind(viewHolder: ViewHolder, position: Int) {
...
viewHolder.itemView.creditcard_nickname.setOnEditorActionListener{ _, actionId, _ ->
if(actionId == EditorInfo.IME_ACTION_DONE){
saveNickname(viewHolder)
true
} else {
false
}
}
private fun saveNickname(viewHolder : ViewHolder){
val nickname = viewHolder.itemView.creditcard_nickname.text.toString()
userCreditcard.nickname = nickname
UserCardStore().updateNickname(userCreditcard)
}
Add android:imeOptions="actionDone" to your EditText in your layout XML.

Simulating a button press with another button press

I currently want to have a button that when I press it another button flashes as if it has been pressed at the same time and activates the function of the other button. My current code is as such:
fun onTwo(view: View) {
button1.callOnClick()
button1.isPressed = true
}
However the issue I am facing is that it freezes button1 as if it is pressed until it is pressed again. Anyone have a solution for this?
You could add listener in one of the button to check for clicks and then use that event to trigger click event in another button, like this:
val button1 = findViewById(R.id.btn1ID) as Button
button1.setOnClickListener {
val button2 = findViewById(R.id.btn2ID) as Button
button2.performClick()
}
Replace R.id.btn1ID and R.id.btn2ID with their respective id(a).
Reference: performClick()
You could also create a utility function to use it without making redundant variables like this:
#Suppress("UNCHECKED_CAST")
fun Activity.findButtonById(#IdRes res : Int) : Button =
findViewById(res) as Button
// and then in your create method of activity:
findButtonById(R.id.btn1ID).setOnClickListener {
findButtonById(R.id.btn2ID).performClick()
}
Try performClick() method like below:
fun onTwo(view: View)
{
button1.performClick()
}
I ended up fixing this issue using coroutines in the end as such:
fun onTwo(view: View){
GlobalScope.async{
delay(100)
button1.isPressed = false
GlobalScope.cancel()
}
button1.setPressed(true)
button1.performClick()
}

Set the button to Done instead of Go in EditText

I'm trying to switch the button in the softkey from "Go" to "Done" and vice-versa.
If I just set the imeoption
private fun showDoneOnSoftKeyboard() {
setImeOptionsOnSoftKeyboard(EditorInfo.IME_ACTION_DONE)
}
private fun showGoOnSoftKeyboard() {
setImeOptionsOnSoftKeyboard(EditorInfo.IME_ACTION_GO)
}
private fun setImeOptionsOnSoftKeyboard(imeOptions: Int) {
contractIdInput.imeOptions = imeOptions
}
the button is not changed. I've found that by doing:
private fun setImeOptionsOnSoftKeyboard(imeOptions: Int) {
val inputType = contractIdInput.inputType
contractIdInput.inputType = InputType.TYPE_NULL
contractIdInput.imeOptions = imeOptions
contractIdInput.inputType = inputType
}
the button is changed. The problem is though that the keyboard settings are reset that means that if I have for example the capslock set after I switch between states (for example from Done to Go) then the capslock is reset.
I have also tried
contractIdInput.imeOptions = imeOptions
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.restartInput(contractIdInput)
but this has the same effect.
I tried this one as well:
contractIdInput.setImeActionLabel("Custom text", KeyEvent.KEYCODE_ENTER)
but it does not work either.
Is there any other way to do the same?
It looks like this can't be done. The IME-options are thought to be set statically in the XML or programmatically but they can't be modified dynamically while the user is typing.

Categories

Resources