Adding cast button to ActionBar using the CastCompanionLibrary - android

I am trying to add the cast icon to the ActionBar using the CastCompanionLibrary's helper method:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.main, menu);
mDataCastManager.addMediaRouterButton(menu, R.id.media_route_menu_item); // This one
return true;
}
I have this as my menu.xml as specified by the PDF that is included with the companion library:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<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>
However, nothing shows up in the ActionBar. No errors are thrown, nothing at all is visible. If I add a different menu item just to see if everything with my menu is set up correctly, that item shows up fine - it is just this cast action menu item that isn't showing up.
I have tried changing the "app" prefixes to "android", but then I get a NullPointerException somewhere deep in the library, and I have tried giving the menu item a different, visible icon. Nothing helps.
In AndroidStudio, the menu preview shows a menu item with the title "Play on...", so it seems like this should work.
What am I doing wrong?

You have to register your chrome-cast as a testing devise for you to be able to detect the chromecast devise from the Android.
Check the full SDK guide
Check the developer console registration. You have to register you chromecast devise in the console here, or else it is not detectable
Update: If nothing works, you may try to publish your app in the chromecast dev console as a last resort.
As mentioned by one of the chromecast developer try to access http://<chromecast-ip>:9222 from browser and see if you are able to see any thing.

Sometimes, these types of errors happen because proguard changes the name of the object and/or functions.
One possible solution is to add these to your progaurd configuration files:
-dontwarn android.support.v7.**
-keep class android.support.v7.internal.** { *; }
-keep interface android.support.v7.internal.** { *; }
-keep class android.support.v7.** { *; }
-keep interface android.support.v7.** { *; }
I actually had this exact error on the exact line and I didn't have my proguard configured properly for the support library.

In my case after device registration I forgot reboot chromecast device

CastCompanionLibrary and MediaRouteActionProvider are written for AppCompat ActionBar and not Sherlock ActionBar. It is strongly recommended to move your project to AppCompat since Sherlock ActionBar is deprecated so moving to AppCompat is generally a good move for your project; making that move is not difficult (see, e.g. this article)

Related

After migrating to AndroidX: Cannot instantiate class: androidx.appcompat.widget.ShareActionProvider

I migrated to AndroidX (using the wizard in Android Studio), and I'm having problems with the share action provider. The wizard changed (among many other things) app:actionProviderClass="android.support.v7.widget.ShareActionProvider" to app:actionProviderClass="androidx.appcompat.widget.ShareActionProvider", in my detailactivity.xml file.
The app compiles fine, and runs fine, too -- so long as I install it on my device over USB. However, if I compile a signed APK, and install that, I get the following runtime error (when starting the detailfragment):
W/SupportMenuInflater: Cannot instantiate class: androidx.appcompat.widget.ShareActionProvider
I didn't notice this problem while developing, since I run/test the app on my device through USB. However, when I'm now testing the (signed/minified) APK, the SHARE button does not work. How can I troubleshoot and fix this? For instance, why does it fail on the signed/minified APK fail, while it works fine when installing on same device through USB?
It's hard to tell specifically where (in the code) the warning occurs, since the code in the APK is minified. Perhaps I could create an APK where the code is not minified, so I'd get proper references to lines in the source code (in Android Studio LogCat)?
For reference, here is an excerpt from the class where the warning occurs. I'm assuming the warning occurs somewhere here, as this is what's referencing the shareActionProvider?
import androidx.appcompat.widget.ShareActionProvider;
public class ScreenSlidePageFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
private ShareActionProvider mShareActionProvider;
public ScreenSlidePageFragment() {
setHasOptionsMenu(true); // only the share button
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.detailfragment, menu);
MenuItem menuItem = menu.findItem(R.id.action_share);
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem);
if (mShareActionProvider != null && mImageData != null) {
mShareActionProvider.setShareIntent(createShareImageIntent());
}
}
}
I got some help on #android-dev (IRC), and it seems the problem was that the minifier (for some reason) removes the androidx.appcompat.widget.ShareActionProvider class. Turning off minification produced a working APK.
The fix was to update my proguard-rules.pro file with a new "keep" line, to prevent it from removing the class. From before, I had a similar rule for the old Android Support Library, so I added the second line below and now it works.
-keep class android.support.v7.widget.** { *; }
-keep class androidx.appcompat.widget.** { *; }
thanks #melatonina!

Convert an old style Android menu to work on newer phones

