Issue
Summary
Trying to get simple buttons to work in a Wear OS application. Tried it in Kotlin, and Java. Neither worked. Got it working using Kotlin once a while back, but scrapped the project. Now, I am unable to get any click or press events to work.
The button itself isn't even showing any visual verification that it has been pressed. It's just static.
edit: When clicking anywhere on the simulator, it throws this error:
Cancelling event due to no window focus: MotionEvent
What I've tried
Aside from my code, I've made a couple of troubleshooting attempts. I've changed the AVD watch, changed the API level, restarted Android Studio, restarted the whole computer.
Heres my coding attempts:
I've tried setting a listener:
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println("TEST");
}
});
I've tried defining the function in XML and defining function in main activity (see onClick):
Main activity:
public class MainActivity extends WearableActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Enables Always-on
setAmbientEnabled();
}
public void onClick(View view){
System.out.println("BUTTON CLICKED!");
}
}
XML:
<Button
android:id="@+id/button"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:text="Button"
android:clickable="true"
android:onClick="onClick"/>
None of these methods resulted in the button providing any visual indication that it had been pressed. No logcat from the functions. Just a static watch screen.
Solution
Check your logs for these - Cancelling event due to no window focus: MotionEvent { action=ACTION_CANCEL, actionButton=0, id[0]=0, x[0]=152.99072, y[0]=111.987305, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=151077888, downTime=151077759, deviceId=0, source=0x1002 } or similar (dropping event).
If you see them, click the back button on the simulator to go the screen that shows your app and other apps like settings etc ( I don't know what that screen is called), bring your app up from there and now it will work... once. If this is the case, the only workaround is to use OnKeyDown. Or if this is not the case, let me know, as it would be another mysterious bug to add to wear os.
Adding animated gif to show what happens (althogh the cursor is not captured, but you can see no reaction on the button at first (and I am clicking it) and visual changes after bring it back)
Answered By - Dmitri
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.