Android sdk 3.0: How to set applicationId when using UiLifecycleHelper.java? - android

I recently migrated from FB for Android 2.0 to 3.0.
Instead of using
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="#string/app_id"/>
in my AndroidManifest.xml, I wanted to programmatically set my application Id to a session.
I know that I can use Session.Builder.setApplicationId() upon the creation of a session object.
However, when I use UiLifecycleHelper.java, it creates a session without using session.Builder. It only uses its constructor (session = new Session(activity)).
It seems like it assumes that I already defined a meta-data for app id in my manifest file.
However, I want UiLifecycleHelper.java to create a session by using Session.Builder and programmatically applying my application Id in its onCreate().
How can I do it? I google it but haven't found any solution yet.
Is it a common practice to use meta data to set FB application Id in the manifest file? I really don't want to reveal my app Id by putting it in an xml file. Please help!

Your Application ID is NOT considered a secret (just like how a person's profile ID is not a secret).
But if you really don't want to use the xml method, you can copy or subclass UiLifecycleHelper and change the appropriate method to use your own way of creating a Session.

If you want to set it in the code, you can change the method getMetadataApplicationId() from Utility.Java and add your code to set the application id to whatever you like.

Related

android: get the application name from inside a module

i'm writing an android module, which i will include in some of my projects. Now i need to get the application name(at runtime), e.g. the name that is set as android:label for the application in the manifest. Obviously i can't use getResources().getString(R.string.app_name) because i can't access the resources of the project that uses this library. Is there another way?
I tried getApplicationInfo().name but that is null and i can't find anything else.
I tried getApplicationInfo().name but that is null
That is because name comes from the android:name attribute on <application>, and you are not using a custom Application subclass in your app.
i can't find anything else.
Call loadLabel() on the ApplicationInfo, passing in a PackageManager as a parameter, to retrieve the android:label value for an <application> or any component (e.g., <activity>).
The module is included in the application so I guess you are calling one of its methods in the application. If it is the case, I propose that you add a parameter in this method to get the context of the application. Then you can use context.getResources().getString(R.string.app_name) in your module to get the application name.

ID error at XML validation Android

I want to know what is the different of declaring a variable or id as
#android:id/
#+id/
When I set #+id/tabhost on Tabhost, it keep giving me error, but if I set #android:id/. it work.
#+id creates a new ID. Android does not know this new ID, so the app crashes.
#id uses an existing ID from inside your app.
#android/id uses an existing ID from the Android framework. Android already knows this ID, it can access the corresponding View and the app will not crash.
Resource ID in Android are specific to a your package.
#+id/name will create a resource ID in your package with the name "name" and give it a unique ID, you can also check that ID in R.java file. In code, you can use like R.id.name.
#android:id/name this will use the ID "name" from the package specified by android (in code you can use like android.R.id.name.)
#android:id/ is use to get id from existing packages.
And tabhost are specified in the android package that's why its giving error #+id/tabhost ,so you have to use #android:id/

Android configuration in meta-data or in properties file?

I'm writing a library that is used by developers and I have a configuration items like apiKey, environment and others. I want to developer to set these values, my first thought was using Java Properties file that I load in the Application class, but I've seen Google Play and Google Map SDK ask the developers to add their apiKey in meta-data tag in Android Manifest. What is the recommended way?
The recommended android way to let the user define values for your library
from the application module is through meta-tags
it is as easy as using this code on the android-manifest
<meta-data android:name="com.yourproject.apikey" android:value="apikey"/>
and you can get these values from your library like this
try {
ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo(activity.getPackageName(), PackageManager.GET_META_DATA);
Bundle bundle = applicationInfo.metaData;
String apiKey = bundle.getString("com.yourproject.apikey");
} catch (NameNotFoundException e) {
//Resolve error for not existing meta-tag, inform the developer about adding his api key
}
The main benefits of this approach are
1) That you can let the user define different values per device type e.g. phone, tablet or sdk-version
(this may not be applicable for your case but it's a good point for sure)
2) You can add complex data as the value in your meta-tag,
and you can use this if you want to add a string-array with all the value that you want to declare.
3) Since meta-tags are basically a bundle it is easier for you to
Read these values on your library code.
Have default values if the user has not declared the required
meta-tags.
(I believe that parsing properties from assets requires more code without any great advantage)
4) You can also add configuration meta-tags for activities,providers and broadcastreceiver on your AndroidManifest.
5) Also I suppose that it is easier to request for the consumers of your library to add a few meta info on his Android Manifest, than adding a properties file on their assets folder.And i believe that this is the main reason Many known libraries use this approach like google-play-service, google-maps, crash reporting libraries and many libraries that provide user analytic.
Good Luck
It´s seems that using meta-data tag is the Android way to achieve this, although I can´t find any official documentation to argue why.
Using meta-data you can define you property value using a resource instead of a static value, this way you can get different values for the same property based on the resources qualifier. This is very useful if your property needs internalization.
Ex:
<meta-data android:value="foo.bar" android:name="#string/hello" />
is easy using this
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="API_KEY"/>

