I'm looking for a technique to determine in Java if the Android Wear device screen is round or rectangular. Note that this isn't just about layouts; my code actually needs to know which shape it's working with, because they're handled differently.
As far as I can see from code samples online, two different approaches should be possible - but I've been unable to get either of them to work. I'll include them here to eliminate them from the running, or for possible troubleshooting (if anyone can see the problem with them). Please don't refer me to another SO post that just reiterates the solutions that aren't working for me here.
Note that all code here is running on the watch. Also, I'm still using Eclipse, FWIW.
The most straightforward method I've seen involves adding an onApplyWindowInsets() listener to a view in my layout. So I created a listener that looks like this:
#Override
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
if (insets.isRound()) {
displayShape = "round";
} else {
displayShape = "rectangular";
}
return null;
}
and added it to the root view of my layout with code like this:
view.setOnApplyWindowInsetsListener(this);
in my onCreate() method. Looks OK as far as it goes - but the listener never gets called. I also found advice saying that I needed to invoke it manually, as such:
view.requestApplyInsets();
but that didn't seem to make any difference. I've experimented with putting it on different views, in different lifecycle methods, and so forth, but never once saw it actually get called in my app. This is running on my LG G Watch, BTW.
The second approach is something of a hack, and is based on the published WatchViewStub helper class. I jumped through the hoops to get the wearable support library imported into an Eclipse project, then added the following to my root layout:
<android.support.wearable.view.WatchViewStub
android:id="#+id/watch_view_stub"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:rectLayout="#layout/rect"
app:roundLayout="#layout/round"
/>
and created rect.xml as such:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/layout_type"
android:text="rectangular"
/>
and round.xml like this:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/layout_type"
android:text="round"
/>
Finally, in my onCreate() I added the following Java code:
final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
#Override
public void onLayoutInflated(WatchViewStub stub) {
TextView layoutType = (TextView) findViewById(R.id.layout_type);
displayShape = layoutType.getText().toString();
}
});
It's a long way around the block, but it should work, right? Not so much... displayShape is always set to "rectangular", indicating that it's always rect.xml that gets used, even when running on a round emulator. [I don't have round-screened hardware to try it on just yet.]
So does anyone see where I've gone wrong with either of these two approaches? Or can you suggest a third way which actually works?
After several days spent chasing false leads, I've finally found the answer. It turns out that it's the android:theme of the application in the manifest that makes the difference.
In order for WatchViewStub to use the correct rect/round layouts, it appears that your application must use #android:style/Theme.DeviceDefault as its theme. Here's an example:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.DeviceDefault">
I expect it would also work if you used a theme that inherited from DeviceDefault, though I haven't tested that. But it appears that if you use any other custom theme, WatchViewStub will not function correctly.
#WaynePiekarski, it'd be nice if this was documented somehere.
Also, here are a couple of other tips that I learned along the way:
The rectangular layout always inflates before the round layout; IOW, on a round device, you'll get two onLayoutInflated() callbacks. This is kind of a pain if you're using the layout inflation to get the screen shape into your Java code, and that turns out to be necessary, because...
Calling setOnApplyWindowInsetsListener() on WatchViewStub prevents the round layout from loading at all (at least in my testing). So if you try to use this callback to determine the screen shape, round devices will still get the square layout.
Finally, a bonus question: Is there any good reason why Android Wear doesn't just report its screen shape as a resource qualifier? You know, like -land, -large, and so on. Why on earth do we need to mess around with WatchViewStub at all?
I'm not sure why it's necessary in your case to employ a callback, but an answer the general question of screen shape is here: Is there any way to detect if the clock is round?
That is, to acquire the current context, and test
context.getResources().getConfiguration().isScreenRound()
In CanvasWatchFaceService.Engine their is a overide method available setOnApplyWindowInsets you can check whether insets is round or square
#Override
public void onApplyWindowInsets(WindowInsets insets) {
super.onApplyWindowInsets(insets);
if(insets.isRound()){
//round
}
else{
//square
}
}
https://plus.google.com/+NicolasPomepuy/posts/ZJ3KZK6uu2e#+NicolasPomepuy/posts/ZJ3KZK6uu2e
and from https://github.com/PomepuyN/WatchviewStubIssue/blob/bcad0de7fa473c757dc27f9dfe65e31561c6097f/wear/src/main/java/com/example/watchviewstubissue/ViewService.java
mainView.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
51 #Override
52 public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
53 if (insets.isRound()) {
54 Log.d("ViewService", "Round");
55 } else {
56 Log.d("ViewService", "Square");
57 }
58 return insets;
59 }
60 });
The diff between your code and his is that you are returning null and he is returning insets.
Related
In code:
function focusOnMainWindow(): void {
win.focus();
}
In practice:
I want my electron application to appear above other programs. But only the yellow glow from him works! Why?
win.focus() isn't necessarily designed to bring the window to the front (see this github issue).
If you want the window to be brought to the front, you'll have to get more creative. The function I have in my app is fairly complex to handle all sorts of edge cases, but maybe something like this will get you started:
// maybe you want to handle this case, maybe not
if (win.isMinimized())
win.restore();
win.setAlwaysOnTop(true);
app.focus();
win.setAlwaysOnTop(false);
The idea is adapted from here. Note in their case, they're doing:
win.setAlwaysOnTop(true);
win.show();
win.setAlwaysOnTop(false);
app.focus();
So my goal is to create a ProgressBar with rounded corners. This seems to be way harder than what it should be. I've tried looking at examples on StackOverflow but most of them are outdated or is referring to Android Studio which is NOT what I am working with.
As far as I know, I need to create a whats called "Custom Renderer" which is essentially a class that inherits from ProgressBarRenderer. And then change most of the properties there, which seems kinda silly since the UI should be done using XAML in my opinion.
I keep running into multiple issues when trying to create the custom renderer.
The first one being this
I've already installed Xamarin.Forms and it still keeps throwing that error.
And then there is a issue regarding the constructor. The constructor that doesn't take any parameters is obsolete and doesn't work anymore and it requires me to add a Context which I can't either because it throws the same error as above.
How do I properly create a progressbar with rounded corners and text in the middle indicating how much % it's at?
You should add proper reference to use the ProgressBarRenderer:
using App498.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
The custom renderer should look like this:
[assembly: ExportRenderer(typeof(Xamarin.Forms.ProgressBar), typeof(myProgressBarRenderer))]
namespace App498.Droid
{
public class myProgressBarRenderer : ProgressBarRenderer {
public myProgressBarRenderer(Context context) : base(context)
{
}
protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.ProgressBar> e)
{
base.OnElementChanged(e);
}
}
}
If you want to add round corner to progress bar, check this thread.
I would refer you to SkiaSharp, graphics library, that enables you to create... well everything.
See the documentation here:
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/
You'll need to play around for a while to get used to work with it, but it's quite intuitive and easy to work with.
To achieve round corners, you'll need to work with paths, described in here:
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/curves/clipping
By a chance I also needed a progress bar in my app. I've drawn a picture with transparent line within for the progress to be shown, merged it with green rectangle (the progress itself) and voila, you've got the progress bar. When you need to update the progress, you just redraw the progress bar.
There are surely plenty of other solutions, but this one I reccomend as I know it will work fine.
Hapyy coding ;)
I have been working on figuring out what is going on with my game's UI for at least two days now, and no progress.
Note that this is a mobile game, but I was asked to build for Windows for visualization and presentation purpose.
So the problem is that when I run my game on the Unity Editor, Android, iOS and Mac platforms the UI works just perfect, but then when I run the game on Windows the UI still works fine UNTIL I load a specific scene.
This specific scene is a loading screen (between main menu and a level) when the level finished async loading, a method called MoveObjects is called in a script in the loading screen, to move some objects that where spawned in the loading screen scene into the level scene (this is not the issue though, since I already try without this method and the problem on the UI persist).
Once the logic of this MoveObjects method is done, a start button is enabled in the loading screen, for the player to click and start playing (I did try moving the start button to the level scene, since maybe it not been a child of the currently active scene could be the issue, but the problem still persist). Is at this point that the UI is partially broken, what I mean with this is, that I can see buttons (and some other UI elements like a scrollbar) changing color/state when the mouse moves over them, but I cannot click on them anymore (the button wont even change to the pressed state).
Also note that I tried creating a development build to see if there was any errors in the console, and I notice that this problem is also affecting the old UI system, so I was not able to interact with the development console anymore.
Also also, note that if I grab and drag the scrollbar before this issue appear, and I keep holding down on the scrollbar until this happens, the mouse gets stuck on the scrollbar, meaning that I cannot interact with the UI anymore, but the scrollbar will still move with the mouse.
I already check that this things are not the source of the problem:
Missing EventSystem, GraphicRaycaster or InputModule.
Another UI element blocking the rest of the UI.
Canvas is Screen Space - Overlay so there is no need for a camera reference.
I only have one EventSystem.
Time.timeScale is 1.
I am not sure what else I could try, so if anyone has any suggestions, I would appreciate it. Thanks.
P.S: I am sorry to say that I cannot share any code or visual material or examples due to the confidentiality.
A major source for a non-working UI for me has always been another (invisible) UI object blocking the raycast (a transparent Image, or a large Text object with raycast on).
Here's a snippet I put together based on info found elsewhere, I often use it to track objects that are masking the raycast in complex UI situations. Place the component on a text object, make sure it's at least few lines tall, as the results will be displayed one under another.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
[RequireComponent(typeof(Text))]
public class DebugShowUnderCursor : MonoBehaviour
{
Text text;
EventSystem eventSystem;
List<RaycastResult> list;
void Start()
{
eventSystem = EventSystem.current;
text = GetComponent<Text>();
text.raycastTarget=false;
}
public List<RaycastResult> RaycastMouse(){
PointerEventData pointerData = new PointerEventData (EventSystem.current) { pointerId = -1, };
pointerData.position = Input.mousePosition;
List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(pointerData, results);
return results;
}
void Update()
{
list= RaycastMouse();
string objects="";
foreach ( RaycastResult result in list)
objects+=result.gameObject.name+"\n";
text.text = objects;
}
}
Basically, what I want to do is have two objects (fpscontroller and a wall) that collide like normal. But, once a script runs, they won't collide anymore.
So, how do I make that script that makes the two objects ignore collision with each other? I've tried looking this up, and what I got was Physics.IgnoreCollision, but the documentation on that confused me. And when I tried putting it in and replacing their variables with my variables, I got a bunch of errors.
So, please help.
The way i would go about doing this is through layers.
In unity, a convenient way to ignore physics collision between objects is through setting the object to be ignored to a layer which your player object will always ignore.
So, to go about setting this up imagine the following:
fpscontroller's layer is set to "player" (a layer i have created and named such).
wall's layer is initially always set to "wall" (once again a layer i have created)
Additionally, I create a layer that i call "IgnorePlayerCollisions"
Now i go to: Edit > Project Settings > Physics
Here i can check the box which will turn off collisions between my player and IgnorePlayerCollisions layers.
Now, at runtime i run my script which says:
Change Wall Layer to "IgnorePlayerCollisions". Voila, my player now ignores all collisions with this layer.
Code Examples: (You don't need these all):
// Function to Change a GameObject Layer to the Ignore Layer
public void ChangeGameObjectLayerToIgnore(GameObject go) {
go.layer = [Index of Ignore Layer];
}
// Function to Change "This" GameObjects layer to any other layer
public void ChangeThisGameObjectLayer(int layerIndex) {
this.gameObject.layer = layerIndex;
}
// Change a GameObject layer to another layer
public void ChangeGameObjectLayer(GameObject go, int layerIndex) {
go.layer = layerIndex;
}
How?
Place a C# function into a script and attach it to either your desired gameobject. In your case, i may attach it my Player GameObject since it seems like you may want to disable object throughout the game based on specific trigger or something. It really depends on how you plan to trigger the function.
Let me know if this makes sense or not. If not, i can attempt to make it more legible.
Good luck and have fun!
I just started out with mono and I've already gotten problems. I'm used to play with c# code and was told that mono would be easy for me, but no no. I simply want to start a new activity and close the one i just were using. I checked out some mono API examples, but they are simply too complicated for this task. It has to be some easier way of doing it. This is my first activity class:
[Activity(Label = "CryptotoDroid", MainLauncher = true, Icon = "#drawable/icon")]
public class Activity1 : Activity
{
EditText inputpassword;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button button = FindViewById<Button>(Resource.Id.MyButton);
inputpassword = FindViewById<EditText>(Resource.Id.beforetext);
button.Click += delegate
{
if (inputpassword.Text == "Moo")
{
StartActivity(typeof(ActivityContacts));
}
};
}
}
This is what i tried, but the program crashes. I Simply want to make the program to start a new activity when the password is "moo".
The activity I want to start is:
[Activity(Label = "My Activity")]
public class ActivityContacts : Activity
{
protected override void OnCreate(Bundle bundle)
{
SetContentView(Resource.Layout.Main);
var contactgrid = FindViewById<GridView>(Resource.Id.gridview);
}
}
Later on, i would also like to fill out my gridview with all contacts in the phone, but that belongs to another topic.
Did you mean to set both activities' content views to the Main layout?
SetContentView(Resource.Layout.Main);
public void setContentView (View view)
Set the activity content to an explicit view. This view is placed directly into the activity's view hierarchy. It can itself be a complex view hierarchy. When calling this method, the layout parameters of the specified view are ignored. Both the width and the height of the view are set by default to MATCH_PARENT. To use your own layout parameters, invoke setContentView(android.view.View, android.view.ViewGroup.LayoutParams) instead.
For starting with MonoDroid, I recommend:
getting hold of one of the excellent books out there (Wally, Greg, Chris or others will be along with suggestions - I've personally yet to see a bad one so won't give a recommendation!)
try watching a couple of the Xaminar's on youTube -http://www.yourepeat.com/g/Xaminar
try building the Xamarin sample programs and then try adapting them - it's sometimes easier to adjust working programs than it is to start new ones (sometimes!)
try building new things just as you have above.
When you hit problems - as we all do - then please do ask questions here or on the Xamarin forums - people will help.
However, when you hit a crash or exception, please try:
Give us as much info as you can about the crash/exception - including there are some ways to get extra debug logs off of a phone or emulator - http://docs.xamarin.com/Android/Guides/Deployment,_Testing,_and_Metrics/Android_Debug_Log - these logs often contain important text which will help us help you diagnose the crash
If you are running under the VS2010, VS2012 or MonoDevelop debugging tools, try adding extra Console.WriteLine statements and/or use breakpoints - this can help you and us work out which line is causing the crash - or if the crash is occurring somewhere in setup before the code even runs.
Speaking personally, I believe Mono for Android does help experience C# devs exploit their skills on Android - but there's still new things to learn, and there are still embedded development frustrations to tackle and overcome (like these sort of crashes)