Android ActionBar Sherlock do not appear - android

I have instaled ActionBarSherlock from this tutorial. The problem is that when I have instaled Sherlock, in 2.2 version ActionBar still does not show up. Then I tried to implement ActionBar from Java file, when I start my app it threws error because I need at least 3.0 version. The Main Activtity is ListActivity, so my question is how can i implement SherlockActivity to ListAcivty and do I need to do it?

first of all you should declare its theme in the manifest
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.Sherlock.Light">
then in each activity you want the actionbar there you should extend it to the actionbar sherlock.
public class xxxx extends SherlockActivity {
and for listview, you can use actionbar sherlock's one by using this
... extends SherlockListActivity {
and this is in the end of you acitvity if you are going to use actionbar sherlock's menu buttons
#Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.MenuOfButtons, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.idOfButton:
//action done
break;
}
return true;
}
}

Check this video tutorial:
http://www.youtube.com/watch?v=1S9f7GQlWDg
You need to extend from SherlockActivity.
I am afraid you can't use sherlock action bar when extending from other class like ListActivity. May be some workaround of creating an inheritance heiratchy of ListActivity and SherlockActivity come handy but it will be very complex.
If you can't afford to avoid ListActivity, you can increase API level (I think 12) in your manifest and use the action bar provided by android API.

You want to use this in manifest.Whether you applied this?
android:theme="#style/Theme.Sherlock.Light"
Moreover declare this in Main Activity when you extend SherlockActivity
final ActionBar ab = getSupportActionBar();
// set defaults for logo & home up
ab.setDisplayHomeAsUpEnabled(false);
ab.setDisplayUseLogoEnabled(true);
ab.setDisplayShowTitleEnabled(true);

Related

Android Navigate To Previous Activity Not Working?

I have added a Toolbar to my AboutApplication activity. I have also added a navigation button to this toolbar -
when this button is clicked, the application should navigate to the previous activity (LoginActivity).
I tried calling the finish inside method onBackPressed() and this did not work. I tried creating an OnClickEventListener() for the toolbar's setNavigationOnClickListener() - this did not work either.
Here is the code for my AboutApplication activity:
public class AboutApplicationActivity extends AppCompatActivity {
private NavigationOnClickListener myListener;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_application);
myListener = new NavigationOnClickListener(this);
myListener.setNextActivity(new LoginActivity());
TextView toolbarTitle = (TextView) findViewById(R.id.aboutApplication);
Toolbar toolbar = (Toolbar) findViewById(R.id.aboutApplicationToolbar);
toolbar.setNavigationIcon(R.drawable.back);
toolbar.setNavigationOnClickListener(myListener);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
Do not confuse what you want with Navigation Design, you are requirement matches a lot like Up navigation provided by android, why do you want to make it around when this feature is already provided to you.
if your AboutApplication Activity comes from LoginActivity then you should apply Up Navigation provided my Android which know the best, let it handle it.
Things that you need to take care of while you are providing up navigation is that you properly define the child-parent structure in the AndroidManifest.xml file in your android project
<!-- Assuming this activity would be a parent -->
<activity
android:name="com.example.myfirstapp.LoginActivity" ...>
...
</activity>
<!-- this would be your child activity in your case `AboutApplicationActivity` -->
<activity
android:name=".AboutApplicationActivity"
>
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.LoginActivity" />
</activity>
now in your child activity things you need to make sure is this,
getActionBar().setDisplayHomeAsUpEnabled(true); // without support library
getSupportActionBar().setDisplayHomeAsUpEnabled(true); // support library
and offcours by now you know where does this goes
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
if you have hard time importing NavUtils.java, than write its fully qualified name android.support.v4.app.NavUtils
for more details use android documentation here,

Action bar setDisplayHomeAsUpEnabled closes app

