Cannot replace android library project resources with android application resources - java

In my android library project, I have:
/res/drawable/redButton.png
And I reference this image file in my library like this:
public class MyLibExample{
.....
RadioButton r = new RadioButton(context);
r.setButtonDrawable(com.mylib.R.drawable.redButton);
}
Now, in my android application project, I have the same res:
/res/drawable/redButton.png
However, this redButton.png is a different graphics file than the one defined in the library.
I do not reference redButton.png in my application. Instead, I just create an object of type MyLibExample which was defined in the library.
I expected to see the redButton.png from the application, not from the library. However, the redButton.png from the library is always used.
How do I force the MyLibExample to use the redButton.png from the application?

The problem is that the id com.mylib.R.drawable.redButton is the id defned in mylib. You need the id defined in my app. So in your app code:
RadioButton r = (RadioButton)findViewById(com.mylib.R.id.radiobuttonId);
r.setButtonDrawable(com.myapp.R.drawable.redButton);

Related

Errors when trying to create Xamarin Android binding Java Library for mapsforge/vtm

I have tried to use the Java map library mapsforge-vtm to show an offline map using a mapsforge file with Xamarin Android.
Within Visual Studio 2019, I created one aar binding project as "LibraryProjectZip" for the file "vtm-android-0.13.0.aar" and one jar binding project as "EmbeddedJar" for the file "vtm-android-0.13.0.jar".
There is also a dependency to "vtm-0.13.0.jar" which you can see in the file "vtm-android-0.13.0.pom".
That "vtm-0.13.0.jar" file seems to cause the problems (e.g. the java interface Gesture which I include further down below).
If you keep following the dependencies (by looking in the pom files), there are also dependencies to "slf4j-api-1.7.28.jar" and "androidsvg-1.4.jar" but those two jars do not seem to cause problems, as far as I can tell.
Regarding the problem file "vtm-0.13.0.jar" I have tried to use it as an "EmbeddedReferenceJar" within both of the above mentioned projects (i.e. the aar binding and the jar binding projects for vtm-android).
Both of those two binding projects can build without errors (when also using "androidsvg-1.4.jar" and "slf4j-api-1.7.28.jar" as "EmbeddedReferenceJar"'s)
However, when trying to reference those projects from a Xamarin Android app, it does not work.
When trying to do that, I can not reference both the aar binding and the jar binding projects because then there is an error message about duplicated versions of for example the class "Org.Oscim.Android.MapView".
But when I use only one of them (i.e. either aar or jar project), then only the types from the main libraries are available (i.e. the "LibraryProjectZip" file for "vtm-android-0.13.0.aar" and "EmbeddedJar" file for "vtm-android-0.13.0.jar").
But I seem to NOT be able to reference any types from the "EmbeddedReferenceJar".
For example, there is a java type "org.oscim.tiling.source.mapfile.MapFileTileSource" in the "vtm-0.13.0.jar" but it does not seem to become available i.e. I can NOT use:
using Org.Oscim.Tiling.Source.Mapfile.MapFileTileSource;
In fact, the only namespace I can use, starting with "Org.Oscim" is "Org.Oscim.Android" e.g. this works:
using Org.Oscim.Android;
Then I tried to create a separate "EmbeddedJar" from the file "vtm-0.13.0.jar", since I was hoping that it then could be referenced from Visual Studio.
(and in that project I added "slf4j-api-1.7.28.jar" as "EmbeddedReferenceJar")
However, the compilation failed with 85 errors.
And I have tried all four combinations of settings for "Android Class Parser" and "Android Codegen Target"
(i.e. the four combinations of "class-parse + XAJavaInterop1" and "jar2xml + XAJavaInterop1" and "class-parse + XamarinAndroid" and "jar2xml + XamarinAndroid")
I have also tried with a couple of different target frameworks (Android 4.4 and Android 9 and Android 10).
50 of those 85 errors are this same error:
Error CS0234 The type or namespace name 'IGesture' does not exist in the namespace 'Org.Oscim.Event' (are you missing an assembly reference?)
vtm-jar ...\obj\Debug\generated\src\Org.Oscim.Event.IGesture.cs
Indeed, when looking in that file "IGesture.cs" there is no interface defined there.
I had expected to find something like "public partial interface IGesture ..." in that file, in a similar way as there is a "public partial interface IGestureListener ..." in the file "IGestureListener.cs" in the same directory.
I suspect that it has something to do with the fact that the java interface "Gesture" seems to be a bit unusal and thus not being nicely supported by Xamarin/Visual Studio.
This is what I am talking about:
Here is a "normal" kind of Java interface (GestureListener) which seems to work without errors in Visual Studio:
https://github.com/mapsforge/vtm/blob/master/vtm/src/org/oscim/event/GestureListener.java
package org.oscim.event;
public interface GestureListener {
boolean onGesture(Gesture g, MotionEvent e);
}
Here is the source code for the problematic interface (Gesture) in "vtm-0.13.0.jar":
https://github.com/mapsforge/vtm/blob/master/vtm/src/org/oscim/event/Gesture.java
package org.oscim.event;
public interface Gesture {
final class Press implements Gesture {
}
final class LongPress implements Gesture {
}
final class Tap implements Gesture {
}
final class DoubleTap implements Gesture {
}
final class TripleTap implements Gesture {
}
final class TwoFingerTap implements Gesture {
}
Gesture PRESS = new Press();
Gesture LONG_PRESS = new LongPress();
Gesture TAP = new Tap();
Gesture DOUBLE_TAP = new DoubleTap();
Gesture TRIPLE_TAP = new TripleTap();
Gesture TWO_FINGER_TAP = new TwoFingerTap();
}
As far as I can remember, I do not think I have ever seen that kind of Java code with classes being defined and instantiated within an interface.
And I suppose that Xamarin/Visual Studio also have a hard time to understand that kind of Java code.
So, is there anything you can do about this (and all other build errors, totally 85, with this vtm library)?
Has anyone been able to implement an Android App with Xamarin Android that uses mapsforge-vtm bindings, including also having been able to put all the pieces together and succeeded to display a map from a mapsforge map file?

My device will flash back to the background after the multilingual handover jump

BaseActivity.java
Language switching
Intent
OnClickListener
Error demo
No error message was prompted.
You are using old style of language switching which were deprecated and removed after releasing android 8, It completely get removed. However, if you want to use your application for android prior to 26, you have to change your code on language switching Image as follow:
Configuration config = new Configuration(resources.getConfiguration());
New instance of Configuration have to be passed to resources.updateConfiguration();. above line will solve your problem.
On the other hand if you want to publish your application for higher api level, follow link below:
#Override
protected void attachBaseContext(Context newBase){
Configuration configuration = new Configuration(newBase.getResoureces().getConfiguration());
configuration.setLocale(/* selected locale which you have to get from user or your app configuration*/);
super.attachBaseContext(newBase.createConfigurationContext(configuration));
}
Override above method for each activity which you have declared;

Android Studio MainActivity.java can't find resources

I'm basically writing my first hello world in android studio and the java file says that the xml layout file and other resources in the res file don't exist. I took the references to these things from books/tutorials so they should work.
The book said to use
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main.xml);
}
But when that didn't work I changed it to res.layout.activity_main.xml
my project directory looks like this
How do I make it work?
http://i.stack.imgur.com/N71sl.png
//EDIT
http://i.stack.imgur.com/HUgsm.png
You are referencing res not R.
do something like this: setContentView( R.layout.activity_main);
Leave off the ".xml"
update your android studio in stable channel and restart android studio and build the project again .
You need to understand the concept of the R class in android.
The R class is auto generated every time you build your project and cannot be modified.
This class contains a map of all the projects assets. Every time you created or import a resource, set a dimension, a string, create a layout, add or change id etc. the R file is changed in the background.
So if you want to access a resource you simply call it using the R class.
For example:
layout: R.layout.activity_main
string: R.string.hello_world
id: R.id.et_main_helloWorld
And so on.
More info can be found here.
Make sure you also check Providing Resources and Accessing Resources for a better understanding.
Good luck and happy coding.
In the end I started a new project and added in everything piece by piece, I had put a mp3 file in the values directory which was messing up the R file.

