I'm trying to extend the DialogPreference so I can have a NumberPicker for one of my
preferences. I've been reading:
http://developer.android.com/guide/topics/ui/settings.html#Custom
But I'm hitting an 'Error inflating NumberPickerReference' message. I think I know
why, but I'm not sure how to fix it.
My preferences.xml looks like:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
android:key="pref_alertsEnabled"
android:title="#string/alert_enable"
android:summary="#string/alert_enable_desc"
android:defaultValue="true" />
<NumberPickerPreference
android:key="perf_Timer"
android:title="#string/timer"
android:negativeButtonText="#android:string/cancel"
android:dialogTitle="#string/timer"
android:summary="#string/timer_desc"
android:dialogMessage="#string/timer_desc"
android:positiveButtonText="#android:string/ok"/>
</PreferenceScreen>
And in my projects src folder I have NumberPickerReference.java. I think the thing I'm missing is how to tell Android where to find NumberPickerReference class.
When preferences.xml is read in, I'm guessing it goes looking for the NumberPickerReference class, but doesn't find it.
Do I need to specify where to find the NumberPickerReference.class ? If yes, how do I do this ?
BTW, the app runs fine if I remove the <NumberPickerReferecnes> item from preferences.xml
Just add the path to that preference:
com.your.package_name.and_path.NumberPickerPreference
E.g.
<com.example.myapp.preferences.NumberPickerPreference
android:key="perf_Timer"
android:title="#string/timer"
android:negativeButtonText="#android:string/cancel"
android:dialogTitle="#string/timer"
android:summary="#string/timer_desc"
android:dialogMessage="#string/timer_desc"
android:positiveButtonText="#android:string/ok"/>
Related
I am trying to set up the default value for the list preferences in my app, but I am having trouble with the default value, as when I try to run the app the default values that I chose in both list preferences are not being selected in runtime. This is my xml code:
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory app:title="#string/display_header">
<ListPreference
app:key="language"
android:title="#string/language_title"
app:summary="#string/en"
android:entries="#array/lang_entries"
android:entryValues="#array/lang_values"
android:defaultValue="#string/en"
/>
<ListPreference
app:key="display"
app:title="#string/display_mode_title"
app:entries="#array/display_entries"
app:entryValues="#array/display_values"
app:summary="#string/automatic"
android:defaultValue="#string/automatic"/>
</PreferenceCategory>
</PreferenceScreen>
I tried uninstalling and reinstalling the app but it did not work. I also checked that the string names that I used in the default value are correct, and I also made sure that the default value matches the one in the string array of entries. Please help me fix this problem
I think defaultValue is a number/index of the choice.
See this post.
I just found out that I had to replace the android:defaultValue line by app:defaultValue and I had to pass the corresponding value to the entry that would be the default value in runtime.
I am following the code lab tutorial.
My Gradle file looks like this
dependencies {
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:mediarouter-v7:25.0.0'}
This is my CastOptionsProvider class
public class CastOptionsProvider implements OptionsProvider {
#Override
public CastOptions getCastOptions(Context context) {
return new CastOptions.Builder()
.setReceiverApplicationId(context.getString(R.string.chromecast_app_id))
.build();
}
#Override
public List<SessionProvider> getAdditionalSessionProviders(Context context) {
return null;
}}
This is the menu xml file
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<item
android:id="#+id/media_route_menu_item"
android:title="#string/media_route_menu_title"
app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
app:showAsAction="always" /></menu>
And this is my OnCreateOptionsMenu method in MainActivity
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.navigation_drawer, menu);
mediaRouteMenuItem = CastButtonFactory.setUpMediaRouteButton(getApplicationContext(), menu, R.id.media_route_menu_item);
return true;
}
And this in the manifest file
<meta-data
android:name="com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME"
android:value="com.mypackage.CastOptionsProvider" />
I have followed the codelab tutorial to its exact form, copy and pasted everything while changing those variable which needs to be changed.
My application and the chromecast device are connected to the same network. The Chromecast button appears in the Youtube app but not on my app. What am I missing ?
Thanks
It might be an invalid app_id.
You might try replacing your chromecast_app_id string resource with the app_id string resource value from the sample project.
(Right now, it's 4F8B3483. See: https://github.com/googlecast/CastVideos-android/blob/master/res/values/strings.xml).
Believe it or not, switching to that value made the cast icon visible in my app.
If everything is done properly, you just need to restart cast device (it does not work with your app_id without restarting after registration).
Check in Google Cast SDK Developer Console if your Application ID is correct and status is Published.
When publish your Application ID, restart your Chromecast device.
For testing purposes, you can use CC1AD845 or 4F8B3483 as Application ID. These are from Google sample apps.
Adding another answer as the accepted answer did nothing for me. The issue happened to me only when building a proguarded version of my app.
I had to add a couple lines to my proguard file (essentially all of the classes referenced by xml as mentioned here: https://stackoverflow.com/a/24578823/1048847)
-keep class com.your.package.CastOptionsProvider { *; }
// I have a custom MediaRouteActionProvider but this may need to be
// android.support.v7.app.MediaRouteActionProvider in the case of OP
-keep class com.your.package.MediaRouteActionProvider { *; }
you are missing attribute icon here
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<item
android:id="#+id/media_route_menu_item"
android:title="#string/media_route_menu_title"
android:icon="#drawable/chromebutton" app:actionProviderClass="android.support.v7.app.MediaRouteActionProvider"
app:showAsAction="always" /></menu>
OK I've read most of available answer on similar questions but nothing fixed my problem
gradle classpath 'com.android.tools.build:gradle:2.1.2'
android {
defaultConfig {
minSdkVersion 16
targetSdkVersion 24
vectorDrawables.useSupportLibrary = true
}
}
dependencies {
compile 'com.android.support:recyclerview-v7:24.1.1'
compile 'com.android.support:appcompat-v7:24.1.1'
compile 'com.android.support:cardview-v7:24.1.1'
compile 'com.android.support:support-v4:24.1.1'
compile 'com.android.support:design:24.1.1'
}
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/ScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ViewItem"
tools:ignore="MissingPrefix">
...
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="center_vertical"
android:layout_marginBottom="10dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:foregroundGravity="center_vertical|center|center_horizontal"
app:srcCompat="#drawable/ic_car" />
...
</ScrollView>
vector ic_car.xml
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24"
android:viewportWidth="24">
<path
android:fillColor="#5c5c5c"
android:pathData="M18.92 6.01C18.72 5.42 18.16 5 17.5 5h-11c-.66 0-1.21 .42 -1.42 1.01L3 12v8c0
.55 .45 1 1 1h1c.55 0 1-.45 1-1v-1h12v1c0 .55 .45 1 1 1h1c.55 0 1-.45
1-1v-8l-2.08-5.99zM6.5 16c-.83 0-1.5-.67-1.5-1.5S5.67 13 6.5 13s1.5 .67 1.5
1.5S7.33 16 6.5 16zm11 0c-.83 0-1.5-.67-1.5-1.5s.67-1.5 1.5-1.5 1.5 .67 1.5
1.5-.67 1.5-1.5 1.5zM5 11l1.5-4.5h11L19 11H5z" />
<path android:pathData="M0 0h24v24H0z" />
</vector>
my activity class extends AppCompatActivity and all android:src tags are changed to app:srcCompat but still I see Invalid drawable tag vector error on the minSdkVersion which is set to 16.
In my case, the crash happened due to using Resources.getDrawable(int) to get the vector drawable on pre-lollipop devices. I was trying to create a drawable to pass into
ImageView.setImageDrawable(Drawable).
Google Blog Post for Android Support Library 23.2:
As of Android Support Library 23.3.0, support vector drawables can
only be loaded via app:srcCompat or setImageResource().
Solution: Use VectorDrawableCompat.create(Resources, int, Theme) to create the drawable.
You can also use ImageView.setImageResource(int) however this will read/decode the bitmap on the UI thread.
Truth is, looking at your code, it should work in any version of the library, as you are using only the srcCompat attribute.
One issue I suspect, is that your vector is in a drawable-21 folder, while in the regular Drawable folder you have single file that references these Drawables. This was a method done in early versions of the library, but does not work anymore due to changes below. Just move the actual vector file to the Drawable folder.
We have had sometimes similar issues that were solved by changing the ImageView to an AppCompatImageView. Even though the library is supposed to do this automatically, it seems that sometimes it doesn't.
It is still possible that the code is breaking if your vector is part of a compound image, for example a layered image. Or it's breaking somewhere else for which you have not posted the code, for example if you are setting a vector as the leftImage on an ImageButton. These functionalities have been removed in 23.3, as #Zeke has answered. Nonetheless, it was returned in 23.4, made usable by adding the following to your activity
static {
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
}
They still warn you it can cause memory leaks. See https://medium.com/#chrisbanes/appcompat-v23-2-age-of-the-vectors-91cbafa87c88#.s48hqaw1u, though for minimal use it shouldn't be an issue.
A simple workaround would be to create the Drawable and set it in code. Something like this;
Drawable icon = AppCompatDrawableManager.get().getDrawable(context, resVectorID);
// Set it for example as a left image of button
button.setCompoundDrawablesWithIntrinsicBounds( icon, null, null, null );
I have not tested this, and have no ideas if this causes memory issues or not. Would love it if someone can follow up.
Problem got solved itself after updating to Android studio 2.2
I did like this:
public static final Drawable getMyDrawable(Context context, int id) {
final int version = Build.VERSION.SDK_INT;
if (version >= 21) {
return ContextCompat.getDrawable(context, id);
} else {
return VectorDrawableCompat.create(context.getResources(), id, context.getTheme());
}
}
I have never really had to ask a question here. Almost any question I can think of has already been asked and answered. Thus the reason I have never registered.
However, I have finally run across something that I cannot find an answer for. I assume it would be fairly simple.
I am trying to implement in app billing for an android project I am working on. I typically code in C# and use Unity to build the .APK.
However, this time I have to make modifications in Eclipse to some PreferenceScreens.
I have an xml file with some string-arrays set up for a ListPreference under my PreferenceScreen. These ListPreference values are then passed to UnityPlayer for a method. That's all set up.
I am trying to figure out a way to set up in app billing to trigger on selection of one of my list preference, or preference items.
I can probably figure a lot of it out on my own, but I need help with a function to do something pulling from a string value in the list preferences. Does this make sense?
I don't expect to be spoon fed, so if I can just get help on a few lines for this I would appreciate it. Of course I won't complain if anyone wants to write up a little 20 liner function to do it for me.
Thanks in advance,
John
** edit **
Here is the xml from the preferenceScreen I need to address with android In App Billing from Java
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="wallpaper_settings"
android:title="#string/wallpaper_settings" >
<!-- Ad Placeholder -->
<CheckBoxPreference
android:defaultValue="true"
android:key="rotate"
android:summary="#string/rotateSummary"
android:title="#string/rotateTitle" />
<CheckBoxPreference
android:defaultValue="true"
android:key="doubleTap"
android:summary="#string/doubleTapSummary"
android:title="#string/doubleTapTitle" />
<CheckBoxPreference
android:defaultValue="false"
android:key="swipeEmul"
android:summary="#string/simulateSwipeSummary"
android:title="#string/simulateSwipeTitle" />
<ListPreference
android:entryValues="#array/cameraValues"
android:defaultValue="MainCamera"
android:entries="#array/whichCam"
android:summary="#string/cameraSummary"
android:dialogTitle="#string/cameraTitles"
android:title="#string/cameraTitles"
android:key="whichCam"/>
</PreferenceScreen>'
This question is very generic, on an Android application you might do this by creating an Activity, then starting that activity from your preference "onClick" method and then, once the activity starts, you load all your in app billing stuff (showing a progress dialog) and finally present the "buy" dialog. Is this what you need?
I am following developer.android to do their training. I am in the process of "Adding the Action Bar", however, i encountered a problem.
In the section "Respond to Action Buttons" here (http://developer.android.com/training/basics/actionbar/adding-buttons.html#Respond), the line "case R.id.action_search:" has error which says "action_search cannot be resolved or is not a field".
[Delete] In fact, the line "openSearch();" and "openSettings();" have red lines as well, saying "The method openSettings() is undefined for the type MainActivity". [/Delete]
(I know what is wrong here, I need to declare those two methods by myself.)
what should I do? Thx for the help first.
<item android:id="#+id/action_search"
android:icon="#drawable/ic_action_search"
android:title="#string/action_search"
android:showAsAction="ifRoom" />
Firstly, the attribute android:id="#+id/action_search has no problem.
There are two things you need to do that the tutorial seems to have taken for granted
Add an image called "ic_action_search.png" to all the res/drawable directories in your project.
Add the definition for the string resource "action_search" which is being assigned to the android:title attribute. Open the res/values/strings.xml file and add
<string name="action_search">Search</string> over there.
Secondly,
The functions openSearch() and openSettings() need to be defined in the same class by you. Something like :
public void openSearch()
{
//Do something here.
}
public void openSettings()
{
//Do something here.
}
their functionality is not important for the tutorial as they are trying to show how you can control what happens when an option on the action menu is selected.
Go Project->Clean
It worked for me
I tried it and had the same problem. I removed android:icon="#drawable/ic_action_search" (instead of adding the images because I'm lazy) and added the action_search string in the resource file to make sure the menu file wasn't referring to anything that didn't exist. After that, I tried changing android:id to android:title because I saw that it had worked for you, and this was curious. It caused an error after saving, and so I changed it back. After changing it back and saving, the original error in the MainActivity.java file disappeared, and R.id.action_search was resolved. I think the problem is just that R.java is not being updated, and you have to basically hit it with a hammer by forcing an error and fixing it, at least in my case. I don't know why using android:title worked for you. Hope this helps somehow.