Can´t get ActionBar on my activity class - android

When I call getActionBar throws me: "The return type is incompatible with Activity.getActionBar()"
I know it's a dumb question but I can´t find the solution.
public class ClaseContacto extends Activity{...
...
private void showGlobalContextActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setTitle(R.string.app_name);
}
private ActionBar getActionBar() {
return ( (ActionBarActivity) getActivity() ).getSupportActionBar();
}
...
//More code here

getSupportActionBar()? Are you using the android.support.v7 library?
If so, then extend ActionBarActivity instead of Activity. Then remove the getActionBar() method and Call getSupportActionBar() directly.

You don't need to override getActionBar();
In Activity, you just need to call
getActionBar()
and it will return ActionBar object for you
You can check this tutorial

You need to extend ActionBarActivity from support-v7-appcompat library.
And then you should use getSupportActionBar()
e.g.
public class SampleActivity extends ActionBarActivity {
}
and inside onCreate() function use below code to get action bar :-
ActionBar actionBar = getSupportActionBar();
Make sure you have imported android-support-v7-appcompat library in your IDE and added that as library project in your application project from build path -> android.

Related

ActionBar didn't work?

My application crashes and I get
Error :
setNavigationMode(int)' on a null object reference
Code :
package com.example.muhammad_adel.tabs;
import android.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText("one"));
actionBar.addTab(actionBar.newTab().setText("two"));
actionBar.addTab(actionBar.newTab().setText("three"));
}
}
Change 1 : instead of import android.support.v4.app.ActionBar use import android.support.v7.app.ActionBar in your activity
Change 2 : change this actionBar = getActionBar(); to actionbar = getSupportActionBar();
Update:
You have to use PagerSlidingTabStrip because setNavigationMode is now deprecated.
It's been long time since ActionBar got deprecated.
Try to use Toolbar and SlidingTabs.
Please refer to this answer for same question. It will provide you enough guide for your query.
Android, Tabs without Actionbar
You can easily implement this and it will be easy for you to handle onClick() events for tabs.
I find the solution thanks for your help
First instead of import android.app.ActionBar => import android.support.v7.app.ActionBar
Second changing this actionBar = getActionBar(); to actionbar = getSupportActionBar();
Third you must implement TabListener
You should Try to change your parent style
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

Android Action bar

I have second problem today with android developing.
I get error:
Cannot make a static reference to the non-static method getActionBar() from the type NavigationDrawerFragment
With code:
ActionBar actionBar = NavigationDrawerFragment.getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
and NavigationDrawerFragment.java:
public ActionBar getActionBar() {
return getActivity().getActionBar();
}
Thank you :)
Did your IDE set this up for you? If so, there should be a NavigationDrawerFragment Object (maybe called "mNavigationDrawerFragment")
Then use
mNavigationDrawerFragment.getActionBar();
But you should probably learn the Java basics first.
For that to work you would have to make the method like this
public static ActionBar getActionBar(){
return getActivity.getActionBar();
}
EDIT:
Create a field at the top of your fragment class
private static ActionBar mActionBar;
Then in the onCreate method add this
mActionBar = getActivity.getActionBar();
Now you can create your static method and return the actionbar
public static ActionBar getActionBar(){
return mActionBar;
}

Start activity without ActionBar and add it in the onCreate event

I'm using the AppCompat/ActionBarCompat library and I need to create a custom ActionBar. I need to open the activity without an ActionBar and enable it only when I add the custom view. How can I do this?
PS: I need to define the activity to not use an ActionBar through the AndroidManifest.xml and my application minimum API level is 10.
.hide() the action bar and then .show() the action bar when your ready for it
import android.support.v7.app.ActionBar;
...
private ActionBar mActionbar;
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mActionbar = getSupportActionBar();
mActionbar.hide()
}
...
somewhere when something cool happens
mActionbar.show();

ActionBar Compat not showing app icon

I'm setting an ActionBar in my app with the Compatibility version. For now I've done:
Import android-support-v7-appcompat and add as library to my project
Set Aplication theme as: Theme.AppCompat
Extend Activities to ActionBarActivity
After this, I use a method to set dynamically the subtitle:
private final void setStatus(int resId) {
ActionBar actionBar = getSupportActionBar();
actionBar.setSubtitle(resId);
}
private final void setStatus(CharSequence subTitle) {
ActionBar actionBar = getSupportActionBar();
actionBar.setSubtitle(subTitle);
}
While testing the app, the subtitle doesn't appear. If I add this:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE);
Then the subtitle appears, but the app icon dissapears. What can I do to mantain the app icon while showing the subtitle?
The display options are bitfields, so you should be able to enable several at the same time (using the OR operator), like this:
getSupportActionBar().setDisplayOptions(
ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE);
Or, to just add one value without affecting other fields, call the version with bitmask:
getSupportActionBar().setDisplayOptions(
ActionBar.DISPLAY_SHOW_TITLE,
ActionBar.DISPLAY_SHOW_TITLE);
This is what I get to solve the problem:
/**Resolves the issue, shows the app icon*/
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled (true);
Use getSupportActionbar instead of actionbar
Actionbar actionbar = getSupportActionBar()
actionbar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionbar.setIcon(YOUR ICON);
Ok, all of the above answer looks similar with minor differences, none of it work for me but this combo
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_HOME);
actionBar.setDisplayShowHomeEnabled (true);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setIcon(R.drawable.rn_logo_icon);
Please note, this fix is if you are using AppCompat theme

setDisplayHomeAsUpEnabled(true) doesn't show arrow if called from non Activity class

In my Activity I want to show arrow to left of ActionBar icon, so in Activity I write:
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
It works fine, but I decide move ActionBar initialization to another class and use it for all activities in my application, new class for this:
public class Utils {
public static void initActionBar(Activity activity, boolean homeIconNeeded) {
ActionBar actionBar = activity.getActionBar();
actionBar.setIcon(R.drawable.logo);
actionBar.setHomeButtonEnabled(homeIconNeeded);
actionBar.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.action_bar_background));
}
public static void initActionBar(ActionBar actionBar, boolean homeIconNeeded) {
actionBar.setIcon(R.drawable.logo);
actionBar.setHomeButtonEnabled(true);
actionBar.setBackgroundDrawable(SmartVmsApplication.getContext().getResources().getDrawable(R.drawable.action_bar_background));
}
}
Then in my activity I insert in onCreate() callback initActionBar(this, true), however arrow doesn’t appear, no matters I passed Activity or ActionBar as parameter and it is an issue.
You forgot to call the setDisplayHomeAsUpEnabled() method.

Categories

Resources