ActionBar Compatibility issue with API 15 - java

I have an issue with actionBar compatibility on API level 15 for Android. The up button doesn't work well with this API level
I use the sample project called "actionbarcompat" provided in the android-sdk folder, so I have imported all class's and I extends all my activities with ActionBarActivity. I also add this piece of code in the Manifest for all my activities :
<activity
android:name="fr.appsolute.rescue.MyActivity"
android:label="#string/title_activity_info"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
and in my ActionBarActivity :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
This permit the user to touch the up button (with the app icon) to return to the MainActivity
This code works well with Android 2.3.3 and 4.2, but doesn't work with 4.0.1 and I don't understand why. When the user touch the up button, nothing happens.
Thanks for your help.
PS : I can't use an external library, I have to use native code

Not sure why it isn't working. I agree it is strange that it only fails on API 15.
An alternative to calling NavUtils.navigateUpFromSameTask(this) could be calling finish(). This would close your current activity and go to the previous activity in the stack. It may give you the same desired result.

Solved it using ActionBarSherlock

Related

What might be preventing the HomeAsUp icon being displayed

I've been working on an app with multiple ActionBar activities, and from the outset the HomeAsUp button has been visible and working on all of the activities selected from the MainActivity. Today I noticed that it is no longer there - but I can't figure out what I have changed that would affect it.
The most significant change in recent days is that I migrated the project from Eclipse to Android Studio because I was having problems with Eclipse frequently corrupting my project files and requiring a complete reinstall.
This is a sample of the code in the main activity which creates the new activities:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent=null;
int id = item.getItemId();
switch (id) {
case R.id.action_list:
intent = new Intent(this, StudyListActivity.class);
break;
// etc
}
if (intent != null) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
Here is the declaration of the intent in the manifest
<activity
android:name="com.example.app.StudyListActivity"
android:label="#string/activity_list"
android:parentActivityName="com.example.app.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.app.MainActivity" />
</activity>
since it stopped working I have also tried putting
getSupportActionBar().setHomeButtonEnabled(true);
in the onCreate method of the called activity (as per Display back button on action bar) but that makes no difference.
So I'm wondering if there is anything that got stripped out in my migration from eclipse which is preventing it showing (or something else I could have changed which might inadvertently have disabled it)
you should enable it via setDisplayOptions
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME);
You could also try
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
In the activity you want the HomeAsUp icon in

Stuck on WebView Tutorial - R.id issue?