My app uses the android.support.v7.app.ActionBar so that I can use the Action Bar on my Android 2.3.4 phone and my tablet using Android 4.4.2. I've got my minimum and target SDK's set at 10 and 18. When I click the back button on the Action Bar which calls setDisplayHomeAsUpEnabled(true) it works correctly on the 2.3.4 phone and goes back to the Home Activity called Main_Interface. But when I click the back button on the Action Bar on the tablet, it closes the app as if I clicked the back button on the tablet. Nothing comes up in LogCat, so it's acting as though it's supposed to close the app when the Action Bar setDisplayHomeAsUpEnabled() button is clicked. Here's what the Activity looks like in the Manifest:
<activity
android:name=".UILNPPager"
android:configChanges="orientation|screenSize"
android:label="UILNPPager"
android:parentActivityName="com.myapp.ktg.Main_Interface"
android:theme="#style/Theme.Blu_uilpager" >
<intent-filter>
<action android:name="com.myapp.ktg.UILNPPAGER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.myapp.ktg.Main_Interface" />
</activity>
Here's the Activity stripped of everything but the ActionBar calls:
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
public class UILNPPager extends ActionBarActivity {
private static final String STATE_POSITION = "STATE_POSITION";
DisplayImageOptions options;
ViewPager pager;
String[] imTitlesStr;
String[] imMediumStr;
String[] imPriceStr;
String[] imageUrls;
int pagerPosition;
PhotoViewAttacher mAttacher;
ActionBar mActionbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.actionbar_uilpager);
//--- ViewPager stuff
....
//--- END ViewPager stuff
/** Getting a reference to action bar of this activity */
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_actions, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
I tried taking the call android:parentActivityName="com.myapp.ktg.Main_Interface" out of the manifest, but that has no effect. It still works correctly on the 2.3.4 phone and not on the 4.4.2 tablet.
Any idea why it doesn't work right on the tablet?
Please use a AppCompat theme or a theme that inherits one of them. For example:
#style/ThemeAppCompat
#style/ThemeAppCompat.Light
#style/ThemeAppCompat.Light.DarkActionBar
I noticed that you are using your own styled theme, you can inherit the theme from one of the AppCompat theme as follow.
<style name="Theme.Blu_uilpager"
parent="#style/ThemeAppCompat.Light">
....
</style>
Quoted from a official Android tutorial, https://developer.android.com/training/basics/actionbar/styling.html
Note: If you are using the Support Library APIs for the action bar, then you must use (or override) the Theme.AppCompat family of styles (rather than the Theme.Holo family, available in API level 11 and higher). In doing so, each style property that you declare must be declared twice: once using the platform's style properties (the android: properties) and once using the style properties included in the Support Library (the appcompat.R.attr properties—the context for these properties is actually your app). See the examples below for details.
In my app I'm using my own transitions to move from Activity A to Activity B. I've discovered that the call overridePendingTransition(); in the onPause(); call in Activity A breaks the getSupportActionBar().setDisplayHomeAsUpEnabled(true); call in Activity B, which has the ActionBar. So on Activity A, I removed the onPause() method and called overridePendingTransition(); in the Intent that calls Activity B, like this:
Intent miI = new Intent();
miI.setClass(Splash.this, Main_Interface.class);
startActivity(miI);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
Then in Activity B I removed the onPause(); method and added overridePendingTransition(R.anim.fadein, R.anim.fadeout); to the onCreate(); method like so:
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
setContentView(R.layout.mainpage);
Then I put the overridePendingTransition(); call in all other Activities' onCreate(); methods just like in Activity B. Doing this makes the transition work correctly. And to make the ActionBar go back to the Main_Interface Activity correctly on my 4.4.2 tablet, I just call getSupportActionBar().setDisplayHomeAsUpEnabled(true);. And everything is working correctly!

Android no menu items showing in Action Bar

I am developing an Android app and I am trying to put a menu item into the ActionBar.
It has enough space, so it shouldn't be on the overflow or anything.
In my menu.xml I have added that item + android:showAsAction="ifRoom|withText"
However, no matter how large the screen is, that damn menu will not show up on the ActionBar (although it is present in the menu, if the user presses a key).
Unfortunately I cannot post any full-code since I am under a non-disclosure agreement, but I will answer all questions.
The section where I inflate the menu:
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.drinks, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.done:
//stuff
}
return super.onOptionsItemSelected(item);
}
Since you have only a menu item, use this attribute instead: android:showAsAction="always".
EDIT Above works all the time if you're running the code on post Honeycomb. But, in order to run on pre-Honeycomb, according to developer article, you need to extend from ActionBarActivity, that means adding compatibility support v4 & v7 and set the following theme for your activity:
<activity android:theme="#style/Theme.AppCompat.Light" ... >
... or a Them.AppCompat theme. Or use one of your own that extends from these.

Android Jellybean onCreateOptionsMenu is not called on Nexus 7

I have a TabActivity with four tabs. When I set android:targetSdkVersion="15" the onCreateOptionsMenu method is not called on any of the tab activities when testing on a Nexus 7.
It works correctly with android:targetSdkVersion="10".
With android:targetSdkVersion="15" it works correctly when the activities are not in a TabActivity and when tested on a phone (Evo).
Here is the code for onCreateOptionsMenu.
public boolean onCreateOptionsMenu(Menu menu) {
Log.i("Test","Base In create option menu");
if( menuId != null ) {
new MenuInflater(this).inflate(menuId,menu);
}
return super.onCreateOptionsMenu(menu);
}
Sorry but this is an easy one. According to Google:
http://developer.android.com/guide/topics/ui/menus.html
So, if you set the target SDK lower, you can show the deprecated options bar. For the newer SDKs you need to use an action bar or some other form of navigation. Basically Google decided that not all devices would have a "menu" button.

Android: How to add menu items to a ActionBarSherlock MapActivity

I am using ActionBarSherlock with ActionBarSherlock-Plugin-Maps in my project. I would like to add menu items to the actionbar of my MapActivity as it is possible for a standard activity that inherits from SherlockActivity. The following code sample shows how to create an icon.
public class CustomSherlockActivity extends SherlockActivity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("Save")
.setIcon(R.drawable.ic_compose)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
However, I can not use the same method in my MapActivity that extends SherlockMapActivity. The method SherlockMapActivity#onCreateOptionsMenu is defined final. Here is the source code of SherlockMapActivity. How then, am I supposed to add actions to the actionbar?
You have the wrong Menu class imported. Make sure you are importing the one from the com.actionbarsherlock.view package.

Categories

Resources