Equivalent of NavUtils when not using the Android support library? - android

This question has also been asked by someone on the Android Developers Google Group (link), but it does not have an answer...
I recently removed the v4 support library from my Android project in Eclipse, because my application only targets Android 4.0 (Ice Cream Sandwich) and up. I'm now going through all the support library references that are giving me errors.
One in particular is NavUtils, which seems to be a support library class used in Activity navigation. Example:
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
What is the equivalent of NavUtils when not using the support library?

Look at the source code of NavUtils, if necessary just copy it to your project. It just gets the parent activity set in the manifest and starts it.

I found #Nikolay's approach to be the best if you don't want to have the whole android v4 support library in your project.
This is the list of classes you should copy into your project:
NavUtils
NavUtilsJB
IntentCompat
IntentCompatHoneycomb
IntentCompatIcsMr1

Android version from Jelly Beans have the NavUtils incorporated into the Activity class.
There is no need to handle the case : android.R.id.home in onOptionsItemSelected().
OR you can just return false for this case in onOptionsItemSelected().

Here is this simplest answer I could find, and what I did in my code:
Simply replace:
NavUtils.navigateUpFromSameTask(this);
with:
navigateUpTo(getParentActivityIntent());
Hope this helps!

Related

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;
}

Is it necessary to perform API if-else check when using AppCompat

Previously, I'm using SherlockActionBar library. The following code will work, across Android 2.3 till Android 5.
this.searchMenuItem.collapseActionView();
However, after migrating to AppCompat, we need to migrate to the following code
MenuItemCompat.collapseActionView(JStockFragmentActivity.this.searchMenuItem);
When I look at the documentation http://developer.android.com/guide/topics/ui/actionbar.html#ActionView, it states that
On API level 11 or higher
Get the action view by calling getActionView() on the corresponding
MenuItem:
menu.findItem(R.id.action_search).getActionView()
I was wondering, is it necessary that I need to write my migrated code in the following way?
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
this.searchMenuItem.collapseActionView();
} else {
MenuItemCompat.collapseActionView(this.searchMenuItem);
}

How can I implement actionbar to api level 8 easily?

I search on google and stackoverflow this topic.I ve found a few way to implement.
one of these is actionbarsherlock , but actually I do not understand how can implement this to my project. Is there any simple way? I mean a few classes or just add a library I do not know but I have a huge project and I want to implement this .Could you show me how can do it easily?
thanks
If you want to use ActionbarCompat library.
1) Import the ActionbarCompat library project into your workspace first and add the library to your project
https://developer.android.com/tools/support-library/setup.html#libs-with-res
2) Extend your Activity Class with ActionBarActivity
3) set your theme in manifest as
android:theme="#style/Theme.AppCompat"
Please check this link.
You can use android support library for this. No need of any other library.
Example also there in side link.
If You want to use ActionBar that supports devices with lower api..
you can do two things ...
1)Use the support Library(ActionbarCompat)
2)Use ActionBarSherlock
I use ActionBarsherlock
Steps to Use
1)YOURACTIVITY extends SherlockActivity
2) Use onCreateOptionsMenu to get the menu
`
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
SubMenu subMenu1 = menu.addSubMenu("");
subMenu1.add(0,2,Menu.NONE,"Rate Us").setIcon(R.drawable.ic_action_good);
MenuItem subMenu1Item = subMenu1.getItem();
subMenu1Item.setIcon(R.drawable.ic_action_overflow);
subMenu1Item.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
return super.onCreateOptionsMenu(menu);
}
3) Use onOptionsItemSelected to get the item selected
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 2:
//rate app
break;
return super.onOptionsItemSelected(item);
}
4)Finally in your AndroidManifest File, add this under your activity
android:theme="#style/Theme.Sherlock"
`
5) and you are Done ...:)
For setup support library see-
https://developer.android.com/tools/support-library/setup.html
And for implementing action bar using support library see this-
http://antonioleiva.com/actionbarcompat-how-to-use/

Zxing 2.2 and R.id error

I would like to integrate zxing into my app.
I'm importing project, set it as library, change target to API 7, download zxing-core-2.2.jar, copy it to /libs and add this jar in Java Building Path as library.
But there are still some errors:
All of them are in switch statements and depend on R.id. for example:
switch (item.getItemId()) {
case R.id.menu_share:
Eclipse error description:
case expressions must be constant expressions
there is a info dialog:
Any idea what I'm doing wrong or how to fix it?
As explained in the dialog you have shown, R.id.menu_settings is now "no longer constant", which means it cannot be used in a switch. the dialog also provides the solution, instead of
switch (item.getItemId()) {
case R.id.menu_share:
//do something
break;
case xxx:
...
}
You should do this:
if(item.getItemId()==R.id.menu_share) {
//do something
} else if (item.getItemId()==xxx) {
//do something
}
Just follow the instruction and you should be fine.
The problem is the source of zxing is not intended to be used as a Library..
Please see the answer by Sean in the below thread.
Zxing project as library in a project won't build
I tried bridling my app within this CaptureActivity project adding my activity and resources and modifying its manifest File accordingly.

Categories

Resources