Good morning - I hope everyone has had an enjoyable weekend.
I am having some issues following the tutorial at https://developer.chrome.com/multidevice/webview/gettingstarted
Everything is going well until I reach the step regarding an edit of the MainActivity class. This is Step 3 in the section Add the WebView: https://developer.chrome.com/multidevice/webview/gettingstarted#add_the_webview
Here is the content of my AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nathan.myapplication" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MyActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
And here is my MyActivity.java:
package com.example.nathan.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
public class MyActivity extends Activity {
private WebView mWebView; // Added by ND Guthrie 8.15.2014:2229
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
mWebView = (WebView)findViewById(R.id.activity_my_webview); // Added by ND Guthrie 8.15.2014:2231
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I have tried suggestions based on these links here on stackoverflow:
Error with R class import in android
Android Studio don't generate R.java for my import project
R.id cannot be resolved
For the error with R class import, I have attempted adding the line
import com.TestApp.HelloWebView.R;
But with no success, as TestApp is not recognized. This stands to reason, since I have not named anything 'TestApp' in my app here, but I do not understand how to fix it.
I also tried deleting the generated folder and cleaning and rebuilding the project. However, I obtain the same results.
I know this is probably a silly little thing, but I have been searching google and stackoverflow for days now, and it seems like there is just something I am not seeing.
Any ideas? Please advise.
Thank you very much for your time!
Best,
Nathan
The way it works is that your XML files get compiled into resources in the background by Eclipse, and an R.java gets generated that allows your project to refer to these resources. If you put "R.id.blah" or "R.layout.blah" or whatever into Eclipse and it isn't recognised, it means one of the following:
Your XML files don't have corresponding elements. R.java is being created, but doesn't contain "R.id.blah" or whatever you're using, because your XML files don't contain elements that would compile with those IDs.
R.java couldn't be compiled at all. That'll be the case if there are errors in your project. You're best off making sure that there are no errors (no red crosses anywhere), and then seeing whether R.java appears. Once you've done that, you'll be able to find out what IDs are being generated, and that should point you in the direction of what's missing or inconsistent in your XML files.

Android Back Action Button Result in App Crash

I am building a new application which composed of 3 activities namely:
- Splash Screen
- Activity A
- Activity B, and
- Activity C
From activity A user can go to both activities indicated below:
A -> B -> C (from act A user can go to B then to C).
A -> C (from act A user can go straight to C).
B -> C (from B user can go to C).
i also pass Serializable intent Extra between activities.
The problem that i am having is whenever i pressed the back button on the Action Bar (Top Left Corner) it always makes my app crashes (Error: NULL Pointer Exception).
I have tried to place this code on ALL of my activity.
#Override
public void onBackPressed() {
this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
}
I tried to somehow mimic the physical backbutton behaviour since it is working when user press physical backbutton. But somewhat throws error as well.
or
public void onBackPressed(){
super.onBackPressed();
}
or (well this one literally restart the app, which is discourage since it restart the app from splash screen).
public void onBackPressed(){
super.onBackPressed();
finish();
}
Does anyone know the appropriate way to implement back button?
The button on the top left corner is not the "Back" button, it would be the "up" button, and it's just a button in the action bar, onBackPressed refers to the hardware back button being pressed.
Navigation with back and up is not necessarily the same ("back" means go to where I was before, whereas "up" means go to the upper level in the app hierarchy).
Take a look at http://developer.android.com/design/patterns/navigation.html for more information.
(Also, try to avoid a splash screen, they are highly discouraged in android design patterns)
Edit: I forgot to mention how to actually handle the "up" button. You do that on your activity's onOptionsItemSelected:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
// Handle "up" button behavior here.
return true;
} else {
// handle other items here
}
// return true if you handled the button click, otherwise return false.
}
You can do something like this when the back button is called
public void onBackPressed(){
// Add data to your intent
finish();
startActivity(intent);
}
And similar in your onClick method for your action bar button.
I think, the problems is from passing value using intent.
If the value is intended to be use by all of your activity, I think the best way
is to use a sharedpreferences.
If You are using API level 16 or more than , Really No need to do anythinngggg,
i just Find the solution :
in manifest file with every other activity Except MainActivity Add meta-data like this :
android:name="android.support.PARENT_ACTIVITY"
android:value="com.gsetia.ohama.MainActivity"/>
with in Activity like this , For Example
<activity
android:name=".ViewReports"
android:label="#string/title_activity_view_reports"
android:parentActivityName=".MainActivity"
android:theme="#style/AppTheme" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.gsetia.ohama.MainActivity"/>
</activity>
And In Last , delete public boolean onOptionsItemSelected(MenuItem item) Method. That's it.
You will find your Back Button and will work fine

"Hello world" android project, straight from book, yielding ActivityNotFoundException sometimes