Change the title of Paypal PaymentActivity class

I included paypal sdk in the app and set the product name and price as shown in the attached image.
Is it possible to change the title of the paypal PaymentActivity class . ?
I used the entire code for paypal from this link
.Please suggest whether we could change the title to any text ,(I want to change the text that is marked in Red Circle)?
Jeff here from the PayPal Mobile SDK team.
The PayPal Android SDK doesn't support modifying any of the SDK activities. We don't plan to allow the ability to modify titles, as this would be an inconsistent user experience. However, we're open to feedback from users, so please feel free to file issues in GitHub if you have feature requests!
Couple observations, apparently:
The activity of off which you have put screenshot is PaymentMethodActivity (according to Hierarchy Viewer)
PayPal SDK uses ABS
According to the sample app provided in PayPal SDK page, onBuyPressed() launches PaymentActivity with startActivityForResult():
Now, according to SDK references, PaymentActivity does not provide any API for setting custom title. The page lists setTitle() as a part of "Methods inherited from class android.app.Activity", but does not give any details on how to extend PaymentActivity so that one could leverage those APIs.
So, currently, there is no way to use a custom title.
I personally would not bother to change the title on that screen as it clearly depicts itself as a PayPal screen (as one would see their homepage on a browser), but if you really want a custom title, you may need to file a new issue on their GitHub page.
Your AndroidManifest.xml file should have an entry for the PaymentActivity that looks like this:
<activity android:name="com.paypal.android.sdk.payments.PaymentActivity" />
All you should have to do is add the label attribute to that to change the title:
<activity android:name="com.paypal.android.sdk.payments.PaymentActivity" android:label="#string/some_title" />
For other potentially useful attributes, refer to the activity tag documentation.
Note: I'm assuming here that the PayPal SDK will allow you to override the title. That is, I would expect it to only set a default title if none is given explicitly.
Edit: Unfortunately, the assumption above has proven to be incorrect. Diving a little deeper, it turns out every activity in the PayPal SDK always sets its title in the onCreate() method. That's actually a little surprising, because that means that PayPal isn't leveraging Android's built-in support for localisation and resources. Although I suppose it does allow them to offer developers the SDK as a jar, which doesn't support bundling Android resources.
In any case, since the various activities haven't been declared final, you could simply try to extend them and set the title after the super has done its work:
public class TitlePaymentMethodActivity extends PaymentMethodActivity {
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Custom Title")
// or:
setTitle(R.string.custom_title);
}
}
After that, you'll have to change the manifest to point to your TitlePaymentMethodActivity, in stead of directly to the activity in the SDK. Do note that this approach may break at any point, if PayPal decides to finalize the activity classes. With that in mind, I suggest you follow #shoe rat's recommendation and file a request with PayPal for 'official' support.

what does the "#+android:id/title" mean?

In normal, we should use #+id/ to define an id and use #id to reference an id. Today I found #+android:id/title in apps/settings/res/layout/preferenc_progress.xml.
How to understand it and how to use it?
It is used for resources that are shipped with the SDK.
You can take a look at them by browsing to
[PATH TO ANDROID SDK]/platforms/android-[VERSION]/data/res
By using the android in android.R.whatever you just specify the R file to look up. For more information you should read Accessing Platform Resources.
That belongs to the app preferences activity screen definition.
title and summary are standard Android fields of a TextView preference item.
I think it does the same thing. It's just a more formal way of saying it by specifying where the namespace is.
I've never met this way of giving id, but in theory this means adding new id title to android package. So you'll be able to use it in your code like android.R.id.title. But I'm not sure resource compiler will really create any id in android package. I think it can be used only with predefined ids. But I'll give you more precise answer later, when I'll be able to check it.
EDIT: I've checked it and found some differences. Firstly, if you define Android's id using #+android:id/some_id, which is already present in SDK, this id will not be defined in your R.java file. If it's not present in SDK, it will be defined in R.java, but with different kind of value. Secondly, if you'll try to convert id from its string representation to int value, Resources.getIdentifier() method will return 0 in case of #+android:id format.
This means it will create an id in your resource file.

Categories

Resources