My old app has one simple menu on the main activity. It has only a few simple options, for instance "About" causing a popup with some info about the app.
It works perfectly on emulator Nexus One (API23), because there is an emulated physical menu button.
However, on most modern phones, there is no button, which means that my menus cannot be accessed.
I actually vaguely remember running it on a phone years ago which didn't have a menu button, yet somehow one could still access the menus. I may remember wrong.
(I started digging into this some days ago, and started modifying my code, the main activity inheriting from something more posh than Activity, which then caused some older API versions to be left out - and things quickly spun out of control. After hours of "maven gradle settings" and "Support Library" stuff and many pages of "AAPT2 errors" and messing up my whole system trying to fix that, I had to throw everything away and get a fresh clone from the repo. Fortunately I could also repair the other changes I had made to the system.)
How does one convert an old-style app menu to work on modern phones? It doesn't have to be fancy.
/** Setup menu */
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
/** Handle menu clicks */
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.action_about:
final SpannableString s =
new SpannableString(getApplicationContext().getText(R.string.about));
Linkify.addLinks(s, Linkify.ALL);
AlertDialog d = new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_info)
.setTitle("About")
.setMessage(s)
//.setView(message)
.show();
((TextView)d.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
return true;
default:
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_info)
.setTitle("Currently not used.")
.show();
return super.onOptionsItemSelected(item);
}
}
I'll admit that I no longer understand all the details above from years ago.. it worked, so I never paid it much attention. It looks a bit wordy... probably there are simpler ways to do it.
This is menu/main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"/>
<item
android:id="#+id/action_about"
android:orderInCategory="3"
android:title="About"/>
<item
android:id="#+id/action_manual"
android:orderInCategory="4"
android:title="Manual"/>
</menu>
Maybe there is some "theme" to just add somewhere that makes the menu button show up somewhere on the screen, and that's that? (I know I am optimistic. :))
Everything looks fine.
I think your problem is because you are extending Activity.
change Activity to AppComatActivity.
and change your appThem to android:theme="#style/Theme.AppCompat.Light.DarkActionBar"
Note:
To use the AppCompatActivity, make sure you have the Google Support Library downloaded (you can check this in your Tools -> Android -> SDK manager). Then just include the gradle dependency in your app's gradle.build file:
compile 'com.android.support:appcompat-v7:27.0.2'
SOLUTION:
The only way to a solution that I could find was to create a completely new project with default settings in the latest Android Studio. This gives a "latest fashion" setup. Then I moved code in from the old project manually.
Everything now works perfectly!
ISSUES / REASONS:
As mentioned in the comment section above, every attempt I made to modernize the code resulted in a maze of problems. It was an old project, from way back when Android Studio was not even in Beta stage. Hence, it was based on Eclipse. The current Android version back then was Jelly Bean (Kitkat was just released).
In summary, we had an ancient project based on an older IDE. Perhaps it would be doable to convert a modern Eclipse project into Android Studio. Perhaps it would be doable to convert an older AS project into a modern one. However, performing both these major jumps at the same time was too great a challenge for me.
Another issue which has nothing to do with the old code, but which confused the matter greatly is that something called AAPT2 currently for whatever reason assumes american characters only in the search path to the .gradle directory. I use the word "assumes", because if the characters are anything else, you get pages of errors in the build log. None of the errors point very clearly to the reason.
AFAIK I don't even use AAPT2! After some sleepless nights, I solved it by changing the global setting in Android Studio to simply use another path.

Hide FloatingMenu caused by Theme.AppCompat.Light.NoActionBar

Slowly, I believe I am simply to dump to get this to work and hope
that somebody of you, can help me.
I am using the Android Design Support Library, AppCompat and support libarties:
compile "com.android.support:design:23.0.1"
compile 'com.android.support:support-v4:23.0.1'
compile 'com.android.support:appcompat-v7:23.0.1'
All I wanted was a simple Toolbar, Tablayout and a ViewPager getting to work.
Like you can see in every other app, too.
But I am getting an annoying FloatingMenu on every activity that uses the
app_theme.
How do I remove this FloatingMenu?
Cheers!
Douplicates:
Remove Floating menu button from HTC one [noanswer]
Lot of people are facing this problem and its just not related to your code but to what kind of updates vendors have pushed.Try to confirm it by running your app on other devices too with different lollypop version.
Create a mainmenu.xml file with just one item (R.id.action_settings)
and try to inflate it within your code and set the visibility to false afterwards.
That will might solve the issue otherwise go into phone settings and button and then try to find out if there is any option to help you. It should be related to navigation or something i suppose.
Edit
Setting your sdk version 14 or above seems to solve the issue. We went through many hit and trial and this one seems to work. I believe its a sort of bug in the design library.
It's seems like HTC issue try to override
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuItem settingsItem = menu.findItem(R.id.action_settings);
settingsItem.setVisible(false);
return true;
}

chromecast button not showing up after proguard configuration

After configuring progaurd, chromecast button doesn't show up and instead text like "PLAY ON.." appears in the Action bar.
I have tried adding below line in config file but no luck
-keep class com.google.sample.castcompanionlibrary.** {*;}
please let me know if i am missing anything.
Thanks.

Popup menu using ABS

Is it possible to have POPUP menu like play store attached to each row of adapter
what i done so far
holder.rl_overflow.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(context, holder.rl_overflow);
popup.getMenuInflater().inflate(R.menu.overflow, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(context,"You Clicked : " + item.getTitle(),Toast.LENGTH_SHORT).show();
return false;
}
});
popup.show();
}
});
but constructor for POPUP menu says its available from API 11. I go through the developers.android.com and i found, its can be added using SUPPORT V7 library "https://developer.android.com/reference/android/support/v7/widget/PopupMenu.html" but i'm not able to implement this using ABS, please help someone.
Use like this PopupMenu in ActionBarSherlock.
Styling of the PopupMenu -
<item name="popupMenuStyle">#style/PopupMenu.MyAppTheme</item>
<style name="PopupMenu.MyAppTheme" parent="#style/Widget.Sherlock.ListPopupWindow">
<item name="android:popupBackground">#android:color/white</item>
</style>
If you are using the support library you should discard using the ABS. Instead, import the support library into your workspace which can be found in ~/adt-bundle-linux-x86_64-20130729/sdk/extras/android/support/v7/appcompact and use is in your project. And also don't forget to add the support library which can be brought up right clicking into your project and entering Android Tools -> Add Support Library
Using appcompact you will have to extend your activity class with ActionBarActivity. And also using the appcompact you have to make changes in your styles folder. You could refer to this. Do not also forget to update the values-v11 and values-v14 file. Doing all of this will make your application compatible.
P.S. If any error occurs in your appcompact library. Don't panic look at the error logs and open the file that seems to contain the error. Most probably you will have to refresh the file and after that you just fix project properties, and the error goes away.
Hope this helps :)

Categories

Resources