I'm just learning to work with Android through a textbook (Learn Android App Development by Jackson). At the moment, I have a MainActivity class. I'm adding Intents to the menu of this activity to launch one of four other activities depending on which option. All activities are in the same package, and all have been declared in the AndroidManifest.XML file.
I am determining which activity to run using a Switch case as follows:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_add:
System.out.println("Add");
Intent intent_add = new Intent(this, NewPlanet.class);
this.startActivity(intent_add);
break;
case R.id.menu_attack:
System.out.println("Attack");
Intent intent_attack = new Intent(this, AttackActivity.class);
this.startActivity(intent_attack);
break;
case R.id.menu_config:
System.out.println("Config");
Intent intent_config = new Intent(this, ConfigActivity.class);
this.startActivity(intent_config);
break;
case R.id.menu_travel:
System.out.println("Travel");
Intent intent_travel = new Intent(this, TravelActivity.class);
this.startActivity(intent_travel);
break;
default:
return super.onOptionsItemSelected(item);
}
System.out.println("Outside switch.");
return true;
}
The problem, however, is that this only works when pressing the "Add" menu button, which successfully launches the NewPlanet activity and displays it.
All of the others, however, produce an ActivityNotFoundException and force the program to crash (same result on various combinations of virtual devices, as well as on my physical Galaxy Note II device).
I've done everything I can think of to try to fix this to no avail. As far as I know, the code is identical to that presented in the book, but the book has begun to move onto the next sections while my project is not yet working yet.
I have LogCat output if anybody wants to see that, but any help or advice would be greatly appreciated. I've Googled the issue which did not help much.
EDIT: As requested, here is my Manifest: (I attached it as a high-res images since I'm having trouble with the editor right now)
http://i.imgur.com/c7wJ8bM.png
And here is the relevant LogCat output:
07-03 10:42:53.954: E/AndroidRuntime(29754): FATAL EXCEPTION: main
07-03 10:42:53.954: E/AndroidRuntime(29754): android.content.ActivityNotFoundException: Unable to find explicit activity class {chapter.two.hello_world/chapter.two.hello_world.ConfigActivity}; have you declared this activity in your AndroidManifest.xml?
FINAL EDIT:
I've solved this problem thanks to user E. Odebugg: I was referring to my activities in the manifest by incorrect names (ConfigPlanet instead of ConfigActivity). I simply did not notice the difference. A silly mistake, but it has been fixed now. Thank you everybody for your help!
There might be something wrong with your manifest. Maybe you copied and pasted the same attributes for all Activities and hence have multiple Launcher IntentFilter Categories. Can you post both, the stack trace and the Manifest?
Clearly, the screen-shot you have shared shows that you have declared some ConfigPlanet activity, while in your switch-case u are calling ConfigActivity.
Replace .ConfigPlanet from your AndroidManifest.xml with .ConfigActivity, making sure that you do have a ConfigActivity.java file in your proper package.
You don't have the ConfigActivity declared in your manifest.
Somehow you have an entry there for ConfigPlanet which is not the same. So that's the cause.
Make sure that all your activities are declared in your AndroidManifest.xml.
<manifest ... >
<application ... >
<activity android:name="your.packagename.AttackActivity" />
<activity android:name="your.packagename.ConfigActivity" />
<activity android:name="your.packagename.TravelActivity" />
...
</application ... >
...
</manifest >
For details, see http://developer.android.com/guide/components/activities.html

Remove icon from launcher

It is most popular problem and no one know how to resolve it...
For example:
<application
android:allowBackup="true"
android:icon="#drawable/add"
android:theme="#style/CustomTheme"
android:showAsAction="ifRoom|withText">
And now i need to hide android:icon or just i wanna to remove it
<application
android:allowBackup="true"
android:theme="#style/CustomTheme"
android:showAsAction="ifRoom|withText">
But in this case i have default android icon -_-
It is possible to permanent remove this icon?
This can remove your launcher icon after next reboot:
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
Also this link can help you too:
Hide application launcher icon in title bar when activity starts in android
You may create you own icon picture.
So you may try just create picture which has necessary size and consist from transparent background only.
I have a solution! I had began learn java and android a couple of day ago, so my solution need verify by someone more experienced then me. Here u have the solution:
Step1
In AndroidManifest.xml file application section we need to declare NoTitleBar theme.
<application
...
android:theme="#android:style/Theme.NoTitleBar">
...
</application>
Step2
Now we have no title bar when our application loader (We set Theme.NoTitleBar), so we need to create it or change android:theme. I take a second option, so:
public class MainActivity extends SherlockActivity {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppTheme);
....
}
....
}
R.style.AppTheme is a style created by us but we can use anyone available style from android API.
Step3
Now our title bar is back but still we have our icon and title. So we need turn off it and now we have two options for do this. First is when we use android api 11 or later and we not need use ActionBarSherlock libs and second is for application lower then api 11.
API 11 or lower
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
...
}
...
API 11 or later
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
...
}
...
So, in first case we use default getActionBar() to manage actionBar but in second case we use getSupportActionBar from ActionBarSherlock
Done :)

Categories