"android.R.id.content" generates an error with no change to the source code

This code is from an exercise source code from a course I took. It worked fine up until this afternoon. No change was made to it and I re imported it and a similar project and experienced the same new error.
public class TourListAdapter extends ArrayAdapter<Tour> {
Context context;
List<Tour> tours;
public TourListAdapter(Context context, List<Tour> tours) {
super(context, android.R.id.content, tours);
this.context = context;
this.tours = tours;
}
I'm using Android Studio 1.3
android.R.id.content used to work and now generates the error:
Expected resource of type layout.
I've searched stackoverflow and there appears to be a new feature that annotates possible mis-matches. Somehow this android id integer used to be ok and is not any longer. I did not download anything new today to Android Studio.
I have re-built, cleaned and sycn'd. I have exited and rebooted the computer with no resolution.
It's not an error, it's simply a warning.
As you've noted in your question, resource method parameters can now be annotated to indicate what type of resource should be passed to it. Android Studio checks these parameters based upon the name and flags it if it doesn't match the correct type.
However, you've stumbled upon a corner-case that Android Studio does not deal with. As noted by this question, android.R.id.content is a special resource ID used to obtain a root view without knowing the actual ID of the view. Sadly, the Android SDK developers put it as an id resource and not as a layout resource (it should probably have been designated android.R.layout.content, but it's too late to change it now!).
So, to sum up, Android Studio is expecting you to use a R.layout.value here, but you are using a special R.id.value. But you are correct and your App should still build correctly (mine does when I tested it) - hence it is a warning, not an error.

Get current remote or game controller

How can I get the current input device in my application in java? I want to know is the remote or the game controller, that is being used.
It is an android application that I want to run on Amazon FireTV. Unlike the Amazon Kindle there is no touchscreen but you can use a remote or a game controller. I would like to know if it is possible to detect what kind of input device the user is currently using.
The code I have until now is a standard Cordova Application code, but when I know how to detect the current input device I would make a plugin to pass the value to the javascript code. That is not the problem.
As mentioned in the comments you should provide steps you have already taken or code you have already written to address this functionality as that will help us tweak the most appropriate answer.
As a general rule, you can look at the official docs to identify controllers on Fire TV.
https://developer.amazon.com/public/solutions/devices/fire-tv/docs/identifying-controllers
Basically, you need to write the identification code in your Cordova plugin as follows:
int hasFlags = InputDevice.SOURCE_GAMEPAD | InputDevice.SOURCE_JOYSTICK;
boolean isGamepad = inputDevice.getSources() & hasFlags == hasFlags;
This will allow you to find out if it's a gamepad. For a Fire TV remote the code you need is:
int hasFlags = InputDevice.SOURCE_DPAD;
bool isRemote = (inputDevice.getSources() & hasFlags == hasFlags)
&& inputDevice.getKeyboardType() == InputDevice.KEYBOARD_TYPE_NON_ALPHABETIC;
The InputDevice class is available on the Android developer site:
http://developer.android.com/reference/android/view/InputDevice.html
So you basically need to import that in your plugin class to ensure the above code works fine.
import android.view.InputDevice;

Categories

Resources