how to detect when a button is held? - android

I want to perform the actions when the button is pressed not just clicked
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var ib:ImageView=findViewById(R.id.imageView4) as ImageView
var final=MediaPlayer.create(this,R.raw.`m1`)
var anim=AnimationUtils.loadAnimation(this,R.anim.rotate)
ib.setOnTouchListener(){
textView.text="rip"
final.start();
ib.startAnimation(anim);
}
}
}
I want the actions to be performed when the button is held and stop when they are not .

this seems to be working fine but the compiler gives a warning
ib.setOnTouchListener(View.OnTouchListener { view, motionEvent ->
when (motionEvent.action){
MotionEvent.ACTION_DOWN -> {
textView4.text="IGNORING"
ib.startAnimation(anim)
final.setLooping(true)
final.start()
}
MotionEvent.ACTION_UP -> {
textView4.text="IGNORE"
ib.clearAnimation()
final.pause()
}
}
return#OnTouchListener true
})

Related

Android Kotlin Click Event for Back Button in Action Bar

I try to get action after pressing back button in top toolbar
class TagsFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
(activity as AppCompatActivity?)?.supportActionBar?.title = "$selectedItemText Tags"
(activity as AppCompatActivity?)?.supportActionBar?.setDisplayHomeAsUpEnabled(true)
// This callback will only be called when MyFragment is at least Started.
val callback = requireActivity().onBackPressedDispatcher.addCallback(this) {
Log.d(InTorry.TAG, "TagsFragment: back BTN Pressed")
}
}
}
Unfortunately, it doest log anything
I found I should add OnBackPressedCallback but it doesnt work as well :
class TagsFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val selectedItemText = arguments?.getString("selectedItemText")//get arguments
(activity as AppCompatActivity?)?.supportActionBar?.title = "$selectedItemText Tags"
(activity as AppCompatActivity?)?.supportActionBar?.setDisplayHomeAsUpEnabled(true)
(activity as AppCompatActivity?)?.onBackPressedDispatcher?.addCallback(
this,
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
Log.d(InTorry.TAG, "Fragment back pressed invoked")
// Do custom work here
// if you want onBackPressed() to be called as normal afterwards
if (isEnabled) {
isEnabled = false
requireActivity().onBackPressed()
}
}
}
)
}
Kind regards
Jack
put this in onCreate method
getActionBar().setDisplayHomeAsUpEnabled(true);
//and then override this method and find id.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
I know sometimes it is painful when android just deprecates on of their on Event functions ,Anyways this is your code :
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getActivity().onBackPressed();
}
});
and you can just remove the onClickListener Interface and shortcut it if you desire like this :
toolbar.setNavigationOnClickListener {
requireActivity().onBackPressedDispatcher.onBackPressed()
}
should be working fine
Thank you all
The code I use now:
For device back button, in Activity onCreate()
class HomeActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
setSupportActionBar(findViewById(R.id.toolbar))
supportActionBar?.setDisplayHomeAsUpEnabled(true)
onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
Log.d(InTorry.TAG,"device back btn click")
//finish()
}
})
Toolbar Back Button arrow - Now I know that it works like one big menu
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
android.R.id.home -> {// this is my back button - simply home
Log.d(InTorry.TAG,"Toolbar Back BTN Click")
...
}
R.id.top_menu_settings -> {
Log.d(InTorry.TAG,"top_menu_settings click")
true
}
R.id.top_menu_logout -> {
Log.d(InTorry.TAG,"top_menu_logout click")
Firebase.auth.signOut()
gotoLogin()
true
}
else -> super.onOptionsItemSelected(item)
}
}

Disable 2 finger taps on button

I want to prevent a button from intercepting 2 finger taps. I have a standard Android Activity with onTouchEvent and a standard button which occupies more or less the entire screen.
private const val DEBUG_TAG = "Gestures"
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = this.findViewById<Button>(R.id.button2)
button.setOnClickListener { Log.d(DEBUG_TAG, "Button tapped")
}
}
override fun onTouchEvent(event: MotionEvent): Boolean {
Log.d(DEBUG_TAG, "onTouchEvent")
val action: Int = MotionEventCompat.getActionMasked(event)
val pointerCount = event.pointerCount;
if (pointerCount > 1) {
Log.d(DEBUG_TAG, "Multi-touch")
}
return super.onTouchEvent(event)
}
}
I want the button to intercept 1 finger taps but ignore 2 finger taps and propagate them to the onTouchEvent of the activity. Currently it intercepts even 2 finger taps.
I tried this in styles.xml but no luck.
<item name="android:windowEnableSplitTouch">false</item>
<item name="android:splitMotionEvents">false</item>
Ideally I would like to do that app-wide but still having onTouchEvent being able to detect 2 finger taps.
You need to block events you don't want to propagate:
override fun onTouchEvent(event: MotionEvent): Boolean {
val action: Int = MotionEventCompat.getActionMasked(event)
val pointerCount = event.pointerCount;
return when (pointerCount) {
1 -> {
// your logic
true //true means you consumed the event (stop propagation)
}
else -> super.onTouchEvent(event)
}
}

Can't assign values outside onTouchListener

when a item is pressed I want to modify a value declared outside the onCreate method. But when the item is pressed, the onTouchListener does the other functions except modify that value. I've tried to write the same instruction to modify the value outside the onTouchListener method and it worked. This is my code:
class MainActivity : AppCompatActivity() {
var ciao: String? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
var listenerUser = View.OnTouchListener(function = { view, motionEvent ->
if (motionEvent.action == MotionEvent.ACTION_DOWN) {
this.ciao = "ciao"
}
true
})
}
override fun onResume() {
super.onResume()
if (ciao != null) {
println(ciao!!)
}
}
}
When my application resumes the var ciao is null, but if I put this.ciao = "ciao" outside the listener the var isn't null.
You need to set your OnTouchListener for the view that should react:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val listener = View.OnTouchListener(function = {view, motionEvent ->
if (motionEvent.action == MotionEvent.ACTION_DOWN) {
this.ciao = "ciao"
}
true
})
clickedView.setOnTouchListener(listener)
}
If you just want to detect a click you are probably better off using a OnClickListener like this:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
clickedView.setOnClickListener(_ ->
this.ciao = "ciao"
)
}

Expand and collapse a colapsingToolBar with a button in Kotlin

I want to expand and collapse a CollapsingToolBar in android studio
when i click a button.
I want it collapsed by dafault. Im doing something wrong and i dont know what, thanks for the help.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//maybe this doesnt go here, im trying to make it collapse by default
var start = appbar.setExpanded(false)
button.setOnClickListener {
if(start == appbar.setExpanded(false)){
start = appbar.setExpanded(true)
}else{
start = appbar.setExpanded(false)
}
}
}
Try this:
class MainActivity : AppCompatActivity() {
private var isAppbarExpanded = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
if(!isAppbarExpanded){
appbar.setExpanded(true)
isAppbarExpanded = true
}else{
appbar.setExpanded(false)
isAppbarExpanded = false
}
}
}

Kotlin edittxt.onKeyListener with emulator keyboard

I'm trying to reset a button to become clickable once a key on the keyboard is pressed. It works with the hardware keyboard but not with the emulator keyboard. How would one implement this?
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button = findViewById<Button>(R.id.log_in_button)
val email = findViewById<EditText>(R.id.email_field)
button.isClickable = false
button.alpha = .5f
email.setOnKeyListener(object : View.OnKeyListener {
override fun onKey(v: View, keyCode: Int, event: KeyEvent): Boolean {
if(event.action == KeyEvent.ACTION_DOWN) {
button.isClickable = true
button.alpha = 1f
return true
}
return false
}
})
}

Categories